Sending Email using Arduino Uno and ESP8266 Wi-Fi Module

We are moving towards the World of Internet of Things (IoT). This technology plays a very important role in the Electronics and Embedded system. Sending an Email from any Microcontroller or Embedded system is very basic thing, which is required in IoT. So in this article, we will learn “How to send an Email using Wi-Fi and Arduino”.

For sending Email via Wi-Fi module and Arduino, first of all we need to have an Email account. So user can create email account at smtp2go.com. After signup, remember your new email address and password.

 

We can understand the whole process in below Steps:

Step 1: First arrange all the required Components.

  • Arduino UNO
  • ESP8266 Wi-Fi module
  • USB Cable
  • Laptop   
  • 16x2 LCD (optional)
  • 10K POT   (optional)
  • Power supply
  • Connecting wires

Wi-Fi Module ESP8266:

ESP8266 Wi-Fi-module

 

Step 2: In this step we will connect ESP8266 Wi-Fi module with Arduino and give it power supply from 3.3v pin of Arduino.

Arduino and Wifi module fritzing-circuit

 

Step 3: In this step, we need to sign-up for Email address and Password (smtp2go.com). smtp2go.com provides the email services, to send the emails using outgoing email server.

Send Email using Arduino smtp2go.com signup

 

Step 4: We will need Username and Password in base64 encoded format with utf-8 character set. For converting the Email and Password in base64 encoded format use below given link. Keep the encoded username and password handy, we need it in our program to login at smtp2go.com.

https://www.base64encode.org/

Send Email using Arduino base64_encode

 

Step 5: Now start writing code for sending Email. ‘Programming Explanation’ and Code is given at the end.

 

Circuit Explanation:

Circuit is very simple, for this project we only need Arduino and ESP8266 Wi-Fi module. A 16x2 LCD is also connected for displaying the status messages. This LCD is optional. ESP8266’s Vcc and GND pins are directly connected to 3.3V and GND of Arduino and CH_PD is also connected with 3.3V.

Tx and Rx pins of ESP8266 are directly connected to pin 2 and 3 of Arduino. And Pin 2 of Arduino is also shorted with Tx pin (Pin 1) of Arduino. This pin is shorted for displaying response of ESP8266 directly on Serial monitor of Arduino. Software Serial Library is used to allow serial communication on pin 2 and 3 of Arduino.

Send Email using Arduino and Wifi Module Circuit Diagram

Note: For watching the response of ESP8266 on Serial Monitor, don’t initialise the Serial.begin(9600) function.

 

Programming Explanation:

1. In programming part first we include libraries and define pins for LCD & software serial communication. By default Pin 0 and 1 of Arduino are used for serial communication but by using SoftwareSerial library, we can allow serial communication on other digital pins of the Arduino.

 

2. void connect_wifi(String cmd, int t) Function is defined to Connect the Wi-Fi module to the Internet.

