ESP8266 GMail Sender

173,266

667

203

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:

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

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

16 People Made This Project!

Recommendations

  • Make it Glow Contest

    Make it Glow Contest
  • First Time Author Contest

    First Time Author Contest
  • PCB Challenge

    PCB Challenge

203 Discussions

7
pavs99
pavs99

1 year 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
ihabelchami
ihabelchami

Reply 11 months ago

thank you working well after i add this function .

0
RalphC13
RalphC13

Reply 1 year ago

wow thank you, i tried many things to get it working and you bring the Fix:D

0
JimB74
JimB74

6 months 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 :-)

0
arniepodo
arniepodo

Reply 3 months 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.

0
kromike2
kromike2

Question 3 months 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

0
kromike2
kromike2

Answer 3 months ago

Ahh... fixed by reading the instructions completely.... base64...

0
arniepodo
arniepodo

Reply 3 months ago

Would you mind posting your .ino (you can obscure your account name and password)? I tried the .ino dated 3-13-2018 that is in the github from Boris and have tried running it with my username and password in plain text or in base64, but keep getting error "Could not connect to mail server." Please see my post of today for more details of what I have. Thanks.

1
arniepodo
arniepodo

3 months ago

I have downloaded the latest version of this from github (ESMTP8266-Master dated 3-13-2018). I get this error when I run the ino:
Error sending message: Could not connect to mail server.

I tried both plain text and base64 encoded user account and passwords for gmail. Same error message. I do notice that the include statements have different colors:
#include <ESP8266WiFi.h>
#include <ESP8266SMTP.h>

The first include is colored brown but the second include is colored black. Could this indicate a problem?
I have set gmail to allow less secure access and am able to send email via a python script on a raspberry pi so I know the gmail smtp server is working for me. I just can't seem to get it to work on my ESP8266 Wemos D1. Using Arduino IDE 1.8.13 and ESP8266 core 2.4.2.
Anyone have any idea how to get this to work? Thanks!

0
HJDGuppy
HJDGuppy

Question 5 months 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?

0
davidpaine
davidpaine

6 months ago

Worked well! Thank you for the tutorial. Here is a website I would like to suggest for base64 encode
https://url-decode.com/tool/base64-encode
You must check it out. That website contains the tools related to number utilities, unit converter, developers, string utilities, converter, and IP tools as well under one domain. That will definitely help you.

2
mrmir
mrmir

7 months ago

I am getting this error:
Connection: ESTABLISHED
Got IP address: 192.168.10.6
Error sending message: Could not connect to mail server

1
Sugandhan-Vazhumuni
Sugandhan-Vazhumuni

8 months ago

Sometimes I have an issue after installing the package. Seems that no package is installed. After sometimes its updated

0
Borya
Borya

Tip 2 years 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

0
mooserp
mooserp

Reply 1 year ago

Hi. I have tried your new code but get the following error>Error sending message: Could not connect to mail server. I am using "version 1" of ESP8266WiFi. Could this be a problem?
Thanks.
By the way, your first version works perfect!