Introduction: ESP32 AUTOMATED EMAIL SENDER

ESP32 Automated Email Sender: Effortless Communication for Your Projects

In the world of IoT, real time communication is vital. Whether you want to receive alerts from sensors, get notified about certain events, or just automate regular updates an email sending mechanism can be incredibly useful. This project demonstrates how to use the ESP32 microcontroller to send emails automatically based on specific triggers. The system is easy to set up, highly customisable, and can be integrated into various IoT applications.

Supplies

  1. ESP32 Development board: The brain of the project that handles the Wi-Fi connectivity and email sending.
  2. Micro USB Cable: for programming the ESP32 and providing power
  3. Wi-Fi Network: To enable the ESP32 to connect to the internet and send emails.
  4. Arduino IDE: For coding and uploading the firmware to the ESP32

Step 1: Setting Up the Environment

  1. Install Arduino IDE: Download and install the Arduino IDE from the official site
  2. Install ESP32 Board Support
  3. Open Arduino IDE, go to FILE>PREFERENCES.
  4. In the “ADDITIONAL BOARD MANAGER URLs” field paste this url: https://dl.espressif.com/dl/package_esp32_index.json.
  5. Go to TOOLS>BOARD>BOARD MANAGER, search for ESP32 and install the latest version
  6. Install Required Libraries:
  7. In the Arduino IDE, go to SKETCH > INCLUDE LIBRARY > MANAGE LIBRARIES. Search for and install the following libraries: ESP Mail Client Wi-Fi (should be per installed with ESP32)

Step 2: Code Explanation

#include <Arduino.h>
#include <WiFi.h>
#include <ESP_Mail_Client.h>
#define WIFI_SSID "your_SSID"
#define WIFI_PASSWORD "your_PASSWORD"
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
#define AUTHOR_EMAIL "your_email@gmail.com"
#define AUTHOR_PASSWORD "your_app_password"
#define RECIPIENT_EMAIL "recipient_email@gmail.com"
SMTPSession smtp;
void smtpCallback(SMTP_Status status);
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
smtp.debug(1);
smtp.callback(smtpCallback);
Session_Config config;
config.server.host_name = SMTP_HOST;
config.server.port = SMTP_PORT;
config.login.email = AUTHOR_EMAIL;
config.login.password = AUTHOR_PASSWORD;
config.time.ntp_server = F("pool.ntp.org,time.nist.gov");
config.time.gmt_offset = 3;
config.time.day_light_offset = 0;
SMTP_Message message;
message.sender.name = F("ESP32");
message.sender.email = AUTHOR_EMAIL;
message.subject = F("ESP32 Test Email");
message.addRecipient(F("Recipient"), RECIPIENT_EMAIL);
message.text.content = "Hello World! - Sent from ESP32";
message.text.charSet = "us-ascii";
message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
if (!smtp.connect(&config)) {
Serial.printf("Connection error: %d, %s\n", smtp.statusCode(), smtp.errorReason().c_str());
return;
}
if (!MailClient.sendMail(&smtp, &message))
Serial.printf("Error: %d, %s\n", smtp.statusCode(), smtp.errorReason().c_str());
}
void loop() {}
void smtpCallback(SMTP_Status status) {
Serial.println(status.info());
if (status.success()) {
Serial.println("Email sent successfully.");
smtp.sendingResult.clear();
} else {
Serial.println("Failed to send email.");
}
}

How It Works:

  1. Wi-Fi Connection: The ESP32 connects to the Wi-Fi network using the provided SSID and password.
  2. SMTP Server Connection: The ESP32 connects to the SMTP server using the specified host and port.
  3. Email Composition: An email is composed with a subject, sender, recipient and text content.
  4. Email Sending: The email is sent using the “MailClient.sendMail()” function.
  5. Status Callback: The status of the email sending process is displayed in the serial monitor.

Step 3: Testing and Debugging


  1. Check Serial Monitor: Open the Serial Monitor in Arduino IDE to view the connection status, email sending status, and any potential errors.
  2. Verify Wi-Fi Credentials: ensure that the SSID and password are correct for the Wi-Fi network.
  3. Check SMTP Credentials: ensure that the email and app password are correct for the SMTP

Potential Use Cases:

  1. Security Systems: Automatically send an email when a sensor is triggered.
  2. Remote Monitoring: Send periodic updates or alerts from the remote sensors.
  3. Notification Systems: Alerts users when certain threshold are met (e.g. Temperature, Humidity etc.).




Step 4: Video Tutorial

Conclusion

This project provides a straightforward way to add functionality to your IoT projects using the ESP32. Whether you need to sends alerts, notifications, or just want to experiment with email automation, this guide covers the essential steps to get you started. With a few modifications, you can customise the system to fit your specific needs.If you loved it don’t forget to like the project and video and also follow me on huckster.io and also subscribe me on YouTube meet you soon with another project.