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

<p>Hi guys. I have done everything but when i try to upload the sketch the error is:</p><p>warning: espcomm_sync failed</p><p>error: espcomm_open failed</p><p>error: espcomm_upload_mem failed</p><p>error: espcomm_upload_mem failed</p><p>Can someone help me to find a way?</p>
<p>smtp.gmail.com does not communicate without encryption. </p><p>This is possible using OpenSSL command:</p><p><strong>s_client -connect smtp.gmail.com:465</strong></p><p>This returns a whole load of jargon. Next command:</p><p><strong>AUTH LOGIN</strong></p><p>from here on you enter all your details etc.</p><p>So how could this instructable ever possibly work?</p>
<p>Thanks. I made it and it worked great. I was looking for a way to send an email</p><p>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.</p>
<p><a href="https://www.instructables.com/member/Borya" rel="nofollow">Borya</a> thank you, it's working ;) </p>
<p>Hi guys. I'm new here. I have done everything but when i try to upload the sketch the error is:</p><p>warning: espcomm_sync failed</p><p>error: espcomm_open failed</p><p>error: espcomm_upload_mem failed</p><p>error: espcomm_upload_mem failed</p><p>Can someone help me to find a way?</p><p><a rel="nofollow">[delete]</a></p>
<p><a href="https://www.instructables.com/member/sellh" rel="nofollow">sellh</a> try conect GPIO0 pin to GND </p>
<p>Hi, i have a problem.</p><p>Error sending message: SMTP AUTH error</p><p>What now ?</p><p>Thanks</p>
<p>I solved it myself........</p><p>&gt;&gt;&gt;</p><p>String content = &quot;&quot;;</p><p>content += &quot;sensorValue = &quot;;</p><p>content += sensorValue;</p><p>content += &quot;&lt;BR&gt;&rdquo;;   //htm &lt;br&gt; code was success.</p><p>content += &quot;outputValue = &quot;;</p><p>content += outputValue;</p><p>if(gsender-&gt;Subject(subject)-&gt;Send(&quot;MyE-mail@address&quot;, content ))</p><p>Thanks,</p>
<p>I could send it with Wemos D1.</p><p>I want to put a &quot;CRLF&quot; in the message text, so please let me know.....</p><p>&gt;&gt;&gt;</p><p>String content = &quot;&quot;;</p><p>content += &quot;sensorValue = &quot;;</p><p>content += sensorValue;</p><p>content += &quot;\r\n&quot;;   //CRLF code was ignore.</p><p>content += &quot;outputValue = &quot;;</p><p>content += outputValue;</p><p>if(gsender-&gt;Subject(subject)-&gt;Send(&quot;MyE-mail@address&quot;, content ))<br></p><p>.&lt;&lt;&lt;</p>
Hi @Borya<br>How can I send an integer and string variable instead of a text defined in the message content?<br>if(gsender-&gt;Subject(subject)-&gt;Send(&quot;boris.on@live.com&quot;, &quot;HERE&quot;))<br>
<p>Hi! If you want something like this &quot;value = 1&quot;, try this one</p><p><strong>int</strong> <em>var</em> = 1;<br> <strong>String</strong> <em>str</em> = &quot;value = &quot;;<br> gsender-&gt;Subject(subject)-&gt;Send(&quot;email&quot;, <strong><em>str</em></strong> + <strong><em>var</em></strong>);</p>
<p>It's just what I needed, thank you very much @Borya</p>
<p>Hi @Borya<br><br>I have added rBase64 to calculate Base64 at runtime and it works fine sending me shorter emails when the device boots. </p><p>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.<br><br>Can you help me find out whats going wrong??</p><p>MUCH appreciated function which I would like to fully use too !<br></p><p>Thanks for your help - Thorbj&ouml;rn</p><p>Exception (29):</p><p>epc1=0x4000e1b2 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000100 depc=0x00000000</p><p>ctx: cont </p><p>sp: 3fff3f60 end: 3fff4540 offset: 01a0</p><p>&gt;&gt;&gt;stack&gt;&gt;&gt;</p><p>3fff4100: 00000040 3fff565c 40212d60 402311af </p><p>3fff4110: 3fffa4a4 3fff565c 3fff6f2c 40231348 </p><p>3fff4120: 00000000 ffffffff 3fffa494 3fff565c </p><p>3fff4130: 00000040 3fff565c 3fff9b0c 40231745 </p><p>3fff4140: 3fff964c 3fff565c 3fff9b0c 4023213d </p><p>3fff4150: 3fff5bdc 87274380 2cd2bef0 00000001 </p><p>3fff4160: 3fff565c 3fff9af4 3fff9b0c 00000001 </p><p>3fff4170: 00000010 3fff565c 0000000f 402323b1 </p><p>3fff4180: 3fff6f2c 3fff5d14 0000000f 00000001 </p><p>3fff4190: 00000001 3fff5d5c 3fff7085 40231214 </p><p>3fff41a0: 00000100 3fff5bac 3fff700f 00000000 </p><p>3fff41b0: 00000100 3fff5bac 3fff700f 4023343d </p><p>3fff41c0: 3fff41f0 00000000 000000d0 00000030 </p><p>3fff41d0: 711d2b0c 771b0000 3fff695c 00000004 </p><p>3fff41e0: 00000004 3fff6d0c 3fff7009 4022f548 </p><p>3fff41f0: aa600203 4c40b2d9 f163236d 2d93e7e0 </p><p>3fff4200: 3eb6f722 766bf20f a7c8ab3d 68bddfba </p><p>3fff4210: 78cb410d 15bf8e5c 351a0a10 2b0cb689 </p><p>3fff4220: 00000004 3fff7009 3fff6d0c 4022f949 </p><p>3fff4230: 00000000 600011f0 3fff695c 3fff6c6c </p><p>3fff4240: 00000000 00000004 00000004 4020bbf3 </p><p>3fff4250: 3fff700d 3fff7009 3fff6ddc 00000b80 </p><p>3fff4260: 00000c09 3fff7bf0 00000021 00000004 </p><p>3fff4270: 3fff6d0c 3fff7009 00000004 00000004 </p><p>3fff4280: 00000004 3fff7009 3fff6d0c 4022f340 </p><p>3fff4290: 00000000 3fff7009 3fff6d0c 4022f6e8 </p><p>3fff42a0: 0000000e 3ffea990 0000000f 401004d8 </p><p>3fff42b0: 3fff4300 0000000e 00000010 00000000 </p><p>3fff42c0: 3fff6a2c 3ffea990 3fff6d0c 01000000 </p><p>3fff42d0: 3fff4300 3fff4350 3fff6d0c 4022f498 </p><p>3fff42e0: 4020baa4 00000004 3fff3520 00001387 </p><p>3fff42f0: 0001c7b3 3fff4350 3fff5864 4020c065 </p><p>3fff4300: 000001d1 3fff695c 3fff4350 4020b4ca </p><p>3fff4310: 6ca6e940 00001000 c0034035 3fff28a8 </p><p>3fff4320: 000001d1 3fff4350 3ffea990 4020c275 </p><p>3fff4330: 3ffead30 6ca6e940 3ffead30 6ca6e940 </p><p>3fff4340: 3fff28b4 3fff32e8 3fff57d4 40211508 </p><p>3fff4350: 3ffea438 00000000 000003e8 4010020c </p><p>3fff4360: 3fff2970 3fff6c6c 3fff5864 4010068c </p><p>3fff4370: 3fff32ec 00000010 3fff43d0 40212010 </p><p>3fff4380: 0000000b 00000010 3fff43d0 40211918 </p><p>3fff4390: 00000000 00000000 00000000 402120ed </p><p>3fff43a0: 3fff28b4 3fff443c 3fff43e8 3fff43d0 </p><p>3fff43b0: 3fff28b4 3fff32e8 3fff43d0 3fff57d4 </p><p>3fff43c0: 3fff28b4 3fff32e8 00000001 40206deb </p><p>3fff43d0: 3fff6934 0000001f 00000010 3fff584c </p><p>3fff43e0: 0000000f 0000000b 3fff68fc 0000002f </p><p>3fff43f0: 00000020 3fff57f4 0000001f 00000017 </p><p>3fff4400: 00000000 00000000 00000000 00000000 </p><p>3fff4410: 00000000 00000000 00000000 00000000 </p><p>3fff4420: 00000000 00000000 00000000 00000000 </p><p>3fff4430: 3fff5824 0000001f 0000001e 3fffab0c </p><p>3fff4440: 00000a3f 00000a3f 00000000 11020000 </p><p>3fff4450: 00000000 3fff57d4 00000000 30303231 </p><p>3fff4460: 0003c03b 00000000 00000000 3fff2940 </p><p>3fff4470: 3fff2840 3fff195c 3fff18a8 40208956 </p><p>3fff4480: 00000000 00000000 00000000 00000000 </p><p>3fff4490: 00000000 00000000 00000000 00000000 </p><p>3fff44a0: 00000000 00000000 00000000 00000000 </p><p>3fff44b0: 00000000 00000000 00000000 00000000 </p><p>3fff44c0: 00000000 00000000 00000000 00000000 </p><p>3fff44d0: 00000000 00000000 00000000 00000000 </p><p>3fff44e0: 00000000 00000000 00000000 3fff587c </p><p>3fff44f0: 0000000f 00000000 feefeffe feefeffe </p><p>3fff4500: 3ffea37e 3fff44bc feefeffe feefeffe </p><p>3fff4510: 00000000 00000000 00000001 3fff3514 </p><p>3fff4520: 3fffdad0 00000000 3fff350d 40212dac </p><p>3fff4530: feefeffe feefeffe 3fff3520 40100718 </p><p>&lt;&lt;&lt;stack&lt;&lt;&lt;</p><p>L(C&Atilde;&uuml;Z@ ŠW&thorn;&yuml;</p>
<p>Sorry, I can not help with debugging.</p>
Hi<br>Great for sending gmails <br><br>I added a few yields() and now it works without WDT reboots, even for my larger emails 3kB<br><br>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?<br><br>How can it be done so that all devices still can use one mail account and send daily ping emails to user?<br><br>Great work sharing the gmail sender !!<br>
<p>Hello congratulations! Your code works fine!<br><br>The Arduino IDE that shows Gsender.h very good guide!<br><br>I would like to write my email login and password in plain text.<br>For this work do I need to use a base64 library?<br><br>I found this library:<br><a href="https://github.com/adamvr/arduino-base64." rel="nofollow">https://github.com/adamvr/arduino-base64.</a></p><p>The library returns:</p><p>smtp.gmail.com = c210cC5nbWFpbC5jb20A</p><p>The <a href="https://www.base64encode.org/" rel="nofollow">https://www.base64encode.org/ </a> returns:</p><p>smtp.gmail.com = c210cC5nbWFpbC5jb20=</p><p>I found a bug on library?</p><p>How can I leave the variable similar to the ssid variable?</p><p>I saw that EMAILBASE64_LOGIN and EMAILBASE64_PASSWORD are private in to Gsender class. Sorry, I do not know about C++ statements.</p><p>Can I move it to main code?</p><p>Can you help me? Please?</p>
<p>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. </p>
Hi,<br><br>Sorry,<br><br>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).<br><br>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.<br><br>The idea is to use a serial connection to enter the login text a@b.com and the password.<br><br>For the serial connection I already have an example.<br><br>Thank you!
<p>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. </p>
<p>I can only echo the other feedbacks - A very well constructed instructable. I have integrated this into my own automation application with data channels to Thingspeak and Twitter and it all fits together nicely. Thanks &amp; well done. Next step a SMS output element</p>
<p>Hi<br>Thsi gMail sender is brilliant. It works well for me for smaller emails.<br><br>However I now send a 2,7kB size massage and it crashes with Exception(29)<br><br>Anyone knows what cancause this?</p><p>I have around 22kB RAM freeheap </p>
<p>Awesome instructable and it worked on the first try. Thank you very much as I have been trying to get that working for awhile. </p>
<p>Hi,</p><p>Great instructable on email sender using only an ESP8266.</p><p>I made it.</p><p>Would love to have a instructable of new email notifier using ESP8266.</p><p>Keep posting Instructables.</p><p>Thanks.</p>
how could i change this code in order to send an email when a digital pin goes high?
<p>The easiest way is to monitor pin state in loop function. </p><p>At the top of sketch after &quot;#pragma region Globals&quot; add:</p><p>const uint8_t triggerPin = 4; // I used pin 4 you can try different</p><p>then in setup function add:</p><p>pinMode(triggerPin, INPUT_PULLUP);</p><p>next modify loop function:</p><p>void loop(){<br> if(digitalRead(triggerPin)) // if pin HIGH<br> {<br> Gsender *gsender = Gsender::Instance();<br> if(gsender-&gt;Subject(&quot;PIN 4 HIGH&quot;)-&gt;Send(&quot;***yourMail***@live.com&quot;, &quot;PIN 4 GOES HIGH&quot;)) {<br> Serial.println(&quot;Message send.&quot;);<br> } else {<br> Serial.print(&quot;Error sending message: &quot;);<br> Serial.println(gsender-&gt;getError());<br> }<br> }<br>}</p><p>But be careful this code will constantly send emails while triggerPin is HIGH</p><p>you can use millis() function to count time from last email.</p>
thank you soo much for your work! i have two more questions, though: <br><br>1. would it be possible to also implement the esp_deepsleep function for a more efficient notification system?<br><br>2.what would it take to convert all this awesome esp email craziness into a library so that the user only calls &quot;ESPMAIL.begin(address,pwd,sendTO) or something like that in the beginning and espmail.send() lateron? this would be soo awesome since the code would then be wayy more understandable for noobs like me and i could (maybe?) implement all the functions i asked you for all by myself
<p>Hi! About your first question visit </p><p><a href="https://github.com/esp8266/Arduino/blob/master/doc/libraries.md" rel="nofollow">https://github.com/esp8266/Arduino/blob/master/doc...</a></p><p>search for ESP.deepSleep</p>
<p>i used a wrong library before,and trun it right now..........run and work perfect.i looking for it a long long long................time,thankyou very much...................</p><p>some little change......</p><p>void setup(){</p><p>..............</p><p>pinMode(4,0);</p><p> pin4=digitalRead(4);</p><p>}</p><p>void loop(){</p><p> if(digitalRead(4)!=pin4)</p><p> {</p><p> pin4=digitalRead(4);</p><p>send_email();</p><p> }</p><p> delay(1000);</p><p> }</p><p>void send_email(){</p><p>Gsender *gsender = Gsender::Instance(); // Getting pointer to class instance</p><p> String subject = &quot;Subject is optional!&quot;;</p><p> String data=&quot;pin4= &quot;;</p><p> data+= digitalRead(4);</p><p>if(gsender-&gt;Subject(subject)-&gt;Send(&quot;emailaddress@email.com&quot;, data) ){</p><p> Serial.println(&quot;Message send.&quot;);</p><p> } else {</p><p> Serial.print(&quot;Error sending message: &quot;);</p><p> Serial.println(gsender-&gt;getError());</p><p> }</p><p>}</p>
<p>Is there an easy way to send the one email to multiple recipients at once without changing Gsender.h or Gsender.cpp? For example: if(gsender-&gt;Subject(subject)-&gt;Send(&quot;boris.on@live.com,your_email@mail.com&quot;, &quot;Setup test&quot;)) doesn't work.</p>
<p>Great work!</p><p>How can I add the following:</p><p>1. Send a larger text with '\n' etc.. does not seem to work now?</p><p>2. Is it possible to add attachments? like a jpg or so?</p><p>//Thorbj&ouml;rn</p>
<p> Why?</p>
<p>so.. &quot;Y29zbWkxMTExMUBnbWFpbC5jb20=&quot; should be &quot;your_email@gmail.com&quot; ?</p>
<p>If I understood your question ... you need to input &quot;your_email@gmail.com&quot; into the <em><a href="https://www.base64encode.org/" rel="nofollow">base64encode.org</a> website and it will give you an output like </em>&quot;Y29zbWkxMTExMUBnbWFpbC5jb20=&quot;. The website converts the email address into base64 format.</p>
ok,i got it.<br>Thank you! :)
<p>how to solve this problem?</p><p>C:\Users\H55M-E33\Documents\Arduino\libraries\ESP8266WiFi\src\ESP8266WiFi.cpp:35:22: fatal error: lwip/opt.h: No such file or directory</p><p>#include &quot;lwip/opt.h&quot;</p>
<p>Hi, thanks a lot for the Instructable. Was possible to make it fast and easy.</p><p>I will continue trying to send a e-mail triggered for different changes in Input status and analog values. I have used ESP8266 ESP-01 </p>
<p>This is great Borya. Simple, clear instructions up and running in about 5 minutes. Thanks for the instructable. </p>
<p> Borya, thankyou for this instructable nicely done. I learned a lot, and was successful in sending my first esp8266 based email:). I can see a number of applications for this notification ability , and will keep this code in my toolbox:)</p>
<p>Thanks Cgiff for your feedback! I really happy that people find this Instructable useful.</p>
<p>Awesome. I am curious about what kinds of applications this could be used for.</p><p>pleace tell becous i got a few, also for an alarm system and an adresseble strip, maybe this can put in to also or just as a new project, but i cant think how.</p>
<p>Great! =D</p>
<p>This works, Thank You!</p>
<p>Great job<br>Borya! Your next instructable should be programming the ESP (once uploaded the<br>code) using a Smartphone to change the settings. Like email user/pass, send to,<br>pin# if needs to detect/read something, etc.. basing on a web server ;-)</p>
Very useful instructables. I think I will need this one day ;)
<p>This is very cool. I am curious about what kinds of applications this could be used for.</p>
This can be applied for IoT (Internet of Things) projects. You can connect some sensors on your ESP8266, for example you can code it to send you an email if the aircon or light in your room is still on at 7 a.m. Considering you are living alone and you go to work at 7:10 a.m., then an email will remind you before leaving home :)
<p>I could totally use this as a &quot;you have mail!&quot; mailer for my outdoor mailbox.</p>
<p>Why use it? It may be necessary to refine the idea and send an email to the temperature or voltage? without a lot of home computers.</p>
<p>Thank you for sharing!</p><p>but I have a question</p><p>why I cannot write a function to sent different content and different receive account.</p><p>If I call it twice, the second one will fail. </p><p>void sending(String subject,String receive_email,String content){</p><p> connection_state = WiFiConnect();</p><p> if(!connection_state) // if not connected to WIFI</p><p> Awaits(); // constantly trying to connect</p><p> Gsender *gsender = Gsender::Instance(); // Getting pointer to class instance</p><p>// String subject = &quot;XXXXXx&quot;;</p><p>// String receive_email = &quot;XXXXX@gmail.com&quot;;</p><p>// String content = &quot;&quot;;</p><p>// content += &quot;123&quot;;</p><p>// content += &quot;test &quot;;</p><p>// content += String(millis(), DEC);</p><p> if(gsender-&gt;Subject(subject)-&gt;Send(receive_email, content)) {</p><p> Serial.println(&quot;Message send.&quot;);</p><p> } else {</p><p> Serial.print(&quot;Error sending message: &quot;);</p><p> Serial.println(gsender-&gt;getError());</p><p> } </p><p>}</p>
<p>Hi syokujinau, delete from your function this lines</p><p>connection_state = WiFiConnect();<br> if (!connection_state) // if not connected to WIFI<br> Awaits(); // constantly trying to connect</p><p>They used once in setup() to connect to the router</p>

About This Instructable




More by Borya:ESP8266 GMail sender 
Add instructable to: