forked from SmingHub/Sming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.cpp
92 lines (71 loc) · 2.34 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
88
89
90
91
92
#include <user_config.h>
#include "SmingCore/SmingCore.h"
#include "SmingCore/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
#define SMTP_PASSWORD
#define SMTP_HOST "attachix.com"
#define SMTP_PORT 25
#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. Status: %s", status);
// And if there are no more pending emails then you can disconnect from the server
if(!client.countPending()) {
debugf("No more mails to send. Quitting...");
client.quit();
}
return 0;
}
void onConnected(IPAddress ip, IPAddress mask, IPAddress gateway)
{
#ifdef ENABLE_SSL
client.addSslOptions(SSL_SERVER_VERIFY_LATER);
#endif
client.onServerError(onServerError);
String dsn = "smtp";
if(SMTP_USE_SSL) {
dsn += "s";
}
dsn += String("://") + SMTP_USERNAME + ":" + SMTP_PASSWORD + "@" + SMTP_HOST + ":" + SMTP_PORT;
debugf("Connecting to SMTP server using: %s", dsn.c_str());
client.connect(URL(dsn));
MailMessage* mail = new MailMessage();
mail->from = MAIL_FROM;
mail->to = MAIL_TO;
mail->subject = "Greetings from Sming";
mail->setBody("Hello.\r\n.\r\n"
"This is test email from Sming <https://github.com/SmingHub/Sming>"
"It contains attachment, Ümlauts, кирилица + etc");
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);
}