Ardueno SMTP Code for sending email

Seems there aren’t many SMTP sending examples for the Ardueno Ethernet Shields… below may be of help:

Get your base64 string by:

perl -MMIME::Base64 -e ‘print encode_base64(“you\@yourdomein.com00you\@yourdomain.com00YourPassWord”)’

Then, in your Arduino code:

if (client.connect("smtp.yourisp.com", 25)) {
  boolean ok=true;
  Serial.println("connected...");
  ok=readInternetLineContining("220 ",5);

  if(ok){
    client.println("ehlo arduino.yourname.com");
    ok=readInternetLineContining("250 ",5);
  }

  if(ok){
    client.println("AUTH PLAIN [[[YOUR BASE64 AUTH STRING HERE]]]");
    ok=readInternetLineContining("235 ",5);
  }

  if(ok){
    client.println("MAIL FROM:<jeremy@laurenson.com>");
    ok=readInternetLineContining("250 OK",5);
  }

  if(ok){
    client.println("RCPT TO:<pagejeremy@laurenson.com>");
    ok=readInternetLineContining("250 OK",5);
  }

  if(ok){
    client.println("DATA");
    ok=readInternetLineContining("354 ",5);
  }

  if(ok){
    client.println("From: <user@ardueno.com>");
    client.println("TO: <pagejeremy@ardueno.com>");
    client.println("SUBJECT: Your page, sir...");
    client.println();
    client.println("This from Arduino");
    client.println(".");
    ok=readInternetLineContining("250 ",5);
  }

  if(ok){
    client.println("QUIT");
    ok=readInternetLineContining("221 OK",5);
  }

  if(ok){
    Serial.println("done with success!");
  }

  client.stop();

}
else {
  Serial.println("connection failed");
}

boolean readInternetLineContining( String search, int timeOut)
{
 time_t t=now();
 boolean found=false;
 String tempString;

 Serial.println("Looking for " + search + " in the Ethernet stream...");

 while(!found){
  tempString =readInternetLine(timeOut);
  Serial.println(tempString);
  if(tempString.indexOf(search)>-1)
  {
    Serial.println("Found!");
    return true;
   }

  if(now()-t>timeOut){
    Serial.println("Timeout looking for "+ search);
    return false;
   }
  }
}

String readInternetLine(int timeOut)
{
// This function reads a line of text from the network client connection.
// and returns after a \r\n (It does not return the \r or \n)
// It also only waits for timeOut number of seconds before returning, assuming
// that the return string never came.

if(!client.connected())return "";
time_t t=now();
int ok=0;
String tempString="";
while(ok==0){
if(client.available()) {
char c = client.read();
if(c=='\n')ok=1;
if(!(c=='\r'))if(!(c=='\n'))
tempString.concat(c);
}
else
{
if(now()-t>timeOut)ok=1;
}
}
return(tempString);
}
Advertisements

2 thoughts on “Ardueno SMTP Code for sending email

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s