ūüď© Sending e-mails from nodemcu

In this tutorial, I will tell you how to send emails with nodemcu. For this, you need a nodemcu or esp32 or another board with an esp module.
How_to_stay_healthy_this_summer

In this tutorial, I will tell you how to send emails with nodemcu. For this, you need a nodemcu or esp32 or another board with an esp module.

Setting up an SMTP server.

Emails are sent using the SMTP protocol. Let’s use the SMTP server ‚ÄúSMTP2GO‚ÄĚ. You may use others, but difficulties may arise.

Go to the site SMTP2GO and register a free account. Then confirm the account by the link from the letter.
Open ‚ÄúDashboard‚ÄĚ -> ‚ÄúSettings‚ÄĚ -> ‚ÄúUsers‚ÄĚ. Here you need to create a user and take the server and port. Usually this:

  • SMTP server: mail.smtp2go.com

  • SMTP port: 2525

Encode a login and password.

You need to recode the SMTP username and SMTP password to Base64 format using the ASCII character set. We use BASE64ENCODE. This is necessary so that our requests can be processed correctly.

In turn, recode the name and password. An example in the picture.

Sketch.

In the code, change the settings of the wifi network, the e-mail address of the sender and the recipient. SMTP login and password (in ASCII).

#include
const char* ssid = “ssid”; ¬†¬†¬†¬†¬†¬†¬†¬†¬†// wifi¬†ssid
const char* password = “password”; ¬†//¬†wifi password
char server[] = “mail.smtp2go.com”; // SMTP¬†server
#define ASCII_username ASCIIusername //ascii login
#define ASCII_password ASCIIpassword //ascii password
//the address of the sender and recipient on line 71, 76 and 87, 88
WiFiClient espClient;
void setup() {
 Serial.begin(9600);
 delay(10);
 //connecting to wifi
¬†Serial.print(“Connecting To: “);
 Serial.println(ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED)  {
   delay(500);
¬†¬†¬†Serial.print(“*”);
 }
¬†Serial.println(“”);
¬†Serial.println(“WiFi Connected.”);
¬†Serial.print(“IP address: “);
 Serial.println(WiFi.localIP());
// send e-mail
 byte ret = sendEmail();
}
void loop() {
}
byte sendEmail() {
 //trying to connect to server
 if (espClient.connect(server, 2525) == 1)  {
¬†¬†¬†Serial.println(F(“connected”));
 }
 else {
¬†¬†¬†Serial.println(F(“connection failed”));
   return 0;
 }
 //check respond
 if (!emailResp()) {
   return 0;
 }
¬†Serial.println(F(“Sending EHLO”));
¬†espClient.println(“EHLO www.example.com”);
 if (!emailResp()) {
   return 0;
 }
 
 //login and password sending
¬†Serial.println(F(“Sending auth login”));
¬†espClient.println(“AUTH LOGIN”);
 if (!emailResp()) {
   return 0;
 }
¬†Serial.println(F(“Sending User”));
 espClient.println(ASCII_username);
 if (!emailResp()) {
   return 0;
 }
¬†Serial.println(F(“Sending Password”));
¬†espClient.println(“ASCII_password”);
 if (!emailResp()) {
   return 0;
 }
 //sending email
¬†Serial.println(F(“Sending From”));
¬†espClient.println(F(“MAIL From: [email protected]”));//sender’s address¬†if (!emailResp()) {
   return 0;
 }
¬†Serial.println(F(“Sending To”));
¬†espClient.println(F(“RCPT To: [email protected]”));//address of the recipient
 if (!emailResp()) {
   return 0;
 }
¬†Serial.println(F(“Sending DATA”));
¬†espClient.println(F(“DATA”));
 if (!emailResp()) {
   return 0;
 }
¬†Serial.println(F(“Sending email”));
¬†//–ĺ—ā–Ņ—Ä–į–≤–ļ–į –Ņ–ł—Ā—Ć–ľ–į
¬†espClient.println(F(“To: ¬†[email protected]”)); //address of the recipient
¬†espClient.println(F(“From: [email protected]”));//sender’s adress
¬†espClient.println(F(“Subject: ESP8266 test e-mailrn”)); //topic of the letter
¬†espClient.println(F(“This is a test e-mail sent from ESP8266.n”));//first line of the letter
¬†espClient.println(F(“Second line of the test e-mail.n”));//second line of the letter
¬†espClient.println(F(“Third line of the test e-mail.”));//third¬†line of the letter
¬†espClient.println(F(“.”));
 //getting respond
 if (!emailResp()) {
   return 0;
 }
 //exitting
¬†Serial.println(F(“Sending QUIT”));
¬†espClient.println(F(“QUIT”));
 if (!emailResp()) {
   return 0;
 }
 espClient.stop();
¬†Serial.println(F(“disconnected”));
 return 1;
}
//response check function
byte emailResp() {
 byte responseCode;
 byte readByte;
 int loopCount = 0;
 while (!espClient.available()) {
   delay(1);
   loopCount++;
   // Wait for 20 seconds and if nothing is received, stop.
   if (loopCount > 20000) {
     espClient.stop();
¬†¬†¬†¬†¬†Serial.println(F(“rnTimeout”));
     return 0;
   }
 }
 responseCode = espClient.peek();
 while (espClient.available()) {
   readByte = espClient.read();
   Serial.write(readByte);
 }
¬†if (responseCode >= ‘4’) {
   return 0;
 }
 return 1;
}
 
The letter is sent when the board is turned on. It gets into spam.
Congratulations! Now your board can communicate with the outside world.
Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest