forked from SmingHub/Sming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.cpp
87 lines (67 loc) · 2.47 KB
/
application.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
#include <SmingCore.h>
#include <Network/SmtpClient.h>
// If you want, you can define WiFi settings globally in Eclipse Environment Variables
#ifndef WIFI_SSID
#define WIFI_SSID "PleaseEnterSSID" // Put you SSID and Password here
#define WIFI_PWD "PleaseEnterPass"
#endif
// Make sure to change those to your desired values
#define MAIL_FROM "[email protected]"
#define MAIL_TO "[email protected]"
#define SMTP_USERNAME nullptr
#define SMTP_PASSWORD nullptr
#define SMTP_HOST "attachix.com"
#define SMTP_PORT 0 // Use default port
#define SMTP_USE_SSL false
SmtpClient client;
int onServerError(SmtpClient& client, int code, char* status)
{
debugf("Status: %s", status);
return 0; // return non-zero value to abort the connection
}
int onMailSent(SmtpClient& client, int code, char* status)
{
// get the sent mail message
MailMessage* mail = client.getCurrentMessage();
// TODO: The status line contains the unique ID that was given to this email
debugf("Mail sent to '%s'. Status: %s", mail->to.c_str(), status);
// And if there are no more pending emails then you can disconnect from the server
if(client.countPending() == 0) {
debugf("No more mails to send. Quitting...");
client.quit();
}
return 0;
}
void onConnected(IpAddress ip, IpAddress mask, IpAddress gateway)
{
client.setSslInitHandler([](Ssl::Session& session) { session.options.verifyLater = true; });
client.onServerError(onServerError);
Url dsn(SMTP_USE_SSL ? URI_SCHEME_SMTP_SECURE : URI_SCHEME_SMTP, SMTP_USERNAME, SMTP_PASSWORD, SMTP_HOST,
SMTP_PORT);
debugf("Connecting to SMTP server using: %s", String(dsn).c_str());
client.connect(dsn);
MailMessage* mail = new MailMessage();
mail->from = MAIL_FROM;
mail->to = MAIL_TO;
mail->subject = "Greetings from Sming";
String body = F("Hello.\r\n.\r\n"
"This is test email from Sming <https://github.com/SmingHub/Sming>"
"It contains attachment, Ümlauts, кирилица + etc");
// Note: Body can be quite large so use move semantics to avoid additional heap allocation
mail->setBody(std::move(body));
FileStream* file = new FileStream("image.png");
mail->addAttachment(file);
client.onMessageSent(onMailSent);
client.send(mail);
}
void init()
{
Serial.begin(SERIAL_BAUD_RATE);
Serial.systemDebugOutput(true);
Serial.println("Sming: SmtpClient example!");
spiffs_mount();
// Setup the WIFI connection
WifiStation.enable(true);
WifiStation.config(WIFI_SSID, WIFI_PWD); // Put you SSID and Password here
WifiEvents.onStationGotIP(onConnected);
}