Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

WiFi Email client

If you want to use SMTP2GO for a SMTP email server, the code is the second example below. It requires registering with SMTP2GO to get a password to send email.

There are several changes you must do to the code. They are indicated by comments in the code.

Many email servers will not accept email from a non-commercial, dhcp-issued ip address. If that is the case, the server will send an error with a statement to that fact in the error message.

The following sketch provides the basic code to enable the developer to test the ability to establish communications to their mailserver and back.

Well thanks to Tim, we successfully were able to send each other an email. Almost a perfect 10, with a small issue in knowing and using the 'other' guys email server. However, for my requirement to have the "official Arduino WiFi Shield" send me an email from the remote location, this WORKS! You may use the network command NSLOOKUP to find your email server address. If you setup your email and use SMTP, it'll be the SMTP server name. We ran into an issue with the WiFi.h library not using the device MAC address, so some destinations will bounce you immediately as a spammer. (nice). Anyway, if you want to have a good working sketch to try, here you go. Many, many thanks to SurferTim on this one.

/*  THIS ONE WORKS!  Output below is from Serial Monitor
Ready. Press 'e' to send.
connected
220 mail.yourmailserver.com ESMTP Sendmail 8.13.6/8.13.1; Sun, 17 Mar 2013 12:59:58 +0000
Sending helo
250 mail.yourmailserver.com Hello [38.123.84.242], pleased to meet you
Sending From
250 2.1.0 <me@mine.com>... Sender ok
Sending To
250 2.1.5 <you@mine.com>... Recipient ok
Sending DATA
354 Enter mail, end with "." on a line by itself
Sending email
250 2.0.0 r2HCxwgc027560 Message accepted for delivery
Sending QUIT
221 2.0.0 mail.yourmailserver.com closing connection
disconnected
Email sent

  1. //    Email client sketch for IDE v1.0.3 and Ethernet Shield
  2. //    Posted December 2012 by SurferTim
  3. //    Modified 17 March 2013 for WiFi Shield by
  4. //    MisterResistor and SurferTim
  5. #include <SPI.h>
  6. #include <WiFi.h>
  7.  
  8. // Setup on a 128 bit WEP network, change to suit for WPA
  9.  
  10. char ssid[] = "MONKEY";      //  your network SSID (name)
  11. char pass[] = "C329888B2B00225566778899AA";   // your network password
  12. int keyIndex = 1;    // your network key Index number (needed only for WEP)
  13.  
  14. IPAddress ip( 192, 168, 1, 12 );    // ipaddress obtained from access point
  15. IPAddress gateway( 192, 168, 1, 1 );
  16. IPAddress subnet( 255, 255, 255, 0 );
  17.  
  18. char server[] = "mail.yourmailserver.com";  // smtp mail server  (mine.com)
  19.  
  20. int status = WL_IDLE_STATUS;
  21.  
  22. WiFiClient client;
  23.  
  24. void setup()
  25.  {
  26.  Serial.begin(9600);
  27.  pinMode(9,OUTPUT);          // changed to use onboard LED
  28.  digitalWrite(9,HIGH);
  29.  WiFi.begin(ssid, keyIndex, pass);
  30.  delay(2000);
  31.  Serial.println("Ready. Press 'e' to send.");
  32.  }
  33.  
  34. void loop()
  35.  {
  36.    byte inChar;
  37.    inChar = Serial.read();
  38.  
  39.   if(inChar == 'e')
  40.    {
  41.        if(sendEmail()) Serial.println("Email sent");    
  42.        else Serial.println("Email failed");
  43.    }      
  44.  }
  45.  
  46. byte sendEmail()
  47.  {
  48.    byte thisByte = 0;
  49.    byte respCode;
  50.  
  51.   if(client.connect(server,25)) {
  52.      Serial.println("connected");
  53.    } else {
  54.      Serial.println("connection failed");
  55.      return 0;
  56.    }
  57.  
  58.   if(!eRcv()) return 0;
  59.    Serial.println("Sending helo");
  60.  
  61. // change to your public ip
  62.    client.write("helo 1.2.3.4\r\n");
  63.  
  64.   if(!eRcv()) return 0;
  65.    Serial.println("Sending From");
  66.  
  67. // change to your email address (sender)
  68.    client.write("MAIL From: <me@mine.com>\r\n");
  69.  
  70.   if(!eRcv()) return 0;
  71.  
  72. // change to recipient address
  73.    Serial.println("Sending To");
  74.    client.write("RCPT To: <you@mine.com>\r\n");
  75.  
  76.   if(!eRcv()) return 0;
  77.  
  78.   Serial.println("Sending DATA");
  79.    client.write("DATA\r\n");
  80.  
  81.   if(!eRcv()) return 0;
  82.  
  83.   Serial.println("Sending email");
  84.  
  85. // change to recipient address
  86.    client.write("To: You <you@mine.com>\r\n");
  87.  
  88. // change to your address
  89.    client.write("From: Me <me@mine.com>\r\n");
  90.  
  91.   client.write("Subject: Arduino email test\r\n");
  92.   client.write("This is from my Arduino WiFi shield!\r\n");
  93.   client.write(".\r\n");
  94.  
  95.   if(!eRcv()) return 0;
  96.  
  97.   Serial.println("Sending QUIT");
  98.   client.write("QUIT\r\n");
  99.  
  100.   if(!eRcv()) return 0;
  101.  
  102.   client.stop();
  103.  
  104.   Serial.println("disconnected");
  105.  
  106.   return 1;
  107.  }
  108.  
  109. byte eRcv()
  110. {
  111.   byte respCode;
  112.   byte thisByte;
  113.   int loopCount = 0;
  114.  
  115.   while(!client.available()) {
  116.     delay(1);
  117.     loopCount++;
  118.  
  119.     // if nothing received for 10 seconds, timeout
  120.     if(loopCount > 10000) {
  121.       client.stop();
  122.       Serial.println("\r\nTimeout");
  123.       return 0;
  124.     }
  125.   }
  126.  
  127.   respCode = client.peek();
  128.  
  129.   while(client.available())
  130.    {  
  131.      thisByte = client.read();    
  132.      Serial.write(thisByte);
  133.    }
  134.  
  135.   if(respCode >= '4')
  136.    {
  137.      efail();
  138.      return 0;  
  139.    }
  140.  
  141.   return 1;
  142.  }
  143.  
  144. void efail()
  145. {
  146.   byte thisByte = 0;
  147.   int loopCount = 0;
  148.  
  149.   client.write("QUIT\r\n");
  150.  
  151.   while(!client.available()) {
  152.     delay(1);
  153.     loopCount++;
  154.  
  155.     // if nothing received for 10 seconds, timeout
  156.     if(loopCount > 10000) {
  157.       client.stop();
  158.       Serial.println("\r\nTimeout");
  159.       return;
  160.     }
  161.   }
  162.  
  163.   while(client.available())
  164.   {  
  165.     thisByte = client.read();    
  166.     Serial.write(thisByte);
  167.   }
  168.  
  169.   client.stop();
  170.  
  171.   Serial.println("disconnected");
  172. }


SMTP code for SMTP2GO

You must get an account with SMTP2GO to use this example code.

The user and password must be base64 encoded. You can do that here. https://www.base64encode.org/

  1. /*
  2.    Email client sketch for WiFi shield
  3.    Posted 29 May 2015 by SurferTim
  4. */
  5. #include <SPI.h>
  6. #include <WiFi.h>
  7.  
  8. WiFiClient client;
  9. char ssid[] = "MySSID";     // the name of your network
  10. char pass[] = "mypassphrase";  // your network password
  11. int status = WL_IDLE_STATUS;     // the Wifi radio's status
  12.  
  13. // change server to your email server ip or domain
  14. // IPAddress server( 1, 2, 3, 4 );
  15. char server[] = "smtpcorp.com";
  16. int port = 2525;
  17.  
  18. void setup()
  19. {
  20.   delay(2000);
  21.   Serial.begin(115200);
  22.   pinMode(4,OUTPUT);
  23.   digitalWrite(4,HIGH);
  24.  
  25.   // check for the presence of the shield:
  26.   if (WiFi.status() == WL_NO_SHIELD) {
  27.     Serial.println(F("WiFi shield not present"));
  28.     // don't continue:
  29.     while(true);
  30.   }
  31.  
  32.   // check firmware version
  33.   Serial.print(F("Firmware version: "));
  34.   Serial.println(WiFi.firmwareVersion());
  35.  
  36.  // attempt to connect to Wifi network:
  37.   while ( status != WL_CONNECTED) {
  38.     Serial.print(F("Attempting to connect to open SSID: "));
  39.     Serial.println(ssid);
  40.     status = WiFi.begin(ssid,pass);
  41.  
  42.     // wait 10 seconds for connection:
  43.     delay(10000);
  44.   }
  45.  
  46.   // you're connected now, so print out the data:
  47.   Serial.print(F("You're connected to the network"));
  48.   printCurrentNet();
  49.   printWifiData();
  50.  
  51.   delay(2000);
  52.   Serial.println(F("Ready. Press 'e' to send."));
  53. }
  54.  
  55. void loop()
  56. {
  57.   byte inChar;
  58.  
  59.   inChar = Serial.read();
  60.  
  61.   if(inChar == 'e')
  62.   {
  63.       if(sendEmail()) Serial.println(F("Email sent"));
  64.       else Serial.println(F("Email failed"));
  65.   }
  66. }
  67.  
  68. byte sendEmail()
  69. {
  70.   byte thisByte = 0;
  71.   byte respCode;
  72.   char tBuf[64];
  73.  
  74.   if(client.connect(server,port) == 1) {
  75.     Serial.println(F("connected"));
  76.   } else {
  77.     client.stop();
  78.     Serial.println(F("connection failed"));
  79.     return 0;
  80.   }
  81.  
  82.   if(!eRcv()) return 0;
  83.  
  84.   Serial.println(F("Sending hello"));
  85.   // change to the IP of your Arduino
  86.   strcpy_P(tBuf,PSTR("EHLO 192.168.0.2\r\n"));  
  87.   client.write(tBuf);
  88.   if(!eRcv()) return 0;
  89.  
  90.   Serial.println(F("Sending auth login"));
  91.   strcpy_P(tBuf,PSTR("auth login\r\n"));  
  92.   client.write(tBuf);
  93.   if(!eRcv()) return 0;
  94.  
  95.   Serial.println(F("Sending User"));
  96.   strcpy_P(tBuf,PSTR("dGltQHByb2xlY3Ryb24uY29t\r\n"));  
  97.  
  98.   client.write(tBuf);
  99.   if(!eRcv()) return 0;
  100.  
  101.   Serial.println(F("Sending Password"));
  102.   strcpy_P(tBuf,PSTR("cGhlbGFu\r\n"));  
  103.   client.write(tBuf);
  104.   if(!eRcv()) return 0;
  105.  
  106. // change to your email address (sender)
  107.   Serial.println(F("Sending From"));
  108.   strcpy_P(tBuf,PSTR("MAIL From: <tim@prolectron.com>\r\n"));  
  109.   client.write(tBuf);
  110.   if(!eRcv()) return 0;
  111.  
  112. // change to recipient address
  113.   Serial.println(F("Sending To"));
  114.   strcpy_P(tBuf,PSTR("RCPT To: <tim@prolectron.com>\r\n"));  
  115.   client.write(tBuf);
  116.   if(!eRcv()) return 0;
  117.  
  118.   Serial.println(F("Sending DATA"));
  119.   strcpy_P(tBuf,PSTR("DATA\r\n"));  
  120.   client.write(tBuf);
  121.   if(!eRcv()) return 0;
  122.  
  123.   Serial.println(F("Sending email"));
  124.  
  125. // change to recipient address
  126.   strcpy_P(tBuf,PSTR("To: Tim Dicus <tim@prolectron.com>\r\n"));  
  127.   client.write(tBuf);
  128.  
  129. // change to your address
  130.   strcpy_P(tBuf,PSTR("From: Tim Dicus <tim@prolectron.com>\r\n"));  
  131.   client.write(tBuf);
  132.  
  133.   client.println("Subject: Arduino email test\r\n");
  134.  
  135.   client.println("This is from my Arduino!");
  136.  
  137.   client.println(".");
  138.   if(!eRcv()) return 0;
  139.  
  140.   Serial.println(F("Sending QUIT"));
  141.   strcpy_P(tBuf,PSTR("QUIT\r\n"));  
  142.   client.write(tBuf);
  143.   if(!eRcv()) return 0;
  144.  
  145.   client.stop();
  146.  
  147.   Serial.println(F("disconnected"));
  148.  
  149.   return 1;
  150. }
  151.  
  152. byte eRcv()
  153. {
  154.   byte respCode;
  155.   byte thisByte;
  156.   int loopCount = 0;
  157.  
  158.   while(!client.available()) {
  159.     delay(1);
  160.     loopCount++;
  161.  
  162.     // if nothing received for 10 seconds, timeout
  163.     if(loopCount > 10000) {
  164.       client.stop();
  165.       Serial.println(F("\r\nTimeout"));
  166.       return 0;
  167.     }
  168.   }
  169.  
  170.   respCode = client.peek();
  171.  
  172.   while(client.available())
  173.   {  
  174.     thisByte = client.read();    
  175.     Serial.write(thisByte);
  176.   }
  177.  
  178.   if(respCode >= '4')
  179.   {
  180.     efail();
  181.     return 0;  
  182.   }
  183.  
  184.   return 1;
  185. }
  186.  
  187.  
  188. void efail()
  189. {
  190.   byte thisByte = 0;
  191.   int loopCount = 0;
  192.  
  193.   client.println("QUIT");
  194.  
  195.   while(!client.available()) {
  196.     delay(1);
  197.     loopCount++;
  198.  
  199.     // if nothing received for 10 seconds, timeout
  200.     if(loopCount > 10000) {
  201.       client.stop();
  202.       Serial.println(F("\r\nTimeout"));
  203.       return;
  204.     }
  205.   }
  206.  
  207.   while(client.available())
  208.   {  
  209.     thisByte = client.read();    
  210.     Serial.write(thisByte);
  211.   }
  212.  
  213.   client.stop();
  214.  
  215.   Serial.println(F("disconnected"));
  216. }
  217.  
  218. void printCurrentNet() {
  219.   // print the SSID of the network you're attached to:
  220.   Serial.print(F("SSID: "));
  221.   Serial.println(WiFi.SSID());
  222.  
  223.   // print the MAC address of the router you're attached to:
  224.   byte bssid[6];
  225.   WiFi.BSSID(bssid);    
  226.   Serial.print(F("BSSID: "));
  227.   Serial.print(bssid[5],HEX);
  228.   Serial.print(F(":"));
  229.   Serial.print(bssid[4],HEX);
  230.   Serial.print(F(":"));
  231.   Serial.print(bssid[3],HEX);
  232.   Serial.print(F(":"));
  233.   Serial.print(bssid[2],HEX);
  234.   Serial.print(F(":"));
  235.   Serial.print(bssid[1],HEX);
  236.   Serial.print(F(":"));
  237.   Serial.println(bssid[0],HEX);
  238.  
  239.   // print the received signal strength:
  240.   long rssi = WiFi.RSSI();
  241.   Serial.print(F("signal strength (RSSI):"));
  242.   Serial.println(rssi);
  243.  
  244.   // print the encryption type:
  245.   byte encryption = WiFi.encryptionType();
  246.   Serial.print(F("Encryption Type:"));
  247.   Serial.println(encryption,HEX);
  248. }
  249.  
  250. void printWifiData() {
  251.   // print your WiFi shield's IP address:
  252.   IPAddress ip = WiFi.localIP();
  253.     Serial.print(F("IP Address: "));
  254.   Serial.println(ip);
  255.   Serial.println(ip);
  256.  
  257.   // print your MAC address:
  258.   byte mac[6];  
  259.   WiFi.macAddress(mac);
  260.   Serial.print(F("MAC address: "));
  261.   Serial.print(mac[5],HEX);
  262.   Serial.print(F(":"));
  263.   Serial.print(mac[4],HEX);
  264.   Serial.print(F(":"));
  265.   Serial.print(mac[3],HEX);
  266.   Serial.print(F(":"));
  267.   Serial.print(mac[2],HEX);
  268.   Serial.print(F(":"));
  269.   Serial.print(mac[1],HEX);
  270.   Serial.print(F(":"));
  271.   Serial.println(mac[0],HEX);
  272.  
  273.   // print your subnet mask:
  274.   IPAddress subnet = WiFi.subnetMask();
  275.   Serial.print(F("NetMask: "));
  276.   Serial.println(subnet);
  277.  
  278.   // print your gateway address:
  279.   IPAddress gateway = WiFi.gatewayIP();
  280.   Serial.print(F("Gateway: "));
  281.   Serial.println(gateway);
  282. }