-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmtpClient.cpp
279 lines (249 loc) · 5.46 KB
/
SmtpClient.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "SmtpClient.h"
#include "Client.h"
#define SMTP_PORT 25
#define SMTP_SSL_PORT 465
#define ZERO_IP IPAddress(0, 0, 0, 0)
#define TIMEOUT 10000
#define CRLF "\r\n"
SmtpClient::SmtpClient(Client* client, char *server) :
_client(client),
_server(server),
_serverIP(ZERO_IP),
_port(SMTP_PORT) {
}
SmtpClient::SmtpClient(Client* client, char *server, uint16_t port) :
_client(client),
_server(server),
_serverIP(ZERO_IP),
_port(port) {
}
SmtpClient::SmtpClient(Client* client, char *server, bool use_ssl) :
_client(client),
_server(server),
_serverIP(ZERO_IP) {
_port = use_ssl ? SMTP_SSL_PORT : SMTP_PORT;
}
SmtpClient::SmtpClient(Client* client, IPAddress serverIP) :
_client(client),
_serverIP(serverIP),
_server(""),
_port(SMTP_PORT) {
}
SmtpClient::SmtpClient(Client* client, IPAddress serverIP, uint16_t port) :
_client(client),
_serverIP(serverIP),
_server(""),
_port(port) {
}
#define Error(txt) \
errorline = __LINE__; \
errortext = txt;
int SmtpClient::GetErrorLine() {
return errorline;
}
char *SmtpClient::GetErrorText() {
return errortext;
}
int SmtpClient::send(Mail *mail) {
int result = connect();
if (!result) {
Error("connect failed");
_client->stop();
return 0;
}
result = _send(mail);
_client->stop();
return result;
}
int SmtpClient::_send(Mail *mail) {
if (readStatus() != 220) {
Error("ReadStatus != 220");
return 0;
}
if (helo() != 250) {
Error("HELO != 250");
return 0;
}
if (mailFrom(mail->_from) != 250) {
Error("MailFrom != 250");
return 0;
}
if (rcptTo(mail) != 250) {
Error("RcptTo != 250");
return 0;
}
if (data() != 354) {
Error("Data != 354");
return 0;
}
headers(mail);
body(mail->_body);
if (finishBody() != 250) {
Error("FinishBody != 250");
return 0;
}
return 1;
}
#include <ESP8266WiFi.h>
int SmtpClient::connect() {
if (strlen(_server) > 0) {
Serial.printf("Connect %s %d\n", _server, _port);
return _client->connect(_server, _port);
} else {
Serial.print("Local IP ");
Serial.print(WiFi.localIP());
Serial.print(", subnet mask ");
Serial.print(WiFi.subnetMask());
Serial.print(", connect to IP ");
Serial.print(_serverIP);
Serial.printf(", %d\n",_port);
int r = _client->connect(_serverIP, _port);
Serial.printf("connect -> %d\n", r);
return r;
}
}
int SmtpClient::helo() {
_client->print("EHLO 192.168.1.100");
_client->print(CRLF);
int status = readStatus();
if (status == 250) {
return status;
}
// IF server doesn't understand EHLO, try HELO
_client->print("HELO 192.168.1.100");
_client->print(CRLF);
return readStatus();
}
int SmtpClient::mailFrom(char *from) {
_client->print("MAIL FROM: ");
_client->print(from);
_client->print(CRLF);
return readStatus();
}
int SmtpClient::rcptTo(Mail *mail) {
int status;
for (uint8_t i = 0; i < mail->_recipientCount; i++) {
_client->print("RCPT TO: ");
_client->print(mail->_recipients[i]);
_client->print(CRLF);
status = readStatus();
if (status != 250) {
break;
}
}
return status;
}
int SmtpClient::data() {
_client->print("DATA ");
_client->print(CRLF);
return readStatus();
}
void SmtpClient::headers(Mail *mail) {
header("From:", mail->_from);
if (mail->_replyTo) {
header("Reply-To:", mail->_replyTo);
}
recipientHeader("To:", TO, mail);
recipientHeader("Cc:", CC, mail);
recipientHeader("Bcc:", BCC, mail);
header("Subject:", mail->_subject);
}
void SmtpClient::header(char* header, char* value) {
_client->print(header);
_client->print(value);
_client->print(CRLF);
}
void SmtpClient::recipientHeader(char* header, recipient_t type, Mail *mail) {
int first = 1;
for (int i = 0; i < mail->_recipientCount; i++) {
if (mail->_recipientTypes[i] == type) {
if (first) {
_client->print(header);
first = 0;
} else {
_client->print(',');
}
_client->print(mail->_recipients[i]);
}
}
if (!first) {
_client->print(CRLF);
}
}
void SmtpClient::body(char *body) {
int cr = 0;
int lf = 0;
while(*body != '\0') {
if (cr && lf && *body == '.') {
// Add a second period to escapt the newline/period
_client->print('.');
}
if (cr && *body == '\n') {
lf = 1;
} else if (*body == '\r') {
cr = 1;
lf = 0;
} else {
cr = lf = 0;
}
_client->print((char) *body++);
}
}
int SmtpClient::finishBody() {
_client->print(CRLF);
_client->print('.');
_client->print(CRLF);
return readStatus();
}
int SmtpClient::readStatus() {
char line[5];
int result;
while(true) {
result = readLine(line, 4);
if (result >= 4 && (line[3] == '-')) {
// Multiline result
continue;
}
break;
}
Serial.printf("ReadStatus {%s}\n", line);
if (result < 3) {
return 0;
}
char st[4];
strncpy(st, line, 3);
st[3] = '\0';
return atoi(st);
}
int SmtpClient::readLine(char *line, int maxLen) {
long start = millis();
int count = 0;
int cr = 0;
while (true) {
long now = millis();
if (now < start || now - start > TIMEOUT) {
return 0;
}
int c = _client->read();
if (c != -1) {
if (count < maxLen) {
line[count++] = c;
}
if (cr && c == '\n') {
break;
}
if (c == '\r') {
cr = 1;
continue;
} else {
cr = 0;
}
}
}
if (count == maxLen - 1) {
line[count - 1] = '\0';
} else {
line[count] = '\0';
}
return count;
}