Introduction: ESP8266 GMail Sender
Hello and welcome to my first Instructable.
I will show you how to send emails from any ESP8266 wifi module using Gmail server.
This instructable relies on Arduino core for ESP8266 WiFi chip,
which makes a self-contained microcontroller from it (no need of AT commands and master devices).
You can connect sensors and get notified by email about changes.
2018 Update:
Here is newer code written as arduino lib. It supports multiple recipients.
Also no need to encode login and password to base64 now it uses ESP core base64 lib. github
2019 Update:
- This code doesn't work with ESP8266 core for Arduino version 2.5.0!
- Temporary solution use core version 2.4.2
Before we begin
Required hardware:
- Any ESP8266 (I'm using ESP8266-07 ebay link).
- In my case USB UART Board(I'm using FT232RL FTDI Serials Adapter Module ebay). Not needed if your board has usb port.
- Some jumper cables.
- WIFI router of course.
List may be incomplete.
Required software:
- Arduino Software
- Arduino core for ESP8266 WiFi chip
- Sketch with project and test code (ESP8266_Gmail_Sender.zip).
Attachments
Step 1: Gmail Account Setup
We are going to use SMTP to send messages.
Using SMTP Authentication we provide only email and password,
by default Google uses more complex verification methods so we need to change settings.
Go to your Google account settings and enable "Allow less secure apps" at the bottom of the page.
This mean apps only need your email and password when login to your gmail account.
If you concerned about security, just use different account.
Step 2: Edit Sketch
I wrote a little sketch which send one test message to check if all works as should.
When all software downloaded and installed:
- Unzip ESP8266_Gmail_Sender.zip
- Find and open ESP8266_Gmail_Sender.ino
- Set your wifi access point name (SSID) and password. Should be like this:
const char* ssid = "MyWiFi"; const char* password = "12345678";
- In setup() function find
if(gsender->Subject(subject)->Send("boris.on@live.com", "Setup test"))
First parameter of Send() function is recipient email, second message text.
Change recipient from boris.on@live.com to your email which will receive a message.
Subject function is optional! Subject sets once and stored until you change it.
You can send mails without subject or if it already set
gsender->Send(to, message);
- Now open Gsender.h tab
- We need Base64 encoded email address and password of gmail account which will be used to send emails.
You can use base64encode.org for encoding, result must be something like:
const char* EMAILBASE64_LOGIN = "Y29zbWkxMTExMUBnbWFpbC5jb20="; const char* EMAILBASE64_PASSWORD = "TGFzZGFzZDEyMzI=";
- Now set FROM field.
const char* FROM = "your_email@gmail.com";
That`s all for this part.
Step 3: Code Uploading and Testing
Save changes. Don`t forget to set your board in Tool menu.
Upload sketch to your ESP8266 board.
Open Serial monitor, board will print log messages.
That`s all I hope you will receive "Message send.". Thanks...
211 Comments
Question 1 year ago on Step 2
Google will not allow "Less secure app access" after May 30, 2022. How do we resolve the issue after that date?
Answer 1 year ago
I found an answer to this. Follow this link: ESP8266 NodeMCU Send Emails SMTP Server: HTML, Text, Attachments (Arduino) | Random Nerd Tutorials. Go to the step "Create an App Password." Use this App Password in place of your account login password. Then everything will work as it should be.
Reply 11 months ago
Hi please I kept my user login in BASE64 and then added the app password : not working . I coded the app password in base64 not working ... coudl you kindly confirm what to use for user and what for password ? thank you
1 year ago
Still having problems as follows:
When running Serial Monitor shows following:
Connection: ESTABLISHED
Got IP address: 192.168.1.16
Error sending message: Could not connect to mail server
I've tried all of the previous recommendations and have just tried turning off '2-Step Verification' but that didn't overcome the error.
I'm using ESP8266 - LoLin NodeMcu V3 and Arduino IDE 1.8.13
Has anyone had success recently sending emails from Gmail to Gmail (using same email address)
Regards all
Reply 1 year ago
SUCCESS - I had to enable in Google Gmail "Less Secure Apps" to be ON
Regards all
Question 2 years ago
I have it working on a Wemos D1 mini Pro. It will send an email successfuly when connected to my home wiFi. Then I take the board to a commercial wifi and it will not work. I also tried to use my cellphone hotspot which I can connect my laptop to but the WeMos D1 will not send email
Suggestions?
Answer 1 year ago
Hi! Kind of late, but in case you still need a solution:
WiFi.setPhyMode (WIFI_PHY_MODE_11G); // By default it is set to WIFI_PHY_MODE_11B
That line made it for me. I could connect at home, but not at work... Hope it helps!
By the way, here's all the code (there are a couple more things i configured, like ESP.eraseConfig() and setting the sleep mode (to no sleep):
Serial.begin(115200);
Serial.setDebugOutput(true);
ESP.eraseConfig();
WiFi.disconnect(); // remove old credentials
WiFi.mode(WIFI_STA); // set station mode
WiFi.setSleepMode (WIFI_NONE_SLEEP, 0); // disable sleep
//WiFi.setOutputPower(17.5); // Can go all the way up to 20.5
WiFi.setPhyMode (WIFI_PHY_MODE_11G);
WiFi.scanNetworksAsync(prinScanResult); // display list of available networks (just to check we can actually see the network we are about to connect)
Serial.print("Connecting to ");
Serial.println(STASSID);
WiFi.begin(STASSID, STAPSK); // connect
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(WiFi.status());
Serial.println(WiFi.localIP());
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
2 years ago
A few years later. Does this still work for you?
I get..
Connection: ESTABLISHED
Got IP address: 10.0.0.56
Error sending message: Could not connect to mail server
435
Reply 2 years ago
Same here :(
but i will try what pavs99 recommended
4 years ago on Step 1
To make this work with esp8266 core 2.5.0 you need to do add a line.
in Gsender.cpp at line 53 and 54 it looks like this:
WiFiClientSecure client;
#if defined(GS_SERIAL_LOG_2)
You need to change it to:
WiFiClientSecure client;
client.setInsecure();
#if defined(GS_SERIAL_LOG_2)
This is because of breaking changes after implementing BearSSL API for all SSL/TLS operations.
(https://github.com/esp8266/Arduino/releases/tag/2.5.0)
By using setInsecure() no check are done of the validity certificates This is the same behavior as with axTLS library that was used before.
More about the new BearSSL api for esp8266:
https://arduino-esp8266.readthedocs.io/en/latest/e...
Someone could update this project to support full ssl validation.
Reply 2 years ago
Thanks bro it worked
Reply 2 years ago
thanks it worked ok with this modification
Reply 3 years ago
Had to use this with the new code https://github.com/CosmicBoris/ESP8266SMTP .
Reply 3 years ago
thank you working well after i add this function .
Reply 3 years ago
wow thank you, i tried many things to get it working and you bring the Fix:D
Reply 3 years ago
Thanks! Now It's working!
2 years ago
Used a D1-Mini ESP8266. the very first thing i did was to remove the email address in the code so someone else wouldnt get a ton of test emails. I am using the gmail to send a text. That is the easy coding. Had some trouble getting the .cpp's and .h's but if you ZIP them then they will go into the IDE. (kind of a pain, but i got it) - then to locate the correct cpp and h files to change the login and password. I already had the password from gmail for such applications. It is different than your normal password - you have to get that from gmail
-- I am making this to send a message from my washer/dryer when they stop. i hope to use an accelerometer as it should be the least intrusive. The dryer will be first.
--thanks for getting me through what i felt was the harder part of this research :-)
Reply 2 years ago
Which .h and .cpp files are you using?
Also, are you saying that I have to modify those files to get this to work? I did not see that in the write-up of this app by Boris.
I have tried using the 3-13-2018 version of his github but keep getting an error message that it could not connect to mail server even though I entered the correct username and password for my gmail account.
Thanks.
Question 2 years ago
Any help would be appreciated, I can connect to wifi but I can't get passed the SMTP auth error.
I have gmail "less secure for apps" on and cleared captcha.
I tried turning on 2 step auth and giving the esp the unique password but that did not work either. Suggestions?
Connection: ESTABLISHED
Got IP address: 192.168.0.18
220 smtp.gmail.com ESMTP w2sm3873261pgb.43 - gsmtp
250 smtp.gmail.com at your service
334 VXNlcm5hbWU6
501 5.5.2 Cannot Decode response w2sm3873261pgb.43 - gsmtp
502 5.5.1 Unrecognized command. w2sm3873261pgb.43 - gsmtp
Error sending message: SMTP AUTH error
Answer 2 years ago
Ahh... fixed by reading the instructions completely.... base64...