-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gsender.cpp
143 lines (128 loc) · 3.3 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
#include "Gsender.h"
Gsender* Gsender::_instance = 0;
Gsender::Gsender(){}
Gsender* Gsender::Instance()
{
if (_instance == 0)
_instance = new Gsender;
return _instance;
}
Gsender* Gsender::Subject(const char* subject)
{
delete [] _subject;
_subject = new char[strlen(subject)+1];
strcpy(_subject, subject);
return _instance;
}
Gsender* Gsender::Subject(const String &subject)
{
return Subject(subject.c_str());
}
bool Gsender::AwaitSMTPResponse(WiFiClientSecure &client, const String &resp, uint16_t timeOut)
{
uint32_t ts = millis();
while (!client.available())
{
if(millis() > (ts + timeOut)) {
_error = "SMTP Response TIMEOUT!";
return false;
}
}
_serverResponce = client.readStringUntil('\n');
#if defined(GS_SERIAL_LOG_1) || defined(GS_SERIAL_LOG_2)
Serial.println(_serverResponce);
#endif
if (resp && _serverResponce.indexOf(resp) == -1) return false;
return true;
}
String Gsender::getLastResponce()
{
return _serverResponce;
}
const char* Gsender::getError()
{
return _error;
}
bool Gsender::Send(const String &to, const String &message)
{
WiFiClientSecure client;
#if defined(GS_SERIAL_LOG_2)
Serial.print("Connecting to :");
Serial.println(SMTP_SERVER);
#endif
if(!client.connect(SMTP_SERVER, SMTP_PORT)) {
_error = "Could not connect to mail server";
return false;
}
if(!AwaitSMTPResponse(client, "220")) {
_error = "Connection Error";
return false;
}
#if defined(GS_SERIAL_LOG_2)
Serial.println("HELO friend:");
#endif
client.println("HELO friend");
if(!AwaitSMTPResponse(client, "250")){
_error = "identification error";
return false;
}
#if defined(GS_SERIAL_LOG_2)
Serial.println("AUTH LOGIN:");
#endif
client.println("AUTH LOGIN");
AwaitSMTPResponse(client);
#if defined(GS_SERIAL_LOG_2)
Serial.println("EMAILBASE64_LOGIN:");
#endif
client.println(EMAILBASE64_LOGIN);
AwaitSMTPResponse(client);
#if defined(GS_SERIAL_LOG_2)
Serial.println("EMAILBASE64_PASSWORD:");
#endif
client.println(EMAILBASE64_PASSWORD);
if (!AwaitSMTPResponse(client, "235")) {
_error = "SMTP AUTH error";
return false;
}
String mailFrom = "MAIL FROM: <" + String(FROM) + '>';
#if defined(GS_SERIAL_LOG_2)
Serial.println(mailFrom);
#endif
client.println(mailFrom);
AwaitSMTPResponse(client);
String rcpt = "RCPT TO: <" + to + '>';
#if defined(GS_SERIAL_LOG_2)
Serial.println(rcpt);
#endif
client.println(rcpt);
AwaitSMTPResponse(client);
#if defined(GS_SERIAL_LOG_2)
Serial.println("DATA:");
#endif
client.println("DATA");
if(!AwaitSMTPResponse(client, "354")) {
_error = "SMTP DATA error";
return false;
}
client.println("From: <" + String(FROM) + '>');
client.println("To: <" + to + '>');
client.print("Subject: ");
client.println(_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 (!AwaitSMTPResponse(client, "250")) {
_error = "Sending message error";
return false;
}
client.println("QUIT");
if (!AwaitSMTPResponse(client, "221")) {
_error = "SMTP QUIT error";
return false;
}
return true;
}