ESP32 E-Mail notifier for your mailbox

Table of Contents

Nowadays less amount of people uses the conventional mail system to send letters. If anybody sends any letter to a person may the letter will not reach to the person because now in generally doesn’t check the mailbox or if anyone wants to get the letters in perfect time he/she has to check the mailbox every day. So to resolve this problem we designed a system that is integrated with IoT.

I made an ESP32 E-Mail notifier for your mailbox that detects if there is any mail/letter dropped into the mailbox and sends Email notifications.

To make this project we will require these parts:

From Amazon India

  1. ESP32
  2. Infrared Sensor

From Ali Express

  1. ESP32
  2. Infrared Sensor

You can also check out my other IoT tutorials here

ESP32 Email notifier Circuit Diagram

ESP32 Email notifier Circuit Diagram
ESP32 Email notifier Circuit Diagram

ESP-Mail-Client Library

Before proceeding further in this tutorial, you will have to install the ESP-Mail-Client library. You will also find the library in Arduino library manager.

Allow less secure apps:

Allow less secure apps to get access to the Gmail account, from where you want to send your emails. You can open this link to go to that menu.

Allow less secure apps
Allow less secure apps

Gmail SMTP Server Settings

If you’re using a Gmail account, these are the SMTP Server details:

  • SMTP Server:
  • SMTP username: Complete Gmail address
  • SMTP password: Your Gmail password
  • SMTP port (TLS): 587
  • SMTP port (SSL): 465
  • SMTP TLS/SSL required: yes

ESP32 Email notifier Code

The Following Code is for the ESP32 Email notifier. It sends an Email whenever mail arrives in the mailbox.

Don’t upload the code yet. In the code, you have to change a few things. First, you have to change the SSID and password according to your router credentials. Then you have to change the SMTP host address. As I am using Gmail for this project son in my case the SMTP host address is “”. Also, you have to change the SMTP port address, for Gmail it is 465.

Next, the Email address & password should be written in the code from where the notification mail will be sent. In this code, you can write the subject and body of the mail as you wish. The last change is the recipient’s mail address. The recipient will get an email notification if there is any mail dropped in the mailbox.

#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP_Mail_Client.h>

#define WIFI_SSID "SSID"
#define WIFI_PASSWORD "*****"

#define sig 15
int flag = 0;
#define SMTP_HOST ""

#define SMTP_PORT esp_mail_smtp_port_465 //gmail

/* The log in credentials */
#define AUTHOR_EMAIL ""
#define AUTHOR_PASSWORD "############"

/* The SMTP Session object used for Email sending */
SMTPSession smtp;

#define ON_Board_LED 2

/* Callback function to get the Email sending status */
void smtpCallback(SMTP_Status status);

const char rootCACert[] PROGMEM = "-----BEGIN CERTIFICATE-----\n"
                                  "-----END CERTIFICATE-----\n";

ESP_Mail_Session session;
SMTP_Message message;
void setup()

#if defined(ARDUINO_ARCH_SAMD)
  while (!Serial)


  digitalWrite(ON_Board_LED, HIGH);


  Serial.print("Connecting to AP");

  while (WiFi.status() != WL_CONNECTED)

  digitalWrite(ON_Board_LED, HIGH);

  Serial.println("WiFi connected.");
  Serial.println("IP address: ");


  /* Set the callback function to get the sending results */

  /* Declare the session config data */
  //ESP_Mail_Session session;


  /* Set the session config */
  session.server.host_name = SMTP_HOST;
  session.server.port = SMTP_PORT; = AUTHOR_EMAIL;
  session.login.password = AUTHOR_PASSWORD;
  //session.login.user_domain = ""; = "Smart Mail Box"; = AUTHOR_EMAIL;
  message.subject = "Hi circuiTician, you have recived a mail";
  message.addRecipient("Someone", "");

  String textMsg = "you have recived a mail, content not verified";
  message.text.content = textMsg.c_str();

  message.text.charSet = "us-ascii";

  message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;

  message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_low;

void loop()
  if (digitalRead(sig) == 0 && flag ==0){
    if (!smtp.connect(&session))

  /* Start sending Email and close the session */
  if (!MailClient.sendMail(&smtp, &message))
    Serial.println("Error sending Email, " + smtp.errorReason());

  //to clear sending result log

  ESP_MAIL_PRINTF("Free Heap: %d\n", MailClient.getFreeHeap());
  flag = 1;
  else if(flag == 1 && digitalRead(sig) == 1){
    flag = 0;

Serial.print("flag: "); Serial.println(flag);

/* Callback function to get the Email sending status */
void smtpCallback(SMTP_Status status)
  /* Print the current status */

  /* Print the sending result */
  if (status.success())
    ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount());
    ESP_MAIL_PRINTF("Message sent failled: %d\n", status.failedCount());
    struct tm dt;

    for (size_t i = 0; i < smtp.sendingResult.size(); i++)
      /* Get the result item */
      SMTP_Result result = smtp.sendingResult.getItem(i);
      time_t ts = (time_t)result.timestamp;
      localtime_r(&ts, &dt);

      ESP_MAIL_PRINTF("Message No: %d\n", i + 1);
      ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed");
      ESP_MAIL_PRINTF("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
      ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients);
      ESP_MAIL_PRINTF("Subject: %s\n", result.subject);



Also, check my Github Repo here. Now upload the code and your smart mailbox is ready.

This ESP32 E-mail notifier is super helpful if you do not check your mailbox regularly.

Check the full video here.

Like it !! Share it !!

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on pinterest

Related Articles