top of page
Writer's pictureCoding Phoenix

Sending Emails via ESP32 Using an Email Gateway: A Step-by-Step Guide

Introduction:

The ESP32 microcontroller, equipped with Wi-Fi capabilities, is a powerful tool for IoT projects. In this tutorial, we'll learn how to send emails using an ESP32 through an email gateway service. This method allows you to integrate email functionality into your ESP32 projects, making them versatile for sending notifications, alerts, or reports via email.

Materials You'll Need:

  1. ESP32 development board (e.g., ESP-WROOM-32)

  2. USB cable for power and programming

  3. Computer with the Arduino IDE installed

Step 1: Set Up the Arduino IDE for ESP32

If you haven't already, download and install the Arduino IDE from the official Arduino website (https://www.arduino.cc/en/software).

Next, set up the Arduino IDE for ESP32 development:

  • Open the Arduino IDE.

  • Go to "File" > "Preferences."

  • In the "Additional Boards Manager URLs" field, add the following URL:arduinoCopy code https://dl.espressif.com/dl/package_esp32_index.json

  • Click "OK" to close the Preferences window.

Step 2: Install the ESP32 Board Package

  • Go to "Tools" > "Board" > "Boards Manager..."

  • In the Boards Manager, type "esp32" in the search bar.

  • Click "Install" on the "esp32" by Espressif Systems package.

Step 3: Get Email Gateway Credentials

To send emails via an email gateway, you'll need the SMTP (Simple Mail Transfer Protocol) server details and authentication credentials (username and password) for your email provider. Most email service providers, such as Gmail or Outlook, offer SMTP settings. Ensure you have these details ready.

Step 4: Write the ESP32 Email Sending Sketch

Here's an example Arduino sketch to send an email using the ESP32 through an email gateway. In this example, we'll use Gmail as the email provider. Replace "YourSSID", "YourPassword", "YourEmail@gmail.com", "RecipientEmail@example.com", "smtp.gmail.com", "587", "YourEmail@gmail.com", and "YourEmailPassword" with your specific information.



#include <WiFi.h>

#include <WiFiClientSecure.h>


const char* ssid = "YourSSID"; // Replace with your Wi-Fi SSID

const char* password = "YourPassword"; // Replace with your Wi-Fi password

const char* emailSender = "YourEmail@gmail.com";

const char* emailRecipient = "RecipientEmail@example.com";

const char* smtpServer = "smtp.gmail.com"; // SMTP server address

const int smtpPort = 587; // SMTP port (587 for TLS)

const char* smtpUser = "YourEmail@gmail.com";

const char* smtpPassword = "YourEmailPassword";


void setup() {

Serial.begin(115200);


// Connect to Wi-Fi

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);

Serial.println("Connecting to WiFi...");

}

Serial.println("Connected to WiFi");


// Send an email

sendEmail();

}


void loop() {

// Your code here

}


void sendEmail() {

WiFiClientSecure client;


if (client.connect(smtpServer, smtpPort)) {

Serial.println("Connected to SMTP server");

// SMTP handshake

if (readResponse(client, "220") == 0) {

// Sending EHLO

client.println("EHLO esp32");

if (readResponse(client, "250") == 0) {

// Authenticating

client.println("AUTH LOGIN");

if (readResponse(client, "334") == 0) {

// Sending username

client.println(base64::encode(smtpUser));

if (readResponse(client, "334") == 0) {

// Sending password

client.println(base64::encode(smtpPassword));

if (readResponse(client, "235") == 0) {

// Sending email content

client.println("MAIL FROM: <" + String(emailSender) + ">");

client.println("RCPT TO: <" + String(emailRecipient) + ">");

client.println("DATA");

client.println("Subject: ESP32 Email Test");

client.println("From: " + String(emailSender));

client.println("To: " + String(emailRecipient));

client.println("Content-Type: text/plain; charset=\"utf-8\"");

client.println("ESP32 Email Test Successful!");

client.println(".");

if (readResponse(client, "250") == 0) {

// Closing the connection

client.println("QUIT");

readResponse(client, "221");

}

}

}

}

}

}

client.stop();

Serial.println("Email sent successfully!");

}

else {

Serial.println("Failed to connect to SMTP server");

}

}


int readResponse(WiFiClientSecure& client, const char* expectedResponse) {

char buffer[256];

int timeout = 10000;

int len = 0;

int responseCode = -1;


memset(buffer, 0, sizeof(buffer));


while (timeout > 0) {

while (client.available()) {

char c = client.read();

buffer[len++] = c;

if (c == '\n' || len == sizeof(buffer) - 1) {

buffer[len] = '\0';

Serial.print(buffer);

if (strstr(buffer, expectedResponse) != NULL) {

responseCode = 0;

}

len = 0;

}

}

if (responseCode == 0) {

break;

}

delay(1);

timeout--;

}

return responseCode;

}


Step 5: Configure the Sketch

  • Replace "YourSSID" and "YourPassword" with your Wi-Fi network credentials.

  • Replace "YourEmail@gmail.com" and "RecipientEmail@example.com" with your sender and recipient email addresses.

  • Set "smtpServer" to the SMTP server address of your email provider.

  • Set "smtpPort" to the SMTP port (e.g., 587 for TLS) of your email provider.

  • Replace "YourEmail@gmail.com" and "YourEmailPassword" with your email address and password.

Step 6: Upload and Run the Sketch

  • Connect your ESP32 to your computer using a USB cable.

  • Ensure you've selected the correct board and port (as described in previous steps).

  • Click the "Upload" button (right arrow) in the Arduino IDE to compile and upload the sketch to your ESP32.

Step 7: Monitor Serial Output

Open the Serial Monitor (Tools > Serial Monitor) to monitor the ESP32's serial output. You should see the email sending process, including connection status and responses from the SMTP server.


Step 8: Check Your Email

After running the sketch, check the recipient email address for the email sent by your ESP32. You should receive an email with the subject "ESP32 Email Test" and the message "ESP32 Email Test Successful!"

Conclusion:

You've successfully learned how to send emails using an ESP32 through an email gateway


38 views0 comments

Recent Posts

See All

Comments


bottom of page