1
1
#include " SmtpClient.h"
2
2
#include " Client.h"
3
- #define SMTP_PORT 25
3
+
4
+ #define SMTP_PORT 25
5
+ #define SMTP_SSL_PORT 465
6
+
4
7
#define ZERO_IP IPAddress (0 , 0 , 0 , 0 )
5
8
#define TIMEOUT 10000
6
9
#define CRLF " \r\n "
7
10
8
- SmtpClient::SmtpClient (Client* client, char *server) : _client(client), _server(server), _serverIP(ZERO_IP), _port(SMTP_PORT) {
11
+ SmtpClient::SmtpClient (Client* client, char *server) :
12
+ _client(client),
13
+ _server(server),
14
+ _serverIP(ZERO_IP),
15
+ _port(SMTP_PORT) {
16
+ }
17
+
18
+ SmtpClient::SmtpClient (Client* client, char *server, uint16_t port) :
19
+ _client(client),
20
+ _server(server),
21
+ _serverIP(ZERO_IP),
22
+ _port(port) {
9
23
}
10
24
11
- SmtpClient::SmtpClient (Client* client, char *server, uint16_t port) : _client(client), _server(server), _serverIP(ZERO_IP), _port(port) {
25
+ SmtpClient::SmtpClient (Client* client, char *server, bool use_ssl) :
26
+ _client(client),
27
+ _server(server),
28
+ _serverIP(ZERO_IP) {
29
+ _port = use_ssl ? SMTP_SSL_PORT : SMTP_PORT;
12
30
}
13
31
14
- SmtpClient::SmtpClient (Client* client, IPAddress serverIP) : _client(client), _serverIP(serverIP), _server(" " ), _port(SMTP_PORT) {
32
+ SmtpClient::SmtpClient (Client* client, IPAddress serverIP) :
33
+ _client(client),
34
+ _serverIP(serverIP),
35
+ _server(" " ),
36
+ _port(SMTP_PORT) {
15
37
}
16
38
17
- SmtpClient::SmtpClient (Client* client, IPAddress serverIP, uint16_t port) : _client(client), _serverIP(serverIP), _server(" " ), _port(port) {
39
+ SmtpClient::SmtpClient (Client* client, IPAddress serverIP, uint16_t port) :
40
+ _client(client),
41
+ _serverIP(serverIP),
42
+ _server(" " ),
43
+ _port(port) {
18
44
}
19
45
46
+ #define Error (txt ) \
47
+ errorline = __LINE__; \
48
+ errortext = txt;
49
+
50
+ int SmtpClient::GetErrorLine () {
51
+ return errorline;
52
+ }
53
+
54
+ char *SmtpClient::GetErrorText () {
55
+ return errortext;
56
+ }
20
57
21
58
int SmtpClient::send (Mail *mail) {
22
59
int result = connect ();
23
60
if (!result) {
61
+ Error (" connect failed" );
24
62
_client->stop ();
25
63
return 0 ;
26
64
}
@@ -31,51 +69,68 @@ int SmtpClient::send(Mail *mail) {
31
69
32
70
int SmtpClient::_send (Mail *mail) {
33
71
if (readStatus () != 220 ) {
72
+ Error (" ReadStatus != 220" );
34
73
return 0 ;
35
74
}
36
75
if (helo () != 250 ) {
76
+ Error (" HELO != 250" );
37
77
return 0 ;
38
78
}
39
79
if (mailFrom (mail->_from ) != 250 ) {
80
+ Error (" MailFrom != 250" );
40
81
return 0 ;
41
82
}
42
83
if (rcptTo (mail) != 250 ) {
84
+ Error (" RcptTo != 250" );
43
85
return 0 ;
44
86
}
45
87
if (data () != 354 ) {
88
+ Error (" Data != 354" );
46
89
return 0 ;
47
90
}
48
91
headers (mail);
49
92
body (mail->_body );
50
93
if (finishBody () != 250 ) {
94
+ Error (" FinishBody != 250" );
51
95
return 0 ;
52
96
}
53
97
return 1 ;
54
98
}
55
99
100
+ #include < ESP8266WiFi.h>
56
101
int SmtpClient::connect () {
57
102
if (strlen (_server) > 0 ) {
103
+ Serial.printf (" Connect %s %d\n " , _server, _port);
58
104
return _client->connect (_server, _port);
59
105
} else {
60
- return _client->connect (_serverIP, _port);
106
+ Serial.print (" Local IP " );
107
+ Serial.print (WiFi.localIP ());
108
+ Serial.print (" , subnet mask " );
109
+ Serial.print (WiFi.subnetMask ());
110
+ Serial.print (" , connect to IP " );
111
+ Serial.print (_serverIP);
112
+ Serial.printf (" , %d\n " ,_port);
113
+ int r = _client->connect (_serverIP, _port);
114
+ Serial.printf (" connect -> %d\n " , r);
115
+ return r;
61
116
}
62
117
}
63
118
64
119
int SmtpClient::helo () {
65
- _client->print (" EHLO" );
120
+ _client->print (" EHLO 192.168.1.100 " );
66
121
_client->print (CRLF);
67
122
int status = readStatus ();
68
123
if (status == 250 ) {
69
124
return status;
70
125
}
71
126
// IF server doesn't understand EHLO, try HELO
72
- _client->print (" HELO" );
127
+ _client->print (" HELO 192.168.1.100 " );
73
128
_client->print (CRLF);
74
129
return readStatus ();
75
130
}
76
131
77
132
int SmtpClient::mailFrom (char *from) {
78
- _client->print (" MAIL FROM:" );
133
+ _client->print (" MAIL FROM: " );
79
134
_client->print (from);
80
135
_client->print (CRLF);
81
136
return readStatus ();
@@ -84,7 +139,7 @@ int SmtpClient::mailFrom(char *from) {
84
139
int SmtpClient::rcptTo (Mail *mail) {
85
140
int status;
86
141
for (uint8_t i = 0 ; i < mail->_recipientCount ; i++) {
87
- _client->print (" RCPT TO:" );
142
+ _client->print (" RCPT TO: " );
88
143
_client->print (mail->_recipients [i]);
89
144
_client->print (CRLF);
90
145
@@ -168,7 +223,7 @@ int SmtpClient::finishBody() {
168
223
}
169
224
170
225
int SmtpClient::readStatus () {
171
- char line[4 ];
226
+ char line[5 ];
172
227
int result;
173
228
while (true ) {
174
229
result = readLine (line, 4 );
@@ -179,6 +234,7 @@ int SmtpClient::readStatus() {
179
234
break ;
180
235
}
181
236
237
+ Serial.printf (" ReadStatus {%s}\n " , line);
182
238
if (result < 3 ) {
183
239
return 0 ;
184
240
}
0 commit comments