void connect_wifi(String cmd, int t)
{
  int temp=0,i=0;
  while(1)
  {
    Serial1.println(cmd); 
    while(Serial1.available())
    {
      if(Serial1.find("OK"))
      ... .... ....
      ... .....

3. After it connect module to SMTP server by using given commands:

 lcd.print("Configuring Email..");
  Serial1.println("AT+CIPSTART=4,\"TCP\",\"mail.smtp2go.com\",2525");
  delay(2000);
  Serial1.println("AT+CIPSEND=4,20");
  delay(2000);
  Serial1.println("EHLO 192.168.1.123");
  delay(2000);
  Serial1.println("AT+CIPSEND=4,12");

Note: Many email servers will not accept email from a non-commercial, dhcp-issued ip address.

 

4. Now we will try to Login at smtp2go.co, using the base64 encoded User name and password, which we have derived in Step 4 above.

lcd.print("Try To Login.....");
  Serial1.println("AUTH LOGIN");
  delay(2000);
  Serial1.println("AT+CIPSEND=4,30");
  delay(2000);
  Serial1.println("c2FkZGFtNDIwMUBnbWFpbC5jb20=");     //base64 encoded username
  delay(2000);
  Serial1.println("AT+CIPSEND=4,18");
  delay(2000);
  Serial1.println("Y2lyY3VpdDQyMDE=");                //base64 encoded password

5. After this, enter your main message that user want to send. And send this email by ending line (sending)  ‘.’

  Serial1.println("Testing Success");
  delay(2000);
  Serial1.println("AT+CIPSEND=4,3");
  delay(2000);
  Serial1.println('.');
  delay(10000);
  Serial1.println("AT+CIPSEND=4,6");
  delay(2000);
  Serial1.println("QUIT");
  ... .....
  .......

Other functions and Code are self-explanatory and easy to understand. 

Code: 

#include<LiquidCrystal.h>
LiquidCrystal lcd(14,15,16,17,18,19);

#include <SoftwareSerial.h>
SoftwareSerial Serial1(2, 3); // RX, TX

boolean No_IP=false;
String IP="";

void check4IP(int t1)
{
  int t2=millis();
  Serial1.flush();
  while(t2+t1>millis())
  {
    while(Serial1.available()>0)
    {
      if(Serial1.find("WIFI GOT IP"))
      {
        No_IP=true;
      }
    }
  }
}

void get_ip()
{
  IP="";
  char ch=0;
  while(1)
  {
    Serial1.println("AT+CIFSR");
    while(Serial1.available()>0)
    {
      if(Serial1.find("STAIP,"))
      {
        delay(1000);
        Serial.print("IP Address:");
        while(Serial1.available()>0)
        {
          ch=Serial1.read();
          if(ch=='+')
          break;
          IP+=ch;
        }
      }
      if(ch=='+')
      break;
    }
    if(ch=='+')
    break;
    delay(1000);
  }
  Serial.print(IP);
  Serial.print("Port:");
  Serial.println(80);
}

void connect_wifi(String cmd, int t)
{
  int temp=0,i=0;
  while(1)
  {
    Serial1.println(cmd); 
    while(Serial1.available())
    {
      if(Serial1.find("OK"))
      i=8;
    }
    delay(t);
    if(i>5)
    break;
    i++;
  }
  if(i==8)
  Serial.println("OK");
  else
  Serial.println("Error");
}

void setup()
{
  Serial1.begin(9600);
    // Serial.begin(9600);
     lcd.begin(16,2);
     lcd.print("Sending Email by");
     lcd.setCursor(0,1);
     lcd.print(" Arduino & WIFI ");
     delay(2000);
     lcd.clear();
     lcd.print("  Circuit Digest  ");
     delay(2000);
     lcd.clear();
     lcd.print("Finding ESP8266");
     connect_wifi("AT",100);
     connect_wifi("ATE1",100);
     lcd.clear();
     lcd.print("Connected");
     delay(1000);
     connect_wifi("AT+CWMODE=3",100);
     connect_wifi("AT+CWQAP",100);  
     connect_wifi("AT+RST",5000);
     lcd.clear();
     lcd.print("Connecting WiFi");
     check4IP(5000);
     if(!No_IP)
     {
        Serial.println("Connecting Wifi....");
        connect_wifi("AT+CWJAP=\"1st floor\",\"muda1884\"",7000);    //provide your WiFi username and password here
      }
      else
        {
        }
      lcd.clear();
      lcd.print("WIFI Connected...");
      Serial.println("Wifi Connected");
      delay(1000);
      lcd.clear();
      lcd.print("Getting IP Add.");
      Serial.println("Getting IP Address....");
      get_ip();
      delay(1000);
      lcd.clear();
      lcd.print("IP:");
      lcd.print(IP);
      lcd.setCursor(0,1);
      lcd.print("PORT: 80");
      connect_wifi("AT+CIPMUX=1",100);
      connect_wifi("AT+CIPSERVER=1,80",100);
      delay(2000);
      lcd.clear();
      lcd.print("Configuring Email..");
  Serial1.println("AT+CIPSTART=4,\"TCP\",\"mail.smtp2go.com\",2525");
  delay(2000);
  Serial1.println("AT+CIPSEND=4,20");
  delay(2000);
  Serial1.println("EHLO 192.168.1.123");
  delay(2000);
  Serial1.println("AT+CIPSEND=4,12");
  delay(2000);
  lcd.clear();
  lcd.print("Try To Login.....");
  Serial1.println("AUTH LOGIN");
  delay(2000);
  Serial1.println("AT+CIPSEND=4,30");
  delay(2000);
  Serial1.println("c2FkZGFtNDIwMUBnbWFpbC5jb20=");     //base64 encoded username
  delay(2000);
  Serial1.println("AT+CIPSEND=4,18");
  delay(2000);
  Serial1.println("Y2lyY3VpdDQyMDE=");                    //base64 encoded password
  lcd.clear();
  lcd.print("Login Success");
  delay(2000);
  Serial1.println("AT+CIPSEND=4,34");
  delay(2000);
  Serial1.println("MAIL FROM:<saddam4201@ gmail.com>");  // use your email address
  delay(2000);  
  Serial1.println("AT+CIPSEND=4,32");
  delay(2000);
  lcd.clear();
  lcd.print("Seniding Email 2");
  lcd.setCursor(0,1);
  lcd.print("Saddam4201@ gmail");
  Serial1.println("RCPT To:<saddam4201@ gmail.com>");
  delay(2000);
  Serial1.println("AT+CIPSEND=4,6");
  delay(2000);
  Serial1.println("DATA");
  delay(2000);
  Serial1.println("AT+CIPSEND=4,24");
  delay(2000);
  Serial1.println("Testing Success");
  delay(2000);
  Serial1.println("AT+CIPSEND=4,3");
  delay(2000);
  Serial1.println('.');
  delay(10000);
  Serial1.println("AT+CIPSEND=4,6");
  delay(2000);
  Serial1.println("QUIT");
  delay(2000);
  lcd.clear();
  lcd.print("Email Sent...");
}

void loop()
{
   
}

Video: 

Comments (29)

  • Rohit Rajapure's picture
    Rohit Rajapure

    Hello Saddam,
    Thank you for posting this project.
    I'm new to the ESP8266 and ARDUINO UNO, Can you please share the step by step procedure followed by you during the project.

    Thanks in advance

    Apr 05, 2016
  • harish kumar's picture
    harish kumar

    hello saddam hsmtp3go is very important to open the account sir

    Apr 19, 2016
  • Nate's picture
    Nate

    It doesn't work :(

    May 10, 2016
  • Maddy's picture
    Maddy

    What problem are you facing? Please follow the tutorial carefully.

    May 21, 2016
  • Keshav dev's picture
    Keshav dev

    Hi,

    I am beginner for embedded systems and working on IoT. I have interfaced arduino uno with ESP8266 wifi module and 16*2 LCD. Now i have to type text in textbox of HTML page and hen i click on submit button ,it should get displayed on LCD.

    Please reply

    Thanks in advance

    May 10, 2016
  • Yoann27's picture
    Yoann27

    Hi, my LCD ago marked 'GET WIFI IP "and the program runs in a loop by giving me the IP address of the wireless card or something else. Why?

    May 22, 2016
  • Maddy's picture
    Maddy

    Have you connected to your Wi Fi, remember to replace your wifi's username and password, here in the line:

    connect_wifi("AT+CWJAP=\"1st floor\",\"muda1884\"",7000); 

    Aug 20, 2016
  • FRANKLIN BONFIM's picture
    FRANKLIN BONFIM

    I have this problem and I am not getting correct.

    "
    [...]
    > IPSEND=4,34

    busy s...

    Recv 18 bytes

    SEND OK

    +IPD,4,35:535 Incorrect authentication data
    MAIL FROM:<fklbonfim@gmail.com>

    ERROR
    AT+CIPSEND=4,32

    OK
    > T+CIPSEND=4,6

    busy s...

    Recv 32 bytes

    SEND OK

    +IPD,4,26:500 unrecognized command
    AT+CIPSEND=4,24

    OK
    > END=4,3

    busy s...

    Recv 24 bytes

    SEND OK

    +IPD,4,26:500 unrecognized command
    AT+CIPSEND=4,6

    OK
    >
    Recv 6 bytes

    SEND OK

    +IPD,4,26:500 unrecognized command"

    Jul 18, 2016
  • Hi!!'s picture
    Hi!!

    Hi!!! How did you solve this problem?

    Dec 27, 2016
  • James's picture
    James

    Hi!
    Having fun working this through. I don't have an LCD, and you mention "Note: For watching the response of ESP8266 on Serial Monitor, don’t initialise the Serial.begin(9600) function.". Can someone explain how to do that in the code?
    MANY thanks!

    Aug 30, 2016
  • Maddy's picture
    Maddy

    Means you should not use default Rx and Tx pins (0 and 1) of Arduino for serial communication, instead use 2 and 3 pin as Rx Tx using SoftwareSerial.h Library and short the Pin 1 and 2 to get the response on Serial Monitor as done in this tutorial.

    Sep 21, 2016
  • Marco's picture
    Marco

    Hi,
    thanks for writing this tutorial. But i got a problem.

    Im always stuck on "Getting IP Add."
    The eps is visible in my router overview and also got an ip adress.

    AT+CIFSR

    192.168.4.1
    192.168.178.48

    OK

    but it wont stop asking for a ip adress.
    What am i doing wrong?

    Sep 09, 2016
  • Apostolis's picture
    Apostolis

    Thank you sir.. I can confirm that this is working. Simple and easy..just follow the steps. I set it up to sent me an email when there is an alarm trigger at home.I wrote the commands in an progmem array and used a for function to call them so they dont use ram.The only problem is that the email applications dont refresh/synchronise on smartphones to often.

    Sep 13, 2016
  • Bub's picture
    Bub

    If your phone doesn't check mail too often, send the email to a email-to-SMS gateway. Most cell providers have a way to do that. Research it.

    Sep 17, 2016
  • CLAUDIO's picture
    CLAUDIO

    Very goog job ......BUT....

    if i give the AT command by hand for the '.' (DOT, for terminate the text of the email) in the serial monitor works and send me an email

    BUT if i put this in the code doesn't work see below....please help me thanks a lot

    Serial1.println("AT+CIPSEND=3");
    delay(2000);
    Serial1.println('.');

    Sep 20, 2016
  • CLAUDIO's picture
    CLAUDIO

    so when you write in serial monitor AT command you must digit AT+CIPSEND=3 and it works

    but when you write in your code you must write this
    Serial1.println("AT+CIPSEND=4,3");

    Sep 21, 2016
  • Rodrigo Cavalheiro's picture
    Rodrigo Cavalheiro

    OK
    AT+CIFSR

    192.168.4.1
    192.168.1.3

    OK
    AT+CIFSR

    192.168.4.1
    192.168.1.3

    What should I do??

    Sep 24, 2016
  • Ris's picture
    Ris

    I get this in my serial monitor:

    ón't use rtc mem data
    rl�‚slÌÿ
    Ai-Thinker Technology Co.,Ltd.

    invalid
    WIFI CONNECTED
    WIFI GOT IP
    AT+CWJAP="Redmi","efdfdf"

    WIFI DISCONNECT
    AT+CWJAP="Redmi","efdfdf"

    busy p...
    AT+CWJAP="Redmi","efdfdf"

    busy p...
    +CWJAP:1

    FAIL
    AT+CWJAP="Redmi","efdfdf"

    WIFI CONNECTED
    WIFI GOT IP

    OK
    AT+CWJAP="Redmi","efdfdf"

    WIFI DISCONNECT
    AT+CIFSR

    busy p...
    AT+CIFSR

    busy p...
    AT+CIFSR

    busy p...
    AT+CIFSR

    busy p...
    AT+CIFSR

    busy p...
    +CWJAP:1

    FAIL
    AT+CIFSR

    +CIFSR:APIP,"192.168.4.1"
    +CIFSR:APMAC,"1a:fe:34:99:87:c7"
    +CIFSR:STAIP,"0.0.0.0"
    +CIFSR:STAMAC,"18:fe:34:99:87:c7"

    and thereafter just the following text repeatedly, with no more progress.

    OK
    AT+CIFSR

    +CIFSR:APIP,"192.168.4.1"
    +CIFSR:APMAC,"1a:fe:34:99:87:c7"
    +CIFSR:STAIP,"0.0.0.0"
    +CIFSR:STAMAC,"18:fe:34:99:87:c7"

    OK
    AT+CIFSR

    +CIFSR:APIP,"192.168.4.1"
    +CIFSR:APMAC,"1a:fe:34:99:87:c7"
    +CIFSR:STAIP,"0.0.0.0"
    +CIFSR:STAMAC,"18:fe:34:99:87:c7"

    ...

    What should I do?

    Nov 15, 2016
  • karim's picture
    karim

    hi saddam ,i dont know where i put my username and password in this:
    connect_wifi("AT+CWJAP=\"1st floor\",\"muda1884\"",7000);

    Nov 28, 2016
  • Maddy's picture
    Maddy

    1st floor is user name and muda1884 is password, replace them with yours.

    Dec 10, 2016
  • Manoj's picture
    Manoj

    Very helpful information.

    Dec 19, 2016
  • Pranav Sharma's picture
    Pranav Sharma

    I am getting some error in SoftwareSerial Serial1 saying conflicting declaration ''SoftwareSerial Serial1'
    I am using arduino mega 2560

    Dec 22, 2016
  • rajani's picture
    rajani

    i got a Incorrect authentication data error

    Dec 28, 2016
  • mosab's picture
    mosab

    hello mr saddam
    thank for your info
    i have tried set the arduino to send distance from ultrasonic sensor to webpage
    but at first i would like to check if my ESP8266 coneccted properly
    i have used your code but when i run it i got thoese on the computer secree :

    AT
    AT
    AT
    AT
    AT
    AT
    AT
    ATE1
    ATE1
    ATE1
    ATE1
    ATE1
    ATE1
    ATE1
    AT+CWMODE=3
    AT+CWMODE=3
    AT+CWMODE=3
    AT+CWMODE=3
    AT+CWMODE=3
    AT+CWMODE=3
    AT+CWMODE=3
    AT+CWQAP
    AT+CWQAP
    AT+CWQAP
    AT+CWQAP
    AT+CWQAP
    AT+CWQAP
    AT+CWQAP
    AT+RST
    AT+RST
    AT+RST
    AT+RST
    AT+RST
    AT+RST
    AT+RST
    can you please let me know the reason and how to solve this or anyone can guide me on that ?

    Dec 29, 2016
  • Mario's picture
    Mario

    Hello mr saddam thank for your info
    i have this problem
    +IPD,4,182:250-mail.smtp2go.com Hello 192.168.1.123 [187.163.212.60]
    250-SIZE 52428800
    250-8BITMIME
    250-DSN
    250-PIPELINING
    250-AUTH CRAM-MD5 PLAIN LOGIN
    250-STARTTLS
    250-PRDR
    250 HELP
    AT+CIPSEND=4,12
    OK
    >
    Recv 12 bytes
    SEND OK
    +IPD,4,18:334 VXNlcm5hbWU6
    AT+CIPSEND=4,30
    OK
    >
    Recv 30 bytes
    SEND OK
    +IPD,4,18:334 UGFzc3dvcmQ6
    AT+CIPSEND=4,18
    OK
    >
    Recv 18 bytes
    SEND OK
    +IPD,4,30:235 Authentication succeeded
    AT+CIPSEND=4,34
    OK
    > T+CIPSEND=4,32
    busy s...
    Recv 34 bytes
    SEND OK
    +IPD,4,8:250 OK
    RCPT To:<ggbathory@gmail.com>
    ERROR
    AT+CIPSEND=4,6
    OK
    >
    Recv 6 bytes
    SEND OK
    +IPD,4,26:500 unrecognized command
    AT+CIPSEND=4,24
    OK
    > END=4,3
    busy s...
    Recv 24 bytes
    SEND OK
    +IPD,4,26:500 unrecognized command
    .
    ERROR
    AT+CIPSEND=4,6
    OK
    >
    Recv 6 bytes
    SEND OK
    +IPD,4,26:500 unrecognized command

    Jan 01, 2017
  • Shaheer's picture
    Shaheer

    Sir I found your tutorial very helpful. I have encountered a slight problem. When i send the EHLO command i dont get any response. Is there any need for port forwarding before i make this connection? Or is it something else?
    Thank you.

    Jan 06, 2017
  • piyush charpe's picture
    piyush charpe

    hello ,
    your privious projects were really awesome, i m done with this project can u please make a tutorial on how to make a call using esp8266.
    it will be very helpfull to explore IOT more.

    Jan 14, 2017

Leave a comment