Skip to content

Commit 0120a91

Browse files
committed
Import (work in progress) current version
1 parent b8efb2f commit 0120a91

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

SmtpClient.cpp

+67-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,64 @@
11
#include "SmtpClient.h"
22
#include "Client.h"
3-
#define SMTP_PORT 25
3+
4+
#define SMTP_PORT 25
5+
#define SMTP_SSL_PORT 465
6+
47
#define ZERO_IP IPAddress(0, 0, 0, 0)
58
#define TIMEOUT 10000
69
#define CRLF "\r\n"
710

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) {
923
}
1024

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;
1230
}
1331

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) {
1537
}
1638

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) {
1844
}
1945

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+
}
2057

2158
int SmtpClient::send(Mail *mail) {
2259
int result = connect();
2360
if (!result) {
61+
Error("connect failed");
2462
_client->stop();
2563
return 0;
2664
}
@@ -31,51 +69,68 @@ int SmtpClient::send(Mail *mail) {
3169

3270
int SmtpClient::_send(Mail *mail) {
3371
if (readStatus() != 220) {
72+
Error("ReadStatus != 220");
3473
return 0;
3574
}
3675
if (helo() != 250) {
76+
Error("HELO != 250");
3777
return 0;
3878
}
3979
if (mailFrom(mail->_from) != 250) {
80+
Error("MailFrom != 250");
4081
return 0;
4182
}
4283
if (rcptTo(mail) != 250) {
84+
Error("RcptTo != 250");
4385
return 0;
4486
}
4587
if (data() != 354) {
88+
Error("Data != 354");
4689
return 0;
4790
}
4891
headers(mail);
4992
body(mail->_body);
5093
if (finishBody() != 250) {
94+
Error("FinishBody != 250");
5195
return 0;
5296
}
5397
return 1;
5498
}
5599

100+
#include <ESP8266WiFi.h>
56101
int SmtpClient::connect() {
57102
if (strlen(_server) > 0) {
103+
Serial.printf("Connect %s %d\n", _server, _port);
58104
return _client->connect(_server, _port);
59105
} 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;
61116
}
62117
}
63118

64119
int SmtpClient::helo() {
65-
_client->print("EHLO");
120+
_client->print("EHLO 192.168.1.100");
66121
_client->print(CRLF);
67122
int status = readStatus();
68123
if (status == 250) {
69124
return status;
70125
}
71126
// IF server doesn't understand EHLO, try HELO
72-
_client->print("HELO");
127+
_client->print("HELO 192.168.1.100");
73128
_client->print(CRLF);
74129
return readStatus();
75130
}
76131

77132
int SmtpClient::mailFrom(char *from) {
78-
_client->print("MAIL FROM:");
133+
_client->print("MAIL FROM: ");
79134
_client->print(from);
80135
_client->print(CRLF);
81136
return readStatus();
@@ -84,7 +139,7 @@ int SmtpClient::mailFrom(char *from) {
84139
int SmtpClient::rcptTo(Mail *mail) {
85140
int status;
86141
for (uint8_t i = 0; i < mail->_recipientCount; i++) {
87-
_client->print("RCPT TO:");
142+
_client->print("RCPT TO: ");
88143
_client->print(mail->_recipients[i]);
89144
_client->print(CRLF);
90145

@@ -168,7 +223,7 @@ int SmtpClient::finishBody() {
168223
}
169224

170225
int SmtpClient::readStatus() {
171-
char line[4];
226+
char line[5];
172227
int result;
173228
while(true) {
174229
result = readLine(line, 4);
@@ -179,6 +234,7 @@ int SmtpClient::readStatus() {
179234
break;
180235
}
181236

237+
Serial.printf("ReadStatus {%s}\n", line);
182238
if (result < 3) {
183239
return 0;
184240
}

SmtpClient.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
#ifndef SMTP_CLIENT_H
22
#define SMTP_CLIENT_H
33

4-
#import "Arduino.h"
5-
#import "Client.h"
6-
#import "Mail.h"
4+
#include "Arduino.h"
5+
#include "Client.h"
6+
#include "Mail.h"
77

88
class SmtpClient {
99
public:
1010
SmtpClient(Client *client, char *server);
1111
SmtpClient(Client *client, char *server, uint16_t port);
1212
SmtpClient(Client *client, IPAddress serverIP);
1313
SmtpClient(Client *client, IPAddress serverIP, uint16_t port);
14+
SmtpClient(Client* client, char *server, bool use_ssl);
1415
int send(Mail *mail);
16+
int GetErrorLine();
17+
char *GetErrorText();
1518

1619
private:
1720
char *_server;
@@ -32,6 +35,9 @@ class SmtpClient {
3235
int finishBody();
3336
int readStatus();
3437
int readLine(char *line, int maxLen);
38+
39+
char *errortext;
40+
int errorline;
3541
};
3642

3743
#endif

0 commit comments

Comments
 (0)