-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gsender.cpp
183 lines (158 loc) · 4.35 KB
/
Gsender.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "Gsender.h"
Gsender* Gsender::instance = 0;
Gsender::Gsender(){}
Gsender* Gsender::get_instance()
{
if (instance == 0)
instance = new Gsender;
return instance;
}
Gsender* Gsender::server(const char* server) {
this->smtp_server = String(server);
return this->instance;
}
Gsender* Gsender::server(const String &server) {
return this->server(server.c_str());
}
Gsender* Gsender::port(uint16_t port){
this->smtp_port = port;
return this->instance;
}
Gsender* Gsender::login(const char* login){
this->email_login_base64 = String(login);
return this->instance;
}
Gsender* Gsender::login(const String &login){
return this->login(login.c_str());
}
Gsender* Gsender::password(const char* password) {
this->email_password_base64 = String(password);
return this->instance;
}
Gsender* Gsender::password(const String &password) {
return this->password(password.c_str());
}
Gsender* Gsender::from(const char* from) {
this->sender = String(from);
return this->instance;
}
Gsender* Gsender::from(const String &from) {
return this->from(from.c_str());
}
Gsender* Gsender::subject(const char* subject)
{
this->mail_subject = String(subject);
return this->instance;
}
Gsender* Gsender::subject(const String &subject)
{
return this->subject(subject.c_str());
}
bool Gsender::await_smtp_response(WiFiClientSecure &client, const String &resp, uint16_t timeOut)
{
uint32_t ts = millis();
while (!client.available())
{
if(millis() > (ts + timeOut)) {
this->error = "SMTP Response TIMEOUT!";
return false;
}
}
this->server_response = client.readStringUntil('\n');
#if defined(GS_SERIAL_LOG_1) || defined(GS_SERIAL_LOG_2)
Serial.println(this->server_response);
#endif
if (resp && this->server_response.indexOf(resp) == -1) return false;
return true;
}
String Gsender::get_last_response()
{
return this->server_response;
}
const char* Gsender::getError()
{
return this->error;
}
bool Gsender::send(const String &to, const String &message)
{
WiFiClientSecure client;
#if defined(GS_SERIAL_LOG_2)
Serial.print("Connecting to :");
Serial.println(this->smtp_server);
#endif
if(!client.connect(this->smtp_server.c_str(), this->smtp_port)) {
this->error = "Could not connect to mail server";
return false;
}
if(!this->await_smtp_response(client, "220")) {
this->error = "Connection Error";
return false;
}
#if defined(GS_SERIAL_LOG_2)
Serial.println("HELO friend:");
#endif
client.println("HELO friend");
if(!this->await_smtp_response(client, "250")){
this->error = "identification error";
return false;
}
#if defined(GS_SERIAL_LOG_2)
Serial.println("AUTH LOGIN:");
#endif
client.println("AUTH LOGIN");
this->await_smtp_response(client);
#if defined(GS_SERIAL_LOG_2)
Serial.println("EMAILBASE64_LOGIN:");
#endif
client.println(this->email_login_base64);
this->await_smtp_response(client);
#if defined(GS_SERIAL_LOG_2)
Serial.println("EMAILBASE64_PASSWORD:");
#endif
client.println(this->email_password_base64);
if (!this->await_smtp_response(client, "235")) {
this->error = "SMTP AUTH error";
return false;
}
String mailfrom = "MAIL from: <" + String(this->sender) + '>';
#if defined(GS_SERIAL_LOG_2)
Serial.println(mailfrom);
#endif
client.println(mailfrom);
this->await_smtp_response(client);
String rcpt = "RCPT TO: <" + to + '>';
#if defined(GS_SERIAL_LOG_2)
Serial.println(rcpt);
#endif
client.println(rcpt);
this->await_smtp_response(client);
#if defined(GS_SERIAL_LOG_2)
Serial.println("DATA:");
#endif
client.println("DATA");
if(!this->await_smtp_response(client, "354")) {
this->error = "SMTP DATA error";
return false;
}
client.println("From: <" + String(this->sender) + '>');
client.println("To: <" + to + '>');
client.print("Subject: ");
client.println(this->mail_subject);
client.println("Mime-Version: 1.0");
client.println("Content-Type: text/html; charset=\"UTF-8\"");
client.println("Content-Transfer-Encoding: 7bit");
client.println();
String body = "<!DOCTYPE html><html lang=\"en\">" + message + "</html>";
client.println(body);
client.println(".");
if (!this->await_smtp_response(client, "250")) {
error = "Sending message error";
return false;
}
client.println("QUIT");
if (!this->await_smtp_response(client, "221")) {
this->error = "SMTP QUIT error";
return false;
}
return true;
}