Arduino - Send Email

In this tutorial, we are going to learn how to use Arduino to send the email.

There are several ways to send email from Arduino:

This tutorial uses IFTTT to send the email. We also learn how to include the sensor's data in the email. The code is available for Arduino Ethernet Shield. You can easily adapt the code for any other WiFi/Ethernet Shield with a small modification.

The tutorial provides the Arduino code for two cases:

Prerequisite

If you do not know about IFTTT and related concepts, We highly recommend you reading the following tutorials:

Wiring Diagram

We just need to stack the shield on Arduino Uno/Mega

Arduino - Ethernet Shield

How It Works

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

When we create the Applet on the IFTTT website:

  • We can input the receiver's email address.
  • We can type the subject and the content of the email to be sent.
  • We can specify the data variables that IFTTT will extract from Arduino's request to include in the email content.

※ NOTE THAT:

The email content is specified on the Applet, not in the Arduino code.

Workflow

After creating the Applet:

  • Arduino makes an HTTP request to Webhooks of IFTTT,
  • The Webhooks triggers the email service on IFTTT
  • The email service on IFTTT sends an email to the email address you specified.
  • If we specify the data variables, IFTTT will extract the variable's value and include it in the email content

What we need to do

We need to do to main tasks:

  • Create an Applet on the IFTTT website
  • Write Arduino code that makes an HTTP request to the Applet

How To Create an Applet between Webhooks and Email service

Arduino create IFTTT applet
  • Click the Add button
Arduino adds IFTTT applet
  • Search for Webhooks, and then click the Webhooks icon
Arduino IFTTT Webhooks
  • Click the Receive a web request icon
Arduino IFTTT Webhooks Service
  • Click the Connect button
Arduino
  • Type an event name. You can give any name you want and memorize it to use later. The event name is a part of URL that Arduino will make request to. In this tutorial, we use a name send-email. And then click Create trigger button
Arduino Webhooks create trigger
  • Click the Add button to add the action
Arduino IFTTT create action
  • Search for email, and then click the Email icon
Arduino  Email service
  • Click the Send me an email icon
Arduino send me an email
  • Click the Connnect button
Arduino Connects to email
  • Input your email address and then click the Semd PIN button
Arduino
  • You will receive an email, get the PIN from email and input to Web UI to do verification
  • After that the email subject and content editor appears
Arduino email subject and content editor
  • You can give any subject and content for the email. The below are example
Arduino
  • Subject:
Temperature from Arduino
  • Content:
Outside Temperature: {{Value1}}°C
Measured Time: {{OccurredAt}}


Take care of yourself!
Arduino Uno
  • Click the Create Action button
  • Click the Continue button
Arduino
  • Click the Finish button
Arduino

Now you succeeded to create an IFTTT Applets for your Arduino. The next step is to get the IFTTT Webhooks key, which is used to authenticate and identify your Arduino.

Arduino IFTTT Webhooks documentation
  • You will see the Webhooks key as below
Arduino IFTTT webhooks key
  • Copy and wire down your Webhooks to use on Arduino code.

Now, We just need to write Arduino code that makes an HTTP request to the below URL:

https://maker.ifttt.com/trigger/send-email/with/key/XXXXXXXXXXXXXXXXXXXXX?value1=27

※ NOTE THAT:

  • In the URL, the send-email is the event name we used:
    • If you already used this name for another Applet, you can give it any other name.
    • If you use another name, please replace the send-email by your event name
  • Webhooks key is generated by IFTTT. Please keep it secret. You can change it by regenerating it on the IFTTT website.

Before writing Arduino code, We can test the IFTTT Applet by doing:

  • Open a web browser
  • Copy the below link:
https://maker.ifttt.com/trigger/send-email/with/key/XXXXXXXXXXXXXXXXXXXXX?value1=27
  • Paste it on the address bar of the web browser
  • Replace XXXXXXXXXXXXXXXXXXXXX with your Webhooks key.
  • Press the enter key on your keyboard. You will see the browser display the message as below image.
Arduino IFTTT applet test
  • And you will receive an email.

Arduino code

We can optionally include the sensor data in the Email content. The below code includes a supposed temperature value

Arduino IFTTT code for Arduino Uno R4 WiFi

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-send-email */ #include <WiFiS3.h> const char ssid[] = "YOUR_WIFI_SSID"; // change your network SSID (name) const char pass[] = "YOUR_WIFI_PASSWORD"; // change your network password (use for WPA, or use as key for WEP) WiFiClient client; int status = WL_IDLE_STATUS; int HTTP_PORT = 80; String HTTP_METHOD = "GET"; char HOST_NAME[] = "maker.ifttt.com"; String PATH_NAME = "/trigger/send-email/with/key/XXXXXXXXXXXXXXXXXXXXX"; // change your Webhooks key String queryString = "?value1=27"; // the supposed temperature value is 27°C void setup() { Serial.begin(9600); // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); // don't continue while (true) ; } String fv = WiFi.firmwareVersion(); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println("Please upgrade the firmware"); } // attempt to connect to WiFi network: while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); } // print your board's IP address: Serial.print("IP Address: "); Serial.println(WiFi.localIP()); // connect to web server on port 80: if (client.connect(HOST_NAME, HTTP_PORT)) { // if connected: Serial.println("Connected to server"); // make a HTTP request: // send HTTP header client.println("GET " + PATH_NAME + queryString + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header while (client.connected()) { if (client.available()) { // read an incoming byte from the server and print it to serial monitor: char c = client.read(); Serial.print(c); } } // the server's disconnected, stop the client: client.stop(); Serial.println(); Serial.println("disconnected"); } else { // if not connected: Serial.println("connection failed"); } } void loop() { }

Arduino IFTTT code for Arduino Uno/Mega with Ethernet Shield

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-send-email */ #include <SPI.h> #include <Ethernet.h> // replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; EthernetClient client; int HTTP_PORT = 80; String HTTP_METHOD = "GET"; char HOST_NAME[] = "maker.ifttt.com"; String PATH_NAME = "/trigger/send-email/with/key/XXXXXXXXXXXXXXXXXXXXX"; // change your Webhooks key String queryString = "?value1=27"; // the supposed temperature value is 27°C void setup() { Serial.begin(9600); // initialize the Ethernet shield using DHCP: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to obtaining an IP address using DHCP"); while (true); } // connect to web server on port 80: if (client.connect(HOST_NAME, HTTP_PORT)) { // if connected: Serial.println("Connected to server"); // make a HTTP request: // send HTTP header client.println("GET " + PATH_NAME + queryString + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header while (client.connected()) { if (client.available()) { // read an incoming byte from the server and print it to serial monitor: char c = client.read(); Serial.print(c); } } // the server's disconnected, stop the client: client.stop(); Serial.println(); Serial.println("disconnected"); } else {// if not connected: Serial.println("connection failed"); } } void loop() { }

※ NOTE THAT:

This code used the dynamic IP address (via DHCP). If you want to use the static IP address, See Arduino Ethernet Shield with static IP address

Quick Steps

  • If using the Ethernet Shield, stack Ethernet Shield on Arduino Uno or Mega
  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Copy the above code and open with Arduino IDE
  • Update your Webhooks key in the Arduino code
  • Click Upload button on Arduino IDE to upload code to Arduino
Arduino IDE Upload Code
  • 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: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ETag: W/"2c-4f52ca3d" Server: web_server Congratulations! You've fired the send-email event disconnected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • You receives an email as below
Arduino Gmail receive

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 ...

The Best Arduino Starter Kit

※ OUR MESSAGES