Introduction: ESP8266 GMail Sender

Picture of 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

Picture of 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

Picture of 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

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


Löwenherz made it! (author)2017-12-11

Yes I made it founding on a contribution from a certain Boris Shobat (September 29 2016). Maybe that's you. Without that help I would never have been able to do it! The eMail-Sender worked fine until yesterday. It seems that Google changed its access restrictions. As you mention it here I had to allow 'less secure apps' and all runs again. Its a great thing and very helpful for older friends of mine. They feel much more secure with such a push button that calls for help or tells others 'I'm OK'. Thank you for sharing!!!

KenjiY4 (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 (author)KenjiY42017-03-31

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

Borya (author)gregmuch2017-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 (author)Borya2017-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.

larryn46 (author)KambizF2017-11-22

It is hard to find "Enable less secure apps" in Gmail. Click on icon with gear on it at upper right corner of gmail screen, look for My Account. Under it find "Sign In and Security", then look for "Apps with Account Access"

bearded_goat (author)2017-11-19

Thanks! this works wonderfully! I am trying to modify this to email myself voltage levels from a battery that I am trying to monitor. What would be the best way to include a constantly changing voltage readout either in the subject or body? I currently have it configured to send me an email letting me know my voltage set point was broken, but I would like to include the actual voltage in a daily heartbeat email as well just to let me know its working.

b7labelle (author)2017-11-14

Hey thanks, this is EXACTLY what I needed for my project! Thank you so much!!!! Got it working in no time!

LeventeR made it! (author)2017-11-09

Thank you!!

gogoxgogo (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

GuillaumeR20 (author)gogoxgogo2017-10-23

isn't it because you tried a wrong username ? have you tried your username or

KambizF (author)gogoxgogo2017-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 (author)KambizF2017-04-28

but this one is working

KambizF (author)gogoxgogo2017-04-28

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

KambizF (author)KambizF2017-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 (author)KambizF2017-04-29

You figured it out by yourself excellent.

Borya (author)KambizF2017-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 (author)Borya2017-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.

lcarcagno (author)2017-10-05

Hi Borya!

Great job! I've got a question if you can help me. Everything works just fine at home or using my phone as hotspot but I'm trying to connect to using the network at the office and I get a message saying "Could not connect to mail server". I tryed to ping and also tryed telnet using ports 25, 465 ans 587 and had no problems from a PC. How can I tell the reason of the failure? Thanks for your great job!

-Giro- (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

MassimoN5 (author)-Giro-2017-08-30

I have the same need: to send a message to two recipients.

I have tried to write "," in the recipient field, but without success. Any idea?

Thank you


The sketch works fine with Yahoo mail too

bpm5cm (author)MassimoN52017-09-22

I was able to send to multiple recipients by basically repeating the code. It may not be the most practical way, but it worked.

Gsender *gsender = Gsender::Instance(); // Getting pointer to class instance

String subject = "Subject Optional";

if(gsender->Subject(subject)->Send("", "Setup Test"))

if(gsender->Subject(subject)->Send("", "Setup Test")) {

Serial.println("Message send.");

soardr (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?

George RayL (author)soardr2017-09-11

Make sure the have the proper port selected when you try and upload your sketch.

esp8266sender (author)soardr2017-03-28

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

lorenzob26 made it! (author)2017-08-31

Very good,

i'd like to use it in trap camera, is it extendable with attachements?

LookStop (author)2017-08-27

This is just what i need. Thank you very much for sharing.

fenixbinario made it! (author)2017-08-26

Nice Job!

DanieleT9 (author)2017-08-22

I have a problem: I'm trying to use a WiFi that require proxy setting, just IP and Port number.

proyectoinvernadero2017 (author)2017-08-21

Perfecto, funciona sin problemas en WEMOS D1 (PRIMERA VERSION). Gracias por el aporte

bruster999 (author)2017-08-17

Thanks! This was a great Instructable, nicely laid out and clearly worded with lots of informative pics. Well done!

I was looking for some way to do this a few weeks ago for a water level monitoring device but couldn't find one that didn't require a paid account somewhere so I built one using a raspberry pi. It worked but all I really needed was an ESP8266 so it was overkill. I'll use this next time I need to email from ESP (have you tried it on the ESP32 yet?).

RayG69 (author)2017-08-15

Works perfectly, thanks very much for your work !!

Just for the newbies like me, it's necessary to encode with base64 the email address and also the gmail account app password.

Really nice and simple to set up.

Thanks a lot !

AliA239 made it! (author)2017-08-02

Much Thanks :)

EduardoB14 (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 (author)EduardoB142017-07-14

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

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

EduardoB14 (author)Borya2017-07-14

Hi Borya, Thans for your answer, the line

Gsender* Gsender::_instance = 0;

was commented.

TogzhanS (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!!!!

zinhtuntest1 (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 (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 (author)TobbeG2017-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 (author)DaveJ12017-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 (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 (author)Avionic man2017-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 (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 (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 (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

kolive (author)2017-04-20

TKS great job,
Work perfect

JatinA3 (author)2017-04-20

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

aeropic. (author)2017-04-08

Brilliant piece of code. It works like a charm.

Thank you for this.


About This Instructable




More by Borya:ESP8266 GMail sender
Add instructable to: