How to send Emails to Gmail using an ESP32 PLC

Tips for Successful Email Sending with ESP32 PLC and EMailSender
May 11, 2023 by
How to send Emails to Gmail using an ESP32 PLC
Boot & Work Corp. S.L., Joan Vilardaga Castro
How to send Emails to Gmail using an ESP32 PLC


The ESP32 industrial PLC is an open source PLC based on ESP32, and has multiple applications in the industrial environment for industrial automation and control solutions.

In this guide, you will find a step-by-step process for sending emails using an ESP32-based PLC using the EMailSender library, which could be useful in various scenarios, such as configuring alarms or enabling monitoring functionalities.



An Ethernet cable, if Ethernet will be used

Install the EMailSender library

The EMailSender library enables any Arduino compatible board to send emails with attachments with the SMTP protocol. The Simple Mail Transfer Protocol is a widely used communication protocol for sending and receiving email messages over a network. SMTP operates on the application layer of the TCP/IP protocol stack and relies on TCP to establish the connection between mail servers and encrypt the communication.

It can be installed with the Arduino IDE's library manager, searching for the library you want to install, or it can be installed manually following these instructions. The recommended way is to install it through the library manager:

1. Open the library manager (Tools > Manage Libraries...):

Arduino IDE Manager Library

2. Search for the library you want to install (e.g. EMailSender):

EMailSender in Library Manager

Previous configuration to send emails

If you intend to secure the connection between the PLC and the SMTP server, you will also need to install the SSLClient library, which will encrypt the TCP traffic between you and the SMTP server. Even if you use Gmail as the SMTP server, Google forces the connections to be encrypted, so you will also need the library. This library can also be installed with the Library Manager, following the same steps for installing the EMailSender library.

After the installation, if you want to enable SSL in EMailSender, you will need to follow multiple steps:

  1. Change the line 469 in the SSLClient.h file from "unsigned char m_iobuf[2048];" to "unsigned char m_iobuf[BR_SSL_BUFSIZE_BIDI];". You can find this file in your Arduino libraries directory, inside the SSLClient/src folder.

  2. Put "define SSLCLIENT_WRAPPER " in the EMailSenderKey.h right after "#define EMailSenderKey_h ".

Plus, if you want to use Ethernet with ESP32 PLC, you will have to put "#define EMAIL_NETWORK_TYPE NETWORK_ETHERNET " right after "#define EMailSenderKey_h ".


 * Modified simple esp32 Gmail send example

#include "Arduino.h"
#include <EMailSender.h>

#define SENDER_EMAIL "[email protected]"
#define RECEIVER_EMAIL "[email protected]"
//#define USE_ETH EMailSender emailSend(SENDER_EMAIL, SENDER_PASSWORD); #ifndef USE_ETH #include <WiFi.h> const char* ssid = "<YOUR-SSID>"; const char* password = "<YOUR-PASSWD>"; uint8_t connection_state = 0; uint16_t reconnect_interval = 10000; uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr) { static uint16_t attempt = 0; Serial.print("Connecting to "); if(nSSID) { WiFi.begin(nSSID, nPassword); Serial.println(nSSID); } uint8_t i = 0; while(WiFi.status()!= WL_CONNECTED && i++ < 50) { delay(200); Serial.print("."); } ++attempt; Serial.println(""); if(i == 51) { Serial.print("Connection: TIMEOUT on attempt: "); Serial.println(attempt); if(attempt % 2 == 0) Serial.println("Check if access point available or SSID and Password\r\n"); return false; } Serial.println("Connection: ESTABLISHED"); Serial.print("Got IP address: "); Serial.println(WiFi.localIP()); return true; } void Awaits() { uint32_t ts = millis(); while(!connection_state) { delay(50); if(millis() > (ts + reconnect_interval) && !connection_state) { connection_state = WiFiConnect(); ts = millis(); } } } #endif void setup() { Serial.begin(115200); #ifdef USE_ETH uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE }; Ethernet.begin(mac); #else connection_state = WiFiConnect(ssid, password); if(!connection_state) // if not connected to WIFI Awaits(); // constantly trying to connect #endif EMailSender::EMailMessage message; message.subject = "Hello"; message.message = "Come to"; EMailSender::Response resp = emailSend.send(RECEIVER_EMAIL, message); Serial.println("Sending status: "); Serial.println(resp.status); Serial.println(resp.code); Serial.println(resp.desc); } void loop() {}


If you want to use Gmail as your SMTP server, you may need to enable Less Secure Applications on the Google account you want to use to send email.

If this does not work, you can visit this website to learn more.

​Search in our Blog

How to send Emails to Gmail using an ESP32 PLC
Boot & Work Corp. S.L., Joan Vilardaga Castro May 11, 2023
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.

Industrial PLC comparison >>>