ESP8266 GMail Sender

132,172

650

169

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:

  1. Any ESP8266 (I'm using ESP8266-07 ebay link).
  2. In my case USB UART Board(I'm using FT232RL FTDI Serials Adapter Module ebay). Not needed if your board has usb port.
  3. Some jumper cables.
  4. WIFI router of course.

List may be incomplete.

Required software:

  1. Arduino Software
  2. Arduino core for ESP8266 WiFi chip
  3. Sketch with project and test code (ESP8266_Gmail_Sender.zip).

Supplies:

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.


I'm receiving many emails every day because some of You guys not attentive, PLEASE DON'T FORGET TO CHANGE RECIPIENT EMAIL!


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...

13 People Made This Project!

Recommendations

  • Fandom Contest

    Fandom Contest
  • Party Challenge

    Party Challenge
  • Classroom Science Contest

    Classroom Science Contest

169 Discussions

0
None
Ottov8henkoegema

Reply 4 months ago

I tried to use MicroPython to send e-mail but I quickly gave up.
You will need a library that allows you to send e-mail in Python.
With Arduino you can just install libraries. With normal desktop Python you can do that too. But with MicroPython you only have the select libraries that come with it, I think you can make a custom MicroPython build with extra libraries and flash that to the ESP8266, but I couldn't find out how. There's a MicroPython forum where you could ask.

I decided to just learn to work with the Arduino IDE (using C++) as there are many thousands of Arduino tutorials out there that work on ESP's and there aren't a lot of MicroPython tutorials out there.
It also makes it much easier to communicate with sensors. And it's faster then MicroPython. There's definitely a place for MicroPython, but I flashed MicroPython to my ESP because I thought it would be easier then using C++ as I already know Python a bit, but actually learning C++ made everything much, much easier for me.

0
None
dheerajgurjar

2 months ago

while running ESP8266 Gmail Sender We are getting
error: Mail server could not connect
Can someone help me??
Thanks in advance

1 reply
0
None
ryuk3

Question 2 months ago

Hello,

im stuck at the last part it says: Error sending message: SMTP DATA error 1085
any clue what causes this?

1 answer
0
None
Moises DavidPryuk3

Answer 7 days ago

Probably you are using the original password of the Gmail account instead of the key password that they gave yo you.
That key is like a master password for your account and doesn't require verification.
The original key need authentication while the password key doesn't.

0
None
61150015

Question 4 weeks ago

Connection: ESTABLISHED
Got IP address: 172.20.10.3
220 smtp.gmail.com ESMTP g64sm33754221pfg.13 - gsmtp
250 smtp.gmail.com at your service
334 VXNlcm5hbWU6
501 5.5.2 Cannot Decode response g64sm33754221pfg.13 - gsmtp
502 5.5.1 Unrecognized command. g64sm33754221pfg.13 - gsmtp
Error sending message: SMTP AUTH error
220 smtp.gmail.com ESMTP y8sm23288382pgk.20 - gsmtp
250 smtp.gmail.com at your service
334 VXNlcm5hbWU6
501 5.5.2 Cannot Decode response y8sm23288382pgk.20 - gsmtp
502 5.5.1 Unrecognized command. y8sm23288382pgk.20 - gsmtp


I am use NodeMCU 1.0 ESP-12E. Arduino IDE 1.6.11

2
None
pavs99

8 weeks 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.

0
None
RenataD9

5 months ago

Hello, I've been tried to use this code and i had some difficults. i have this:
Connection: ESTABLISHED
Got IP address: 192.168.0.14
Error sending message: Could not connect to mail server

Anyone knows how can i solve this?

Thank you!

8 replies
0
None
omrif1RenataD9

Reply 3 months ago

Getting the same issue:
"Connection: ESTABLISHED
Got IP address: 192.168.0.14
Error sending message: Could not connect to mail server"

i tried to modified the ESP8266WiFi\src\WiFiClientSecure.h file as suggested by Rundhall but still getting the same error...

i am using nodeMCU V3
Arduino IDE 1.8.6

any idea? someone?

many thanks

1
None
RFcontrolledSwitchRenataD9

Reply 4 months ago

Your problem is probably the ESP8266 core version you are using. The new 2.5.0_betaX is using BearSSL. If you downgrade to anything lower than beta1 you will have AxTLS and should be good to go. Worked for me.
It also works with 2.5.0-beta2 core if you switch to WiFiClientSecureAxTLS.h as this uses the old AxTLS.

0
None
RundhallDavidS1694

Reply 3 months ago

I had the
same issue.


In my
Arduino library I modified the ESP8266WiFi\src\WiFiClientSecure.h


To this:


#define USING_AXTLS
#include <ESP8266WiFi.h>
//#include <WiFiClientSecure.h>
#include "WiFiClientSecureAxTLS.h"
using namespace axTLS;
#ifndef USING_AXTLS
// do not default to BearSSL API ("using" has no "unusing" counterpart)
using namespace BearSSL;
#endif

After it
worked!!

0
None
mooserpRundhall

Reply 3 months ago

Thanks a ton Rundhall! worked perfect. Thanks for taking the time to post!

0
None
1-prop-headRundhall

Reply 3 months ago

As I did not properly encrypt email and pw
I also needed to enable less secure feature
All good sorry for the unnecessary question Great stuff thanks

0
None
1-prop-headRundhall

Reply 3 months ago

This did move me forward but.. now I see the following response

501 5.5.2 Connot Decode response d3sm--------------.64 - gsmtp
502 5.5.1 Unrecognized command. d3sm---same----.64 - gsmtp
Error sending message: SMTP AUTH error

Great sutff thanks for your help
dm

0
None
DarkoP3Rundhall

Reply 3 months ago

With same problem ... Now I got:

Connection: ESTABLISHED
Got IP address: 192.168.1.76
Error sending message: SMTP DATA error
1517

Do you have any idea what is wrong? (Arduino 1.8.8, ESP8266 core 2.5.0) Tnx

0
None
Borya

Tip 7 months ago

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.

https://github.com/CosmicBoris/ESP8266SMTP