Loading...

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.

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!

/*
   Email client sketch for IDE v1.0.1 and w5100/w5200
   Posted December 2012 by SurferTim
*/


#include <SPI.h>
#include <Ethernet.h>

// this must be unique
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };  
// change network settings to yours
IPAddress ip( 192, 168, 2, 2 );    
IPAddress gateway( 192, 168, 2, 1 );
IPAddress subnet( 255, 255, 255, 0 );

// change server to your email server ip or domain
// IPAddress server( 1, 2, 3, 4 );
char server[] = "email.yourdomain.com";

EthernetClient client;

void setup()
{
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  delay(2000);
  Serial.println("Ready. Press 'e' to send.");
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'e')
  {
      if(sendEmail()) Serial.println("Email sent");
      else Serial.println("Email failed");
  }
}

byte sendEmail()
{
  byte thisByte = 0;
  byte respCode;

  if(client.connect(server,25)) {
    Serial.println("connected");
  } else {
    Serial.println("connection failed");
    return 0;
  }

  if(!eRcv()) return 0;
  Serial.println("Sending helo");

// change to your public ip
  client.write("helo 1.2.3.4\r\n");

  if(!eRcv()) return 0;
  Serial.println("Sending From");

// change to your email address (sender)
  client.write("MAIL From: <me@mydomain.com>\r\n");

  if(!eRcv()) return 0;

// change to recipient address
  Serial.println("Sending To");
  client.write("RCPT To: <you@yourdomain.com>\r\n");

  if(!eRcv()) return 0;

  Serial.println("Sending DATA");
  client.write("DATA\r\n");

  if(!eRcv()) return 0;

  Serial.println("Sending email");

// change to recipient address
  client.write("To: You <you@yourdomain.com>\r\n");

// change to your address
  client.write("From: Me <me@mydomain.com>\r\n");

  client.write("Subject: Arduino email test\r\n");

  client.write("This is from my Arduino!\r\n");

  client.write(".\r\n");

  if(!eRcv()) return 0;

  Serial.println("Sending QUIT");
  client.write("QUIT\r\n");

  if(!eRcv()) return 0;

  client.stop();

  Serial.println("disconnected");

  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;

  while(!client.available()) delay(1);

  respCode = client.peek();

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  if(respCode >= '4')
  {
    efail();
    return 0;  
  }

  return 1;
}


void efail()
{
  byte thisByte = 0;

  client.write("QUIT\r\n");

  while(!client.available()) delay(1);

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  client.stop();

  Serial.println("disconnected");

}