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

Email client

If you want to send email via SMTP2GO, that code is the second sketch below this code.

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 title says email client, but in fact, this sends email like an email server (Sendmail or Postfix), not like a client (Outlook or Thunderbird). The SMTP protocal is used. For retrieving emails with POP3, see Email POP3

WARNING! Each device on a network must have a unique mac address. If you are using more than one ethernet shield on a network, you must insure all mac addresses are unique. No duplicates!

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


SMTP2GO

This is for SMTP2GO. You must change the user, password, from and to email addresses. Your user name and password must be base64 encoded with utf-8 character set. You can do that here. https://www.base64encode.org/

  1. /*
  2.    Email client sketch for IDE v1.0.5 and w5100/w5200
  3.    Posted 7 May 2015 by SurferTim
  4. */
  5.  
  6. #include <SPI.h>
  7. #include <Ethernet.h>
  8.  
  9. // this must be unique
  10. byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };  
  11. // change network settings to yours
  12. IPAddress ip( 192, 168, 2, 2 );    
  13. IPAddress gateway( 192, 168, 2, 1 );
  14. IPAddress subnet( 255, 255, 255, 0 );
  15.  
  16. char server[] = "smtpcorp.com";
  17. int port = 2525;
  18.  
  19. EthernetClient client;
  20.  
  21. void setup()
  22. {
  23.   Serial.begin(115200);
  24.   pinMode(4,OUTPUT);
  25.   digitalWrite(4,HIGH);
  26.   Ethernet.begin(mac, ip, gateway, gateway, subnet);
  27.   delay(2000);
  28.   Serial.println(F("Ready. Press 'e' to send."));
  29. }
  30.  
  31. void loop()
  32. {
  33.   byte inChar;
  34.  
  35.   inChar = Serial.read();
  36.  
  37.   if(inChar == 'e')
  38.   {
  39.       if(sendEmail()) Serial.println(F("Email sent"));
  40.       else Serial.println(F("Email failed"));
  41.   }
  42. }
  43.  
  44. byte sendEmail()
  45. {
  46.   byte thisByte = 0;
  47.   byte respCode;
  48.  
  49.   if(client.connect(server,port) == 1) {
  50.     Serial.println(F("connected"));
  51.   } else {
  52.     Serial.println(F("connection failed"));
  53.     return 0;
  54.   }
  55.  
  56.   if(!eRcv()) return 0;
  57.  
  58.   Serial.println(F("Sending hello"));
  59. // replace 1.2.3.4 with your Arduino's ip
  60.   client.println("EHLO 1.2.3.4");
  61.   if(!eRcv()) return 0;
  62.  
  63.   Serial.println(F("Sending auth login"));
  64.   client.println("auth login");
  65.   if(!eRcv()) return 0;
  66.  
  67.   Serial.println(F("Sending User"));
  68. // Change to your base64 encoded user
  69.   client.println("xxxx");
  70.  
  71.   if(!eRcv()) return 0;
  72.  
  73.   Serial.println(F("Sending Password"));
  74. // change to your base64 encoded password
  75.   client.println("yyyy");
  76.  
  77.   if(!eRcv()) return 0;
  78.  
  79. // change to your email address (sender)
  80.   Serial.println(F("Sending From"));
  81.   client.println("MAIL From: <me@mydomain.com>");
  82.   if(!eRcv()) return 0;
  83.  
  84. // change to recipient address
  85.   Serial.println(F("Sending To"));
  86.   client.println("RCPT To: <you@yourdomain.com>");
  87.   if(!eRcv()) return 0;
  88.  
  89.   Serial.println(F("Sending DATA"));
  90.   client.println("DATA");
  91.   if(!eRcv()) return 0;
  92.  
  93.   Serial.println(F("Sending email"));
  94.  
  95. // change to recipient address
  96.   client.println("To: You <you@yourdomain.com>");
  97.  
  98. // change to your address
  99.   client.println("From: Me <me@mydomain.com>");
  100.  
  101.   client.println("Subject: Arduino email test\r\n");
  102.  
  103.   client.println("This is from my Arduino!");
  104.  
  105.   client.println(".");
  106.  
  107.   if(!eRcv()) return 0;
  108.  
  109.   Serial.println(F("Sending QUIT"));
  110.   client.println("QUIT");
  111.   if(!eRcv()) return 0;
  112.  
  113.   client.stop();
  114.  
  115.   Serial.println(F("disconnected"));
  116.  
  117.   return 1;
  118. }
  119.  
  120. byte eRcv()
  121. {
  122.   byte respCode;
  123.   byte thisByte;
  124.   int loopCount = 0;
  125.  
  126.   while(!client.available()) {
  127.     delay(1);
  128.     loopCount++;
  129.  
  130.     // if nothing received for 10 seconds, timeout
  131.     if(loopCount > 10000) {
  132.       client.stop();
  133.       Serial.println(F("\r\nTimeout"));
  134.       return 0;
  135.     }
  136.   }
  137.  
  138.   respCode = client.peek();
  139.  
  140.   while(client.available())
  141.   {  
  142.     thisByte = client.read();    
  143.     Serial.write(thisByte);
  144.   }
  145.  
  146.   if(respCode >= '4')
  147.   {
  148.     efail();
  149.     return 0;  
  150.   }
  151.  
  152.   return 1;
  153. }
  154.  
  155.  
  156. void efail()
  157. {
  158.   byte thisByte = 0;
  159.   int loopCount = 0;
  160.  
  161.   client.println(F("QUIT"));
  162.  
  163.   while(!client.available()) {
  164.     delay(1);
  165.     loopCount++;
  166.  
  167.     // if nothing received for 10 seconds, timeout
  168.     if(loopCount > 10000) {
  169.       client.stop();
  170.       Serial.println(F("\r\nTimeout"));
  171.       return;
  172.     }
  173.   }
  174.  
  175.   while(client.available())
  176.   {  
  177.     thisByte = client.read();    
  178.     Serial.write(thisByte);
  179.   }
  180.  
  181.   client.stop();
  182.  
  183.   Serial.println(F("disconnected"));
  184. }