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.

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 (

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
  • 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("", "Setup test"))

first parameter of Send function is recipient email, second message text.

Change recipient from <> to your email which will receive 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 for encoding, result must be something like:

const char* EMAILBASE64_LOGIN = "Y29zbWkxMTExMUBnbWFpbC5jb20=";
  • Now set FROM field.
const char* FROM = "";

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


EduardoB14 made it!(author)2017-07-13

Hi Borya.
'Gsender' was not declared in this scope

This is my error when I try to compile it.
Could somedody help me.

Borya made it!(author)2017-07-14

Did you include Gsender.h in your main sketch file?

#include "Gsender.h"
void setup(){}
void loop(){}

EduardoB14 made it!(author)2017-07-14

Hi Borya, Thans for your answer, the line

Gsender* Gsender::_instance = 0;

was commented.

TogzhanS made it!(author)2017-07-07

I have done everything described on your tutorial, and I always have the error about missing the wifi settings. Actually my question is where I have to get ESP8266WiFi.h cause I think that I don't have it.

I'm a beginner and I have to make the progect with trash box that send email when it's full. i'm using a different model of esp 8266. It's simplier than yours

Heeeeeelp me please!!!!

-Giro- made it!(author)2017-07-04

Hi Borya,

your code works veru well and your job is magnificent! Thank you very much!!

Only one question: how to add more recipients for one mail?

tried to change some strings in gsender.cpp and gsender.h with var %to2
but whitout success. Can you give me a little help please?

Thank you very much

zinhtuntest1 made it!(author)2017-05-27


I uploaded the code into my ESP8266 and it has been working perfectly for the past few days. But when I try it today, it is giving me the error "Cannot connect to mail server"... I tried with new email address but still not working. Please help me.


TobbeG made it!(author)2016-11-21

Great for sending gmails

I added a few yields() and now it works without WDT reboots, even for my larger emails 3kB

One question - I have 1000 IOT units sending email once a day , most likely at the same time, at least within 5 minutes - I assume gmail will not work correctly or block the account if they all access the same minute, or?

How can it be done so that all devices still can use one mail account and send daily ping emails to user?

Great work sharing the gmail sender !!

DaveJ1 made it!(author)2017-05-20

Hi TobbeG - I'm having the same issue, but am not as advanced as you...can you provide some more specifics about how you avoided this error?

TobbeG made it!(author)2017-05-26

Hi DaveJ1

I redesigned this thing moving it to SMTP2GO which allows all my IOT devices to use the same smtp server and not having to go through creating a gMail account.

Have a look here :

Setting up a SMTP2GO account is easy, and you get 1000 emails/month for free.

Avionic+man made it!(author)2017-05-14

great job!

I used it for sending pir and flame sensors works good but i have a problem that sending email successively while pins are high.would you please help me ?

DaveJ1 made it!(author)2017-05-21

I think you need something like this:

int Astate = 0;

int lastAState = 0;

Astate = digitalRead (A);

if (Astate != lastAState)


