Experiment # 1: Using ESP32 To Send Emails Via An SMTP Server
Experiment # 1: Using ESP32 To Send Emails Via An SMTP Server
Experiment 1
Experiment # 1:
Using ESP32 to send Emails via an
SMTP Server
What is an SMTP server?
An SMTP server will have an address (or addresses) that can be set by the mail
client or application that you are using, and is generally formatted as
smtp.serveraddress.com. (For example, Gmail’s SMTP server address is
smtp.gmail.com, and Twilio SendGrid’s is smtp.sendgrid.com. You can generally
find your SMTP server address in the account or settings section of your mail
client.)
When you send an email, the SMTP server processes your email, decides which
server to send the message to, and relays the message to that server. The
recipient’s inbox service provider, such as Gmail or AOL then downloads the
message and places it in the recipient’s inbox.
To send emails with the ESP32, we’ll use the ESP-Mail-Client library. This library
allows the ESP32 to send and receive emails with or without attachments via
SMTP and IMAP servers.
In this experiment, we’ll use SMTP to send an email with and without
attachments. As an example, we’ll send an image (.png) and a text (.txt) file. The
files sent via email can be saved in the ESP32 Filesystem (SPIFFS) or a microSD
card (not covered in this tutorial).
Embedded Systems Laboratory 2
Experiment 1
Before proceeding with this tutorial, you need to install the ESP-Mail-Client library.
This library is not available to install through the Arduino IDE Library Manager.
Follow the next steps to install the library:
1. Download ESP-Mail-Client library .zip folder.
2. In your Arduino IDE, go to Sketch > Include Library > Add .ZIP Library.
3. Select the .zip file you’ve just downloaded.
Then, if you go to File > Examples > ESP-Mail-Client, you’ll find several examples that
you can try.
We recommend creating a new email account to send the emails to your main
personal email address. Do not use your main personal email to send emails via
ESP32. If something goes wrong in your code or if by mistake you make too many
requests, you can be banned or have your account temporarily disabled.
We’ll use a newly created Gmail.com account to send the emails, but you can use
any other email provider. The receiver email can be your personal email without any
problem.
If you’re using a Gmail account, these are the SMTP Server details:
For Live or Hotmail accounts, these are the SMTP Server settings:
The following code sends an email via SMTP Server with HTML or raw text. For
demonstration purposes, the ESP32 sends an email once when it boots. Then, you
should be able to modify the code and integrate it into your own projects.
Don’t upload the code yet, you need to make some modifications to make it work for
you.
You need to insert your network credentials as well as setting the sender email,
SMTP Server details, recipient and message.
Code:
#include <Arduino.h>
Embedded Systems Laboratory 4
Experiment 1
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <ESP_Mail_Client.h>
/* Recipient's email*/
#define RECIPIENT_EMAIL "RECIPIENTE_EMAIL@XXXX.com"
void setup(){
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to AP");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(200);
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Embedded Systems Laboratory 5
Experiment 1
Serial.println();
/*
Embedded Systems Laboratory 6
Experiment 1
message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_low;
message.response.notify = esp_mail_smtp_notify_success |
esp_mail_smtp_notify_failure | esp_mail_smtp_notify_delay;*/
void loop(){
This code is adapted from an example provided by the library. The example is well
commented so that you understand what each line of code does. Let’s just take a
look at the relevant parts that you need or may need to change.
Insert your SMTP server settings. If you’re using a Gmail account to send the emails,
these are the settings:
Insert the sender email sign in credentials (complete email and password)
Embedded Systems Laboratory 8
Experiment 1
Set the message headers in the following lines in the setup()—sender name, sender
email, email subject, and the recipient name and email:
In the following lines, set the content of the message (raw text) in
the textMsg variable:
If you want to send HTML text instead, uncomment the following lines— you should
insert your HTML text in the htmlMsg variable.
if (!MailClient.sendMail(&smtp, &message))
Serial.println("Error sending Email, " + smtp.errorReason());
Demonstration
Upload the code to your ESP32. After uploading, open the Serial Monitor at a baud
rate of 115200. Press the ESP32 Reset button.
If everything went as expected you should get a similar message in the Serial
Monitor.
Embedded Systems Laboratory 10
Experiment 1
Check your email account. You should have received an email from your ESP32
board.
If you set the option to send a message with HTML text, this is how the message
looks like:
If you’ve enable the raw text message, this is the email that you should receive.
Embedded Systems Laboratory 11
Experiment 1