-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsmtp.cpp
More file actions
181 lines (140 loc) · 3.3 KB
/
smtp.cpp
File metadata and controls
181 lines (140 loc) · 3.3 KB
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
#include "GrowduinoFirmware.h"
#include <Arduino.h>
#include "smtp.h"
/* * *
sends email mesasge
copied from http://playground.arduino.cc/Code/Email#.Uzc30HWx0S4
* * */
EthernetClient eth_client;
extern Config config;
int send_mail(char * dest, char * subject, char * body) {
byte smtp[4] = { config.smtp[0], config.smtp[1], config.smtp[2], config.smtp[3] };
if (eth_client.connect(smtp, 25)) {
#ifdef DEBUG_SMTP
SERIAL.println(F("SMTP connect"));
SERIAL.println(config.smtp);
#endif
lcd_publish(F("SMTP connect"));
} else {
#ifdef DEBUG_SMTP
SERIAL.println(F("connection failed"));
#endif
lcd_publish(F("SMTP fail"));
return 0;
}
if (!eRcv()) return 0;
#ifdef DEBUG_SMTP
SERIAL.println(F("Sending helo"));
#endif
eth_client.print(F("helo "));
eth_client.println(config.sys_name);
if (!eRcv()) return 0;
#ifdef DEBUG_SMTP
SERIAL.println(F("Sending From"));
#endif
// change to your email address (sender)
eth_client.print(F("MAIL From: <"));
eth_client.print(config.mail_from);
eth_client.println(F(">"));
if (!eRcv()) return 0;
// change to recipient address
#ifdef DEBUG_SMTP
SERIAL.println(F("Sending To"));
#endif
eth_client.print(F("RCPT To: <"));
eth_client.print(dest);
eth_client.println(F(">"));
if (!eRcv()) return 0;
#ifdef DEBUG_SMTP
SERIAL.println(F("Sending DATA"));
#endif
eth_client.println(F("DATA"));
if (!eRcv()) return 0;
#ifdef DEBUG_SMTP
SERIAL.println(F("Sending email"));
#endif
// change to recipient address
eth_client.print(F("To: <"));
eth_client.print(dest);
eth_client.println(F(">"));
// change to your address
eth_client.print(F("From: "));
eth_client.print(config.sys_name);
eth_client.print(F(" <"));
eth_client.print(config.mail_from);
eth_client.println(F(">"));
eth_client.print(F("Subject: "));
eth_client.print(config.sys_name);
eth_client.print(F(" alert"));
eth_client.println(F("\r\n"));
eth_client.println(body);
#ifdef DEBUG_SMTP
SERIAL.println(body);
#endif
eth_client.println(F("."));
if (!eRcv()) return 0;
#ifdef DEBUG_SMTP
SERIAL.println(F("Sending QUIT"));
#endif
eth_client.println(F("QUIT"));
if (!eRcv()) return 0;
eth_client.stop();
SERIAL.println(F("disconnected"));
return 1;
}
byte eRcv() {
byte respCode;
byte thisByte;
int loopCount = 0;
while (!eth_client.available()) {
#ifdef WATCHDOG
wdt_reset();
#endif
delay(1);
loopCount++;
// if nothing received for 10 seconds, timeout
if (loopCount > 10000) {
eth_client.stop();
SERIAL.println(F("\r\nTimeout"));
return 0;
}
}
respCode = eth_client.peek();
while (eth_client.available())
{
thisByte = eth_client.read();
SERIAL.write(thisByte);
}
if (respCode >= '4')
{
efail();
return 0;
}
return 1;
}
void efail()
{
byte thisByte = 0;
int loopCount = 0;
eth_client.println(F("QUIT"));
while (!eth_client.available()) {
#ifdef WATCHDOG
wdt_reset();
#endif
delay(1);
loopCount++;
// if nothing received for 10 seconds, timeout
if (loopCount > 10000) {
eth_client.stop();
SERIAL.println(F("\r\nTimeout"));
return;
}
}
while (eth_client.available())
{
thisByte = eth_client.read();
SERIAL.write(thisByte);
}
eth_client.stop();
SERIAL.println(F("disconnected"));
}