if (AState == HIGH){

send mail


else {}

lastAState = AState;

This is a single-shot high type code...once the code goes high, it activates the IF. You can put a negative statement in the ELSE if you want to see when it goes Low (I'm struggling myself with the email send part...not sure if you can have two mail send commands).

DaveJ1 made it!(author)2017-05-21

Great i'ble, it worked out of the box for me, but I'm trying to include the "send mail" functionality into another sketch that is already up and running. I tried to copy out only the GSender portion of the code, since the rest of the code handles the WiFi connection, and my other sketch already takes care of that. My plan was to tuck in that section of your Gmail code into an "IF" statement of my other one, so if the condition is met, an email is sent. Instead, however, I get a Stack error (Exception 29). I see others have gotten this too, and research on the internet shows it's waaay over my head. Is there a better way to modify your code so that it only sends when desired?

plrang made it!(author)2017-05-13

Works great, there is a javascript function if one needs that, so it's easy to encode strings in the browser console instead of using some external services for login and pwd.

In JS or console: btoa("password");

msameer39 made it!(author)2017-05-06

Thanks a lot buddy. I was searching all day long

diy_bloke made it!(author)2017-05-06

Well done. I have sent mail using a 3rd party mailservice (3rd as in 'besides google') to send from my google account, but I know that in the past, with an arduino I had been able to do it directly. You made it happen.
As I just stumbled upon your code, I haven't tried it yet, but I know that some mail mail servers on receiving are a bit picky regarding the domain the mail is coming from and if your IP address is not known as belonging to Google, they may reject the mail as undeliverable.
That is no shortcoming of your code, but it is good to take into account when people experience rouble in getting their mail delivered

gogoxgogo made it!(author)2017-04-12

please help i got this error

535-5.7.8 Username and Password not accepted. Learn more at

Error sending message: SMTP AUTH error

KambizF made it!(author)2017-04-28

gogoxgogo -- Did you find a way to solve the "username and password not accepted" problem? I would greatly appreciate any hint you can provide. I have done all the tricks that have been mentioned on this site and many otehr blogs, but I cannot get it to work. Thanks

gogoxgogo made it!(author)2017-04-28

but this one is working

KambizF made it!(author)2017-04-28

gogoxgogo -- Thank you for the reply and the link to a great YouTube Video. I am following it.

KambizF made it!(author)2017-04-28

gogoxgogo -- I figured out what I was doing wrong. Please see my latest reply to Borya. You have to put the login and password of the gmail account in the body of the Gsender library, not in the body of the sketch. Mine works now. Thanks to Borya, I feel I have grown a new set of wings.

Borya made it!(author)2017-04-29

You figured it out by yourself excellent.

Borya made it!(author)2017-04-28

Hi! I just spent 2 minutes to check if its still works, and it works great for me.

Got IP address:
220 ESMTP g24sm1253499ljg.11 - gsmtp

250 at your service

334 VXNlc........./*some symbols here looks like key*/

334 UGFzc3......../*some symbols here looks like key*/

235 2.7.0 Accepted

250 2.1.0 OK g24sm1253499ljg.11 - gsmtp

250 2.1.5 OK g24sm1253499ljg.11 - gsmtp

354 Go ahead g24sm1253499ljg.11 - gsmtp

250 2.0.0 OK 1493414890 g24sm1253499ljg.11 - gsmtp

221 2.0.0 closing connection g24sm1253499ljg.11 - gsmtp

Message send.

If you have some problems please read carefully whole instructable again.

Double check:

1. did you allowed Less secure apps in your gmail account?

2. Is your credentials correct? As I wrote use to

encode your login and password, it should to somethisng like this:

const char * EMAILBASE64_LOGIN = "Tdd23lOp9ya532WwuY29t";
const char* EMAILBASE64_PASSWORD = "IGh4KFloTldGH0u";

3. If none of above will not help you can send me your code and I will try to help you.

KambizF made it!(author)2017-04-28

Borya, Hi

I found out what I was doing wrong. Instead of putting the login name and password of the gmail account in the Gsender library, I had put it in the main sketch. I am an old man trying to learn Arduino programming, and I did not know how to open a tab for the library. Anyway, it works now. Thank you so much.

KenjiY4 made it!(author)2016-12-06

I could send it with Wemos D1.

I want to put a "CRLF" in the message text, so please let me know.....


String content = "";

content += "sensorValue = ";

content += sensorValue;

content += "\r\n";   //CRLF code was ignore.

content += "outputValue = ";

content += outputValue;

if(gsender->Subject(subject)->Send("MyE-mail@address", content ))


gregmuch made it!(author)2017-03-31

Hello, do you finally find a way to have CRLF in the body message ?

Borya made it!(author)2017-04-28

Hello, sorry for really late reply.

What is your email massage to web browser???

Its HTML document.

Does HTML understand CRLF???

I guess not.

So you should use HTML tag <p>Text from new line!</p>

or <br> a line break.

KambizF made it!(author)2017-04-28

Borya -- Thank you for getting back to me so quickly. You are very smart and a very nice guy at the same time. That is a wonderful combination. I have enabled the Less Secure Apps in my gmail account, and I checked it again and again. I used the base64encode for both the login gmail address and the email password, and I did it about 10 times to make sure it was correct. I still get this:



Got IP address:

220 ESMTP h34sm1321168otb.39 - gsmtp

250 at your service

334 VXNlcm5hbWU6

334 UGFzc3dvcmQ6

535-5.7.8 Username and Password not accepted. Learn more at

Error sending message: SMTP AUTH error

Here is the code. I have changed the SSID, its passowrd, Login email address and password. I tried to use your code as much as possible.

#include <Gsender.h>
#include "ESP8266WiFi.h"

#pragma region Globals
const char* ssid = "MyHomeSSID"; // WIFI network name
const char* password = "MyHomePassword"; // WIFI network password
uint8_t connection_state = 0; // Connected to WIFI or not
uint16_t reconnect_interval = 10000; // If not connected wait time to try again
#pragma endregion Globals

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);
} else {
WiFi.begin(ssid, password);

uint8_t i = 0;
while(WiFi.status()!= WL_CONNECTED && i++ < 50)
if(i == 51) {
Serial.print("Connection: TIMEOUT on 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: ");
return true;

void Awaits()
uint32_t ts = millis();
if(millis() > (ts + reconnect_interval) && !connection_state){
connection_state = WiFiConnect();
ts = millis();

void setup()
connection_state = WiFiConnect();
if(!connection_state) // if not connected to WIFI
Awaits(); // constantly trying to connect

Gsender *gsender = Gsender::Instance(); // Getting pointer to class instance
const char* EMAILBASE64_LOGIN = "encodedvalueofloginemailaddresofgmail";
const char* EMAILBASE64_PASSWORD = "encodedvalueofemailpassword";

const char* FROM = "";

String subject = "Alert from iot of Kam";
if(gsender->Subject(subject)->Send("", "This alert is sent from iot of Kam due to event")) {

Serial.println("Message send.");
} else {
Serial.print("Error sending message: ");

void loop(){}

Thank you so much for taking time to help me.

kolive made it!(author)2017-04-20

TKS great job,
Work perfect

JatinA3 made it!(author)2017-04-20

Brilliant work.. Thanks for it.. I made it successfully :)

aeropic. made it!(author)2017-04-08

Brilliant piece of code. It works like a charm.

Thank you for this.


gregmuch made it!(author)2017-03-30

Wow, that's great !

Less than 10 minutes to send the first email from my eps (WeMos D1 R2), your code is very nice.

esp8266sender made it!(author)2017-03-28

Thank you!It is working! and how receive messages on ESP?P.S. sorry for my bad English - I am from Ukraine

soardr made it!(author)2017-03-21

Hi guys. I have done everything but when i try to upload the sketch the error is:

warning: espcomm_sync failed

error: espcomm_open failed

error: espcomm_upload_mem failed

error: espcomm_upload_mem failed

Can someone help me to find a way?

esp8266sender made it!(author)2017-03-28

Before uploading conect GPIO 0 to ground and restart your esp.

Davey_D made it!(author)2017-03-16 does not communicate without encryption.

This is possible using OpenSSL command:

s_client -connect

This returns a whole load of jargon. Next command:


from here on you enter all your details etc.

So how could this instructable ever possibly work?

cw1000 made it!(author)2017-02-06

Thanks. I made it and it worked great. I was looking for a way to send an email

To notify me if there was a problem at a remote site without having the be linked to the server constantly. Instructions were clear and concise.

RimvydasP made it!(author)2017-01-08

Borya thank you, it's working ;)

sellh made it!(author)2016-11-28

Hi guys. I'm new here. I have done everything but when i try to upload the sketch the error is:

warning: espcomm_sync failed

error: espcomm_open failed

error: espcomm_upload_mem failed

error: espcomm_upload_mem failed

Can someone help me to find a way?


RimvydasP made it!(author)2017-01-08

sellh try conect GPIO0 pin to GND

Kuba+Kub%C3%AD%C4%8DekA made it!(author)2016-12-28

Hi, i have a problem.

Error sending message: SMTP AUTH error

What now ?


KenjiY4 made it!(author)2016-12-06

I solved it myself........


String content = "";

content += "sensorValue = ";

content += sensorValue;

content += "<BR>”;   //htm <br> code was success.

content += "outputValue = ";

content += outputValue;

if(gsender->Subject(subject)->Send("MyE-mail@address", content ))


Arthuro7 made it!(author)2016-11-23

Hi @Borya
How can I send an integer and string variable instead of a text defined in the message content?
if(gsender->Subject(subject)->Send("", "HERE"))

Borya made it!(author)2016-11-25

Hi! If you want something like this "value = 1", try this one

intvar = 1;
Stringstr = "value = ";
gsender->Subject(subject)->Send("email", str + var);

Arthuro7 made it!(author)2016-11-25

It's just what I needed, thank you very much @Borya

TobbeG made it!(author)2016-11-24

Hi @Borya

I have added rBase64 to calculate Base64 at runtime and it works fine sending me shorter emails when the device boots.

However, when I am sending somewhat longer emails, ca 3kB, it always crashes and I have aded yield(); all over the place w/o success.

Can you help me find out whats going wrong??

MUCH appreciated function which I would like to fully use too !

Thanks for your help - Thorbjörn

Exception (29):

epc1=0x4000e1b2 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000100 depc=0x00000000

ctx: cont

sp: 3fff3f60 end: 3fff4540 offset: 01a0


3fff4100: 00000040 3fff565c 40212d60 402311af

3fff4110: 3fffa4a4 3fff565c 3fff6f2c 40231348

3fff4120: 00000000 ffffffff 3fffa494 3fff565c

3fff4130: 00000040 3fff565c 3fff9b0c 40231745

3fff4140: 3fff964c 3fff565c 3fff9b0c 4023213d

3fff4150: 3fff5bdc 87274380 2cd2bef0 00000001

3fff4160: 3fff565c 3fff9af4 3fff9b0c 00000001

3fff4170: 00000010 3fff565c 0000000f 402323b1

3fff4180: 3fff6f2c 3fff5d14 0000000f 00000001

3fff4190: 00000001 3fff5d5c 3fff7085 40231214

3fff41a0: 00000100 3fff5bac 3fff700f 00000000

3fff41b0: 00000100 3fff5bac 3fff700f 4023343d

3fff41c0: 3fff41f0 00000000 000000d0 00000030

3fff41d0: 711d2b0c 771b0000 3fff695c 00000004

3fff41e0: 00000004 3fff6d0c 3fff7009 4022f548

3fff41f0: aa600203 4c40b2d9 f163236d 2d93e7e0

3fff4200: 3eb6f722 766bf20f a7c8ab3d 68bddfba

3fff4210: 78cb410d 15bf8e5c 351a0a10 2b0cb689

3fff4220: 00000004 3fff7009 3fff6d0c 4022f949

3fff4230: 00000000 600011f0 3fff695c 3fff6c6c

3fff4240: 00000000 00000004 00000004 4020bbf3

3fff4250: 3fff700d 3fff7009 3fff6ddc 00000b80

3fff4260: 00000c09 3fff7bf0 00000021 00000004

3fff4270: 3fff6d0c 3fff7009 00000004 00000004

3fff4280: 00000004 3fff7009 3fff6d0c 4022f340

3fff4290: 00000000 3fff7009 3fff6d0c 4022f6e8

3fff42a0: 0000000e 3ffea990 0000000f 401004d8

3fff42b0: 3fff4300 0000000e 00000010 00000000

3fff42c0: 3fff6a2c 3ffea990 3fff6d0c 01000000

3fff42d0: 3fff4300 3fff4350 3fff6d0c 4022f498

3fff42e0: 4020baa4 00000004 3fff3520 00001387

3fff42f0: 0001c7b3 3fff4350 3fff5864 4020c065

3fff4300: 000001d1 3fff695c 3fff4350 4020b4ca

3fff4310: 6ca6e940 00001000 c0034035 3fff28a8

3fff4320: 000001d1 3fff4350 3ffea990 4020c275

3fff4330: 3ffead30 6ca6e940 3ffead30 6ca6e940

3fff4340: 3fff28b4 3fff32e8 3fff57d4 40211508

3fff4350: 3ffea438 00000000 000003e8 4010020c

3fff4360: 3fff2970 3fff6c6c 3fff5864 4010068c

3fff4370: 3fff32ec 00000010 3fff43d0 40212010

3fff4380: 0000000b 00000010 3fff43d0 40211918

3fff4390: 00000000 00000000 00000000 402120ed

3fff43a0: 3fff28b4 3fff443c 3fff43e8 3fff43d0

3fff43b0: 3fff28b4 3fff32e8 3fff43d0 3fff57d4

3fff43c0: 3fff28b4 3fff32e8 00000001 40206deb

3fff43d0: 3fff6934 0000001f 00000010 3fff584c

3fff43e0: 0000000f 0000000b 3fff68fc 0000002f

3fff43f0: 00000020 3fff57f4 0000001f 00000017

3fff4400: 00000000 00000000 00000000 00000000

3fff4410: 00000000 00000000 00000000 00000000

3fff4420: 00000000 00000000 00000000 00000000

3fff4430: 3fff5824 0000001f 0000001e 3fffab0c

3fff4440: 00000a3f 00000a3f 00000000 11020000

3fff4450: 00000000 3fff57d4 00000000 30303231

3fff4460: 0003c03b 00000000 00000000 3fff2940

3fff4470: 3fff2840 3fff195c 3fff18a8 40208956

3fff4480: 00000000 00000000 00000000 00000000

3fff4490: 00000000 00000000 00000000 00000000

3fff44a0: 00000000 00000000 00000000 00000000

3fff44b0: 00000000 00000000 00000000 00000000

3fff44c0: 00000000 00000000 00000000 00000000

3fff44d0: 00000000 00000000 00000000 00000000

3fff44e0: 00000000 00000000 00000000 3fff587c

3fff44f0: 0000000f 00000000 feefeffe feefeffe

3fff4500: 3ffea37e 3fff44bc feefeffe feefeffe

3fff4510: 00000000 00000000 00000001 3fff3514

3fff4520: 3fffdad0 00000000 3fff350d 40212dac

3fff4530: feefeffe feefeffe 3fff3520 40100718


L(CÃüZ@ ŠWþÿ

Borya made it!(author)2016-11-25

Sorry, I can not help with debugging.

Rob0148 made it!(author)2016-11-19

Hello congratulations! Your code works fine!

The Arduino IDE that shows Gsender.h very good guide!

I would like to write my email login and password in plain text.
For this work do I need to use a base64 library?

I found this library:

The library returns: = c210cC5nbWFpbC5jb20A

The returns: = c210cC5nbWFpbC5jb20=

I found a bug on library?

How can I leave the variable similar to the ssid variable?

I saw that EMAILBASE64_LOGIN and EMAILBASE64_PASSWORD are private in to Gsender class. Sorry, I do not know about C++ statements.

Can I move it to main code?

Can you help me? Please?

Borya made it!(author)2016-11-21

Hi! You want to modify EMAILBASE64_LOGIN and EMAILBASE64_PASSWORD or just be able to read them in main file? Sorry I not sure what you ask about ssid.

Rob0148 made it!(author)2016-11-21



First I would like to enter data for variables only in the main code, the original code is done in the Gsender.h file, I would like to enter email and password in the main code (*.ino).

Secondly, we need to convert the text email and password to base64 code, and I did not find a good library to do this, I found a library that results in a different base64 code.

The idea is to use a serial connection to enter the login text and the password.

For the serial connection I already have an example.

Thank you!

tangowhisky37 made it!(author)2016-11-20

Awesome tute Borya. Simple, easy to integrate and get running. I bolted the email code onto my existing Plant monitoring system and everything was up and running in a few minutes. Thanks a heap!!!!! Good job Borya.