Send Email using ESP32 with SMTP Server: Plain and HTML email

This guide describes how to send email using ESP32 with an SMTP server. There are several scenarios in IoT where there is the need to send an email:

  • alerting email
  • status email
  • email containing information (such as sensor reading and so on)

An email is the simplest way to exchange information and it is a common way, we all use them. Therefore it is useful to learn how to send emails using ESP32. Before doing it, it is necessary to explore how to connect the ESP32 to the SMTP server

Project overview

In this project, you will learn how to send two different type of emails:

  • plain text email: simple email body
  • html email: the email body is in the HTML format

To apply it to a real case example, we will send an email from ESP32 through Google Gmail. Moreover, the email body will contains the temperature and humidity read using DHT11 sensor. You can extend this example and using other kind of sensors or other email providers.

Prerequisites

In order to complete this tutorial, you need the following components:

  • ESP32 or a compatible device
  • a Google Gmail account. You can make it for free here.
  • a DHT11 sensor or another kind of compatible sensor

How to send Email using ESP32

Generally speaking, to send an email we use the SMTP protocol (Simple Mail Transfer Protocol). To do it, we need:

  • a sender account
  • a recipient (the destination email)
  • a body (the mail content). The body can be in a plain text or in HTML. You can send attachment too using email

Configuring the Gmail account

Before going on in this tutorial, you must have a Gmail account. You can use one you already have or create a new Gmail account as shown below:

create GMail account to use with ESP32

You can create your account using the username and password you prefer.

Less secure app

Once you have your email account, you have to allow less secure app to connect to Gmail. To do it use this link:

That’s all, you can send now email using your ESP32 through the Gmail account.

Implementing the ESP32 Email client

Let us implement the email client using the ESP32.

Installing the Email client library

To simplify the client development we will use an ESP32 email client library. The picture below shows how to do it in PlatformIO, but you can use the same client in Arduino IDE:

ESP32 Email client

You have to look for ESP32 Mail client. If you want to import the ZIP file you can use this link.

Gmail server parameters

Before coding, you need the following information to implement your email client:

  • SMTP server: smtp.gmail.com
  • SMTP server port: 465
  • SMTP sender address: full Gmail address
  • SMTP sender password: the pasword used when you have created your account

Send an email using ESP32: Source code

The code below shows how to send an email:

#include <Arduino.h> #include "ESP32_MailClient.h" #include <WiFi.h> const char *SSID = "wifi_ssid"; const char *PWD = "wifi_password"; #define GMAIL_SMTP_SEVER "smtp.gmail.com" #define GMAIL_SMTP_USERNAME "[email protected]" #define GMAIL_SMTP_PASSWORD "smtp_password" #define GMAIL_SMTP_PORT 465 // EMail Data SMTPData data; void connectToWiFi() { Serial.print("Connectiog to "); WiFi.begin(SSID, PWD); Serial.println(SSID); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.print("Connected - "); //Serial.println(WiFi.localIP); } String sendEmail(char *subject, char *sender, char *body, char *recipient, boolean htmlFormat) { data.setLogin(GMAIL_SMTP_SEVER, GMAIL_SMTP_PORT, GMAIL_SMTP_USERNAME, GMAIL_SMTP_PASSWORD); data.setSender(sender, GMAIL_SMTP_USERNAME); data.setSubject(subject); data.setMessage(body, htmlFormat); data.addRecipient(recipient); if (!MailClient.sendMail(data)) return MailClient.smtpErrorReason(); return ""; } void setup() { Serial.begin(9600); // Connect to WiFi connectToWiFi(); // Send plain email } void loop() { // put your main code here, to run repeatedly: }

How the code works

A brief code description:

  • Line 8-11 the code sets the SMTP configuration parameters as described previously
  • Line 14 defines the SMTPData that represents the email we are sending. We will set here the email parameters (recipient, email body and so on)
  • Line 31-40 the ESP32 code sends an email:
    • Line 32 sets the Gmail SMTP server parameters to the client can connect to it
    • Line 33 sets the email sender
    • Line 34 sets the email subject
    • Line 35 sets the email body and the email format (plain text or html body)
    • Line 36 sets the recipient
  • Line 37 sends the email

Send a plain text email using ESP32

If we want to send a simple text email, we have to use the following code:

// Send plain email String result = sendEmail("Test", "ESP32", "Hello World!", "[email protected]", false);

Using this example, the ESP32 sends an email to [email protected] with Hello World body. The result is shown below:

ESP32 Sends plain email usng SMTP server

Send an HTML body Email with ESP32

To send an HTML body email, use this code:

// Send HTML email char *body = "<b>Hello World!</b><br><p>This is an <i>HTML email</i> body"; String result = sendEmail("Test", "ESP32", body, "[email protected]", true);

Notice that the body uses HTML tags. The result shown in the recipient email is:

ESP32 email client to send HTML email

Recommended:
How to connect ESP32 to Amazon AWS IoT Core using AWS MQTT
Cloud IoT Core ESP32: Send data to Google Cloud Platform using MQTT
How to connect ESP32 to the smartphone

ESP32 mail client with Temperature and Humidity body

In this last part of this guide, we will discover how to send temperature and humidity via email using ESP32 and DHT11.

Schematic ESP32 and DHT11 connections

The picture below shows how to connect ESP32 to DHT11:

ESP32 with DHT11 to get temperature and humidity

The DHT11 signal pin is connected to G04 ESP32 pin.

Code: Sending DHT11 sensor readings via email

Before sending the email with sensor readings body, we have to get it:

  • Import Adafruit DHT11 library
  • Add the following code
#include <Adafruit_Sensor.h> #include <DHT.h> #define DHT_PIN 4 #define DHTTYPE DHT11 // DHT Sensor DHT dht(DHT_PIN, DHTTYPE); void setup() { Serial.begin(9600); // Init sensor dht.begin(); ... } void loop() { char body[2048]; float temp = dht.readTemperature(); float hum = dht.readHumidity(); Serial.println("Temperature"); Serial.println(temp); sprintf(body, "<h2>Sensor readings</h2><br/><p><b>Temperature (°C)</b>: <font color='red'>%.2f</font></p><br/><p><b>Humidity (%)</b>: <font color='blue'>%.2f</font></p>", temp, hum); // Sending the email String result = sendEmail("Sensor data", "ESP32", body, "[email protected]", true); delay(600000); }

A brief description:

  • Line 6 initializes the DHT11 sensor
  • Line 15 gets the current temperature
  • Line 16 gets the current humidity
  • Line 19 builds the HTML email body

Test the ESP32 email client

If you upload the code before in your ESP32 and check your email, the result is shown below:

Send email using ESP32 with temperature and humidty via Gmail

As a result, the ESP32 sends an email containing an HTML body with the temperature and the humidity retrieved via DHT11.

Wrapping up..

At the end of this post you learned how to send email using ESP32 with plain text body or HTML body. Moreover, you learned how to send the temperature and humidity via Gmail using ESP32.