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. <img src='https://www.element14.com/community//files1.element14.com/community/9.0.4.edd1c5d/images/emojis/1f603.png' class='jive_emoji'/>

 

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. <img src='https://www.element14.com/community//files1.element14.com/community/9.0.4.edd1c5d/images/emojis/1f604.png' class='jive_emoji'/>

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.