Email client

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)) {
  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. }

Share