ESP32 - Send Email

In this tutorial we are going to learn how to use ESP32 to send email.

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

Prerequisite

If you do not have knowledge of IFTTT and related concepts, We highly recommend you reading the following tutorials:

How It Works

We need to create an IFTTT Applet on the IFTTT website, which connects Webhooks to Email service.

When we create the Applet:

  • We can compose the content of Email to be sent.
  • We can include data from ESP32 to the Email content.

※ NOTE THAT:

The Email content is specified on the Applet, not in the ESP32 code.

Workflow

  • ESP32 makes an HTTP request to Webhooks of IFTTT,
  • Webhooks triggers Email service on IFTTT
  • Email service on IFTTT sends an email to the email address you used to create the IFTTT account.

How To Use IFTTT with ESP32

  • Create an IFTTT account and Login to IFTTT.
  • Create an Applet of Webhooks and Email services. Please write down the event name we uses
  • After creating the applet, get Webhooks key from IFTTT.
  • Copy below ESP32 code and paste in Arduino IDE.
  • Replace event name and Webhooks key in the ESP32 code
  • Upload the code to ESP32

※ NOTE THAT:

  • The event name is given by you. You can give any name.
  • Webhooks key is created by IFTTT. You can not change it.

See the video tutorial at the end of this tutorial.

※ NOTE THAT:

Note that, you can create the IFTTT account by anyway. This tutorial creates the IFTTT account with the Google account. If you do not use Google account, the step-by-step instruction may be slightly different.

ESP32 code

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-send-email */ #include <WiFi.h> #include <HTTPClient.h> const char WIFI_SSID[] = "YOUR_WIFI_SSID"; const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; String HOST_NAME = String HOST_NAME = String HOST_NAME = String HOST_NAME = tring HOST_NAME = ring HOST_NAME = ing HOST_NAME = ng HOST_NAME = g HOST_NAME = HOST_NAME = HOST_NAME = OST_NAME = ST_NAME = T_NAME = _NAME = NAME = AME = ME = E = " = "h = "ht = "htt= "http "http://maker.ifttt.com"; String PATH_NAME = "/trigger/EVENT-NAME/with/key/YOUR-KEY"; // change your EVENT-NAME and YOUR-KEY String queryString = "?value1=26&value2=70"; void setup() { Serial.begin(9600); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.println("Connecting"); while(WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to WiFi network with IP Address: "); Serial.println(WiFi.localIP()); HTTPClient http; http.begin(HOST_NAME + PATH_NAME + queryString); //HTTP int httpCode = http.GET(); // httpCode will be negative on error if(httpCode > 0) { // file found at server if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } else { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] GET... code: %d\n", httpCode); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } void loop() { }

Result

In the two above codes, If succeeded,

  • The result in Serial Monitor looks like below:
  • COM6
    Send
    Connected to server HTTP/1.1 200 OK Date: Thu, 14 May 2020 07:06:40 GMT Content-Type: text/html; charset=utf-8 Content-Length: 44 Connection: close X-Top-SecreTTT: VG9vIGVhc3k/IElmIHlvdSBjYW4gcmVhZCB0a== ETag: W/"2c-4f52ca3d" Server: web_server Congratulations! You've fired the test event disconnected
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  
  • You receives an email
  • https://esp32io.com/images/tutorial/esp32-email-receive.jpg

Video Tutorial

How to Include Sensor Data to Email Content

To include data from ESP32 to Email content, we need to do:

  • In ESP32: Include data into HTTP request (in the query string).
  • In Applet: modify the setting to extract data from the HTTP request.

You can learn what is query string in this tutorial

There are 5 variables we can use in the setting of the IFTTT Applet.

{{EventName}} Extracted from HTTP request
{{OccurredAt}} System time when event occurs
{{Value1}} Extracted from the variable value1 of query string in HTTP request
{{Value2}} Extracted from the variable value2 of query string in HTTP request
{{Value3}} Extracted from the variable value3 of query string in HTTP request

For example:

  • In ESP32: Change String queryString = "" to String queryString = "?value1=26&value2=70" in above codes.
  • In Applet: Modify the email content in the setting of the IFTTT Applet as follow:

Hi guy,


This is ESP32, I would like to inform you that:

- The outside temperature is {{Value1}} °C

- The outside humidity is {{Value2}} %


All the above value is measured at {{OccurredAt}}


Good luck and stay healthy!

In the above message, "
" make new line when displaying.

The email will be like below:

https://esp32io.com/images/tutorial/esp32-email-temperature-humidity.jpg

You can read the temperature and humidity from sensor and put in the query string. See how to include data from sensor to query string

Challenge Yourself

Based on the above code, make the following project:

  • Send an email notification when the button is pressed
  • Send an email notification when the door is open
  • Send an email notification when the temperature is too hot ...

See Also

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere