element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs Send an Email from ESP8266
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: neilk
  • Date Created: 22 Feb 2019 4:27 PM Date Created
  • Views 3275 views
  • Likes 9 likes
  • Comments 6 comments
  • arduino_tutorials
  • esp8266
  • smtp
  • email
Related
Recommended

Send an Email from ESP8266

neilk
neilk
22 Feb 2019

Background

 

In previous blogs, I have described my investigations into using an 18650 3.7V 2400mAh battery to power a Wemos d1 mini ESP8266, via a Wemos battery shield, which plugs directly into the d1 mini, just like an Arduino shield.

 

While waiting for my latest long-term power consumption tests to complete, I wondered if it might be possible to send an Email from an ESP8266, to alert me that the battery voltage is reaching a critical level, or perhaps, just to confirm that the battery powered device is still functioning.

 

Mr Google soon confirmed that it was possible and I quickly located this particular article, which is from the same source that I have used before in my previous blogs: https://arduinodiy.wordpress.com/2016/12/28/sending-mail-with-an-esp8266/

 

It seemed to be the most straightforward of the articles I found, and, based on my success using the author's other articles, was likely to be the most reliable!

 

Basic Principles

 

At first I assumed that I would need access to a mail server - possibly one running on my own webserver. I soon discovered that this is not the case: instead I can use the services of the wonderful people at http://SMTP2GO.com Through them you can get your ESP8266 sketch to send an Email seeming to come from an imaginary user but I think it has to be from a real domain - the article used yrmail@gmail.com. This is perfectly OK, provided you are only sending the emails to yourself!

 

As directed by the article, I signed up for a free account with SMTP2GO, which allows me to send up to a 1000 emails a month, with an HOURLY limit of 25 emails - that should be more than enough for my purposes!

 

Once you have logged in, you need to setup an SMTP username and password - SMTP2GO suggests a username and password, which I was happy to accept - the username they suggested is the gmail address I registered on the site and the password is a string of 12 characters - upper and lower case alpha, and numeric.

 

Before these can be used in the sketch, they BOTH have to be encoded in base 64.There is an online encoder here: https://www.base64encode.org/

 

So myname@gmail.com encodes to bXluYW1lQGdtYWlsLmNvbQ== Similarly, a password like aAbBb12fg168 would encode to YUFiQmIxMmZnMTY4

 

I had already determined from looking at the remainder of the setup process that I seemed to need a domain, but I do not have one. I read all the articles on the website and couldn't see what to do next/

 

So I did what I usually do - loaded up my sketch, modified to the best of my ability, and pressed go, metaphorically. Of course it failed, but the failure mode gave me something to send to SMTP2GO support.

 

Teresa at SMTP2GO

 

This is what I wrote:

--------------------------------------------------------------------------------------------

I am trying to send an email from an ESP8266 program published here:
https://arduinodiy.wordpress.com/2016/12/28/sending-mail-with-an-esp8266/

My program fails with the following dialog snippet:
.....
235 Authentication succeeded
Sending From

250 OK
Sending To
550 that smtp username's account is not allowed to send
.....
I don't know what to put as the Sending From parameter - the example uses yrmail@gmail.com, but says this is unimportant. However, it gives an error I have tried a valid gmail address and a valid hotmail address.

Please can you help?

Neil Kenyon

-------------------------------------------------------------------------------------

I had a reply very quickly:

-------------------------------------------------------------------------------------

 

Hi there,

 

Thank you for reaching out to us. image

 

You have not yet finished setting up your account. You will need to add your domain name to the Sender Domains section of the dashboard. This will include setting up an SPF record and a DKIM record so that we have permission to send on your behalf.

 

If you do not own a domain, please answer the following:

 

1. What is the nature of your business/ what product or service are you going to provide?

 

2. Who will you be sending emails to? Do you have a mailing list? If so, how was it compiled?

 

3. Do you have a website? If so, please provide me with the URL.

Best regards,
Teresa at SMTP2GO

--------------------------------------------------------------------------------------------------------------------------------------------

 

This is my reply:

----------------------------------------------------------------------------------------------------------------------------

Hi Teresa,

Thank you for your quick response.
I do not own a domain
1 I am a hobbyist; this is not a commercial system
2 I only want to send emails to myself - to alert me to problems with my home built IOT devices
3 I do not have a website
I hope you can help me
Many thanks

Neil

---------------------------------------------------------------------------------------------------

And finally:

-----------------------------------------------------------------------------------------------------------------

Hi Neil,

 

I have activated your account to send.

 

Let me know how you get on. image

Best regards,

Teresa at SMTP2GO

---------------------------------------------------------

 

The whole process, from placing the initial request for support, to having a working system, took less than 25 minutes! Now that's what I call SUPPORT!

 

My first attempt at sending an Email from my ESP8266 used yrmail@gmail.com as the sender address and it arrived OK in my gmail inbox. However, when I changed the sender address to IOT60@gmail.com, that proved to be very suspicious and it went to spam.

 

Outline of the Sketch

 

  1. Configure and initialise ESP8266 Wifi. I prefer to assign an IP address  rather than let DHCP in my router do it; it's much faster.
  2. Connect and authenticate on local WiFi network.
  3. Connect to the smtpcorp server
  4. Login with SMTP username and password - the base64 ASCII encoded versions as described above.
  5. Pass the sender and recipient Email addresses to the SMTP2GO server
  6. Pass the Email to the server - this includes the usual To:, From, Subject: and a Body.
  7. Disconnect from the SMTP2GO server

 

Code Snippet to show how to connect to smtpcorp Server

 

in the following snippet, the parameter "server" has been previously defined as   char server[] = "mail.smtpcorp.com";

 

  if (client.connect(server, 2525) == 1) {
    Serial.println(F("connected"));
  } else {
    Serial.println(F("connection failed"));
    return 0;
  }
  if (!eRcv()) return 0;


  Serial.println(F("Sending EHLO"));
  client.println("EHLO www.example.com");
  if (!eRcv()) return 0;

 

 

Code Snippet to show how to login with base64 credentials

 

  Serial.println(F("Sending auth login"));
  client.println("auth login");
  if (!eRcv()) return 0;
  
  Serial.println(F("Sending User"));  
  client.println("Your base64 encoded smtp username");  // base64, ASCII encoded SMTP username, NOT SMTP2GO account username!
  if (!eRcv()) return 0;

  Serial.println(F("Sending Password"));
  client.println("Your base64 encoded smtp password");  // base64, ASCII encoded SMTP password, NOT SMPTP2GO account password!
  if (!eRcv()) return 0;

 

 

Code Snippet to show how Sender and Recipient Email Addresses are sent

 

  Serial.println(F("Sending From")); 
  client.println(F("MAIL From: yrmail@gmail.com")); // Not a real address
  if (!eRcv()) return 0; 
  
  Serial.println(F("Sending To"));
  client.println(F("RCPT To: youractualEmail@gmail.com"));  //Your actual email address to receive the email
  if (!eRcv()) return 0;

 

Code Snippet to show how the Header and Body of the Email are sent

 

  client.println(F("DATA"));            // Tells the server to expect DATA - ie the Email
  if (!eRcv()) return 0;
    
  Serial.println(F("Sending email"));
  // Email Header
  client.println(F("To: youractualEmail@gmail.com"));   // Your actual email address to receive the email
  client.println(F("From: yrmail@gmail.com"));
  client.println(F("Subject: Email from ESp8266\r\n"));
  
  // Email Body
  client.print(F("Vcc Voltage"));
  client.print(ESP.getVcc());
  client.println(F("mV"));
  
  client.print(F("Device ID: "));
  client.println(ESP.getChipId());


  // this terminates the email
  client.println(F(".")); 
  
  if (!eRcv()) return 0;
 

 

 

Code Snippet to show how the Sketch disconnects from the Server

 

  if (!eRcv()) return 0;
  
    Serial.println(F("Sending QUIT"));
    client.println(F("QUIT"));
    if (!eRcv()) return 0;
    
    client.stop();
    Serial.println(F("disconnected"));
    return 1;

 

I hope the attached complete sketch works - any problems, give me a shout.

Attachments:
ESP8266_send_email_anonymous.zip
  • Sign in to reply

Top Comments

  • mp2100
    mp2100 over 4 years ago +3
    Neil, Very nice write up and very useful. I've bookmarked it because I'm working my way through several other projects on other systems. I'd like to monitor my sump pump and send an email if there's a…
  • genebren
    genebren over 4 years ago +2
    Great blog! These are some very useful findings and solutions, providing a nice path between small, WiFi enabled processor and your home PC. Well done. I will have to revisit this if I ever need to do…
  • neilk
    neilk over 4 years ago in reply to genebren +2
    Thanks , Gene. Someone else did all the hard work! As I've said in another thread: I'm always happy to ride on the back of people cleverer than me Neil
  • neilk
    neilk over 4 years ago in reply to jemssmith

    I assume your comment was rejected because it looks like a blatant advertisement for the organisation you represent.

     

    if you want to post something, a more detailed account of how to send an email from an ESP8266 device might get through.

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jemssmith
    jemssmith over 4 years ago
    This comment was deleted.
    • Cancel
    • More
    • Cancel
  • neilk
    neilk over 4 years ago in reply to mp2100

    Hi Allen

     

    Thanks very much.

     

    Nice to know that you probably have a practical application for this. I hope it goes well and all your other projects.

     

    Please post accounts of them them on E14  image

     

    Neil

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mp2100
    mp2100 over 4 years ago

    Neil,

    Very nice write up and very useful.  I've bookmarked it because I'm working my way through several  other projects on other systems.  I'd like to monitor my sump pump and send an email if there's a high level. 

    Allen

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 4 years ago in reply to genebren

    Thanks , Gene.

     

    Someone else did all the hard work!  As I've said in another thread: I'm always happy to ride on the back of people cleverer than me

     

    Neil

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube