Search
Close this search box.

How to Send an Email using ESP8266 WiFi Module?

In this project, I will show you how to Send an Email using ESP8266. For this project, I will be using the DFRobot FireBeetle ESP8266 IoT Board. Sending an e-mail with the help of the ESP8266 Module will be a step forward in the IoT Implementation.

Introduction

With Internet of Things (IoT) expanding, the scope of the IoT applications is growing from controlling appliances to monitoring devices (like sensors) and sending e-mails.

By sending e-mails from you ESP8266 Module, you can know the status of any sensor you are monitoring or you can receive an emergency e-mail in case of burglary or an intruder alert.

So, without further delay, let me show you all the steps required to Send an Email using ESP8266 WiFi Module.

ESP8266 Board

As said earlier, I will be using the DFRobot FireBeetle ESP8266 board in this project to send e-mails. You can use any ESP8266 Module and I have tested this procedure with my ESP-01 ESP8266 Board and it worked just fine.

DFRobot FireBeetle ESP8266 Review FireBeetle

Since the DFRobot FireBeetle ESP8266 board has all the required components like the MicroUSB Port, 3.3V Regulator, etc. I don’t need to make any extra connections with respect to the board.

All I need to do is to plug-in a MicroUSB cable in the port and connect it to a computer (with the drivers being already installed and Arduino IDE is already setup).

This is one of the main reasons for choosing the DFRobot FireBeetle ESP8266 board over my regular ESP-01 Module.

So, I suggest you to go through the hook-up guide of the DFRobot FireBeetle ESP8266 board as described in the following project: DFROBOT FIREBEETLE ESP8266 REVIEW AND HOOK-UP GUIDE.

SMTP Server Setup

In order to send e-mails from ESP8266 Module, you need to follow the SMTP protocol. Hence, an SMTP Server is required to send the e-mails and the ESP8266 will act as an SMTP Client.

I have tried the Gmail’s SMTP Settings several times to send email using ESP8266 but it was not fruitful. So, I have decided to use a third-party SMTP Server and I found “SMTP2GO” as a reliable choice.

Send an Email using ESP8266 SMTP2GO Home

So, go to the SMTP2GO website and register with a free account. After creating an account, the first step is to create the SMTP Username and SMTP Password. In fact, the moment you confirm your e-mail address and first login to SMTP2GO, this is the information that you’ll get.

Leave the username as it is i.e. the e-mail address and change the SMTP Password with your own choice. Note that this is SMTP Password and is different from the SMTP2GO Login Password.

Make a note of the two i.e. SMTP Username and SMTP Password.  

Now, enter the dashboard of the SMTP2GO app and on the left access bar, click on “Settings” and then on “Users”.

In that, you can see the information regarding the SMTP Server and PORT number. It is usually as follows:

  • SMTP Server: mail.smtp2go.com
  • SMTP PORT: 2525

Send an Email using ESP8266 SMTP2GO Server

Note down this information as you need to use this data in the code.

Encoding Username and Password

You need to encode your SMTP Username and SMTP Password to Base64 format with ASCII Character Set. For this, you can either use an Arduino Library or a website called BASE64ENCODE.

Send an Email using ESP8266 Base64 Encode

Enter your SMTP Username and Password separately and note the encoded content. For example, if your email address is test@gmail.com, it would be encoded as dGVzdEBnbWFpbC5jb20=.

And if your password is “testpassword” (excluding the quotes), it would be encoded as dGVzdHBhc3N3b3Jk.  

Code

Now, let me show you the necessary code in order to Send an Email using ESP8266.

In the code, make necessary changes like SSID, Password of your WiFi Connection, Sender and Receiver E-Mail Address, Base64 Encoded SMTP Username and SMTP Password.

Explanation of the Code

The initial section of the code is simple used to connect the ESP8266 WiFi Module to the Internet. If the connection is successful, you will get an IP Address.

Next is the actual code for sending the e-mail. The code can be divided into 8 stages.

Stage 1: In stage 1, you will be connecting to the SMTP Server on a PORT. This is done using the command client.connect(SMTP_SERVER, SMTP_PORT). In response, you will get a 220 code.

Stage 2: Greet the SMTP Server with EHLO Command (Previously HELO Command). The command is client.println(“EHLO www.example.com”);

If the connection is successful, you will get a 250 Response Code.

Stage 3: Stage 3 is to authorize the user with AUTH LOGIN Command. The command is client.println(“AUTH LOGIN”);. You will get a Response 334 for success.

Stage 4: Send the encoded SMTP Username and Password one after the other. The commands are client.println(“Base64, ASCII encoded username”); and client.println(“Base64, ASCII encoded password”);.

If the authentication is success, you will get a 235 Response.

Stage 5: Now its time to send the mail from string where you have to enter the sender’s email address using the format MAIL FROM:<”+String(from)+’>’;.

The command is client.println(“MAIL From: sender@gmail.com”);

Stage 6: Then send the rcpt to string using the format RCPT TO:<”+String(to)+’>’;. The command is client.println(“RCPT To: receiver@gmail.com”);.

Stage 7: Send the “DATA” followed by the Message body of the e-mail. The commands are

client.println(“DATA”);

client.println(“To:  receiver@gmail.com”);

client.println(“From: sender@gmail.com”);

client.println(“Subject: ESP8266 test e-mail\r\n”);

client.println(“This is is a test e-mail sent from ESP8266.\n”);

client.println(“Second line of the test e-mail.”);   

stage 8: Finally, terminate the mail with “.” and send the quit command.

client.println(“.”);

client.println(“QUIT”);

All these commands and responses will be shown in the serial monitor of the Arduino IDE.

Send an Email using ESP8266 Output

If you follow all the mentioned steps, you will be able send an email using ESP8266 successfully.       

 

18 Responses

    1. The line at the end of the “setup” section, “byte ret = sendEmail();” calls the email send function (the big bit below), which has built in data to send. If you were to put some sort of program in the loop, that would detect a button or sensor, you could call the line then, and it should work just fine.

      1. Hi thank for this snippet this is the only email code that worked first time, the issue I have is getting a button press to send working, I assume it’s all down to the “byte ret = sendEmail()” being called and my lack of c++ experience, do you have any example code as a guide, I’ve tried just putting in an if/else in the void loop. Maybe you could suggest a good tutorial that doesn’t gloss over so many of the why this error, I learned on a Z80 2k ram and seven segment display machine back in the 80s and programmed in hex on a 4×4 keypad, no abstraction layers just a book a PCB and me…..now I’m starting from scratch again…frustrations abounds but I love the smell of flux in the morning.

    2. Hi,
      I’m a newby of arduino world so excuse me if this is a dumb question.
      I don’t underderstand if the code above, must reside on the esp8266 wifi module or onto Arduino board.
      Can you help me ?
      Thanks

  1. I’m using this same code. But I get error like “550 that smtp username’s account is not allowed to send ”

    Please help me to solve this issue

  2. Same problem here “550 that smtp username’s account is not allowed to send ”
    this problem is because of not adding sender domain in SMTP2go dashboard…

    but i dont have my own sender domain.. Is there any alternative to SMTP2go without using sender domain
    Can you please help ???
    Thanks in advance

  3. what email did you use in this line “(“RCPT To: receiver@gmail.com”)”? i got some error in this line! the error is “550 that smtp username’s account is not allowed to send ”

    please help to solve this problem, please

  4. i’m getting the same error,

    Sending From
    250 OK
    Sending To
    550 that smtp username’s account is not allowed to send

  5. Hi~
    I have the same problem.

    Sending To
    550 that smtp username’s account is not allowed to send

    Can you help me?

  6. I solve the problem :
    send an email to the smtp2go service and explain your problem (.”.. I use it for IOT services on an ESP Microcontroler…, please alow the use witout own mail server account….”)

    greetings
    Juergen

    1. Many Thanks, 3 days I try to send email, and it was not working, I just asked them (21h35 GMT) and they reply in a few minute and open it for me, everything ok now, many thanks again (y) 🙂

  7. Very good tutorial. Good code, works.
    Error 550: Need a domain name or ask from smtp2go to allow sending without a domain name.

  8. Hi,

    I saw several few peoples has been asking for this error
    ” 550 that smtp username’s account is not allowed to send ”
    can you help to give solution for all of us ? Thank you very much

  9. got ” 550 that smtp username’s account is not allowed to send ” and contacted them mentioning even that it is about a university project and gave them my uni mail, no matter, their response was

    “Since you’ve signed up for a free account with a free email address, we require the “Settings > Sender Domains” section be setup and verified before you’ll be permitted to start sending mail through your account. This is a measure we’ve taken to reduce spam as well as improve the deliverability of your outbound mail.

    If you don’t have your own domain, you’ll need to find an alternative SMTP provider.”

    now i am trying to send email using google SMTP server…

  10. Took a little tweaking but got it to work. Biggest problem was the emails going to the SPAM folder. Found them there, fixed the problem and, n ow, perfect. Thanks for the excellent tutorial. It got me working.

Leave a Reply

Your email address will not be published. Required fields are marked *