Rohan Barnwal
Published

ESP32 Automated Email Sender

Build a seamless email-sending system using the ESP32 microcontroller.

BeginnerFull instructions provided1 hour310
ESP32 Automated Email Sender

Things used in this project

Hardware components

Espressif ESP32 Development Board - Developer Edition
Espressif ESP32 Development Board - Developer Edition
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code

Arduino
#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.");
}
}

Credits

Rohan Barnwal
22 projects • 30 followers
Rohan Barnwal - maker, hacker, tech enthusiast. I explore new tech & find innovative solutions. See my projects on hackster.io!

Comments