Sending e-mail/sms with Arduino UNO

Hi everybody!

I am a very very new with this arduino stuff. At the beginning it seemed straightforward and easy but as I went further I started to get stuck!

I am trying to make my Arduino send an e-mail. I connected the ethernet shield and it receives internet, everything is ok so far. I am using a g-mail account for the sender.

In the Serial monitor I get the following messages:

connecting…
connected
220 mx.google.com ESMTP en1sm464023wbb.1
250 mx.google.com at your service
530 5.7.0 Must issue a STARTTLS command first. en1sm464023wbb.1
530 5.7.0 Must issue a STARTTLS command first. en1sm464023wbb.1
530 5.7.0 Must issue a STARTTLS command first. en1sm464023wbb.1
502 5.5.1 Unrecognized command. en1sm464023wbb.1
502 5.5.1 Unrecognized command. en1sm464023wbb.1
502 5.5.1 Unrecognized command. en1sm464023wbb.1
502 5.5.1 Unrecognized command. en1sm464023wbb.1
221 2.0.0 closing connection en1sm464023wbb.1

disconnecting.

This is the code I’m using:

#include <SPI.h>

#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <Udp.h>

#include <Ethernet.h>

byte mac = { 0x*, 0x*, 0x*, 0x*, 0x*, 0x* }; // Arduino’s artificial mac address
byte ip = { *, *, *, * }; // my ip
byte gateway = { *, *, *, *};
byte server = { *, *, *, *}; // my smtp server ip
byte subnet = { *, *, *, *};
int time = 5000;
int wait = 2000;

Client client(server, 25); // introduces the gmail smtp server ip/port

void setup()
{
delay(time); //allow the router to indentify arduino before arduino connects

Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);

delay(1000);

Serial.println(“connecting…”);

if (client.connect()) {
Serial.println(“connected”);

client.println(“HELO something.com”); // say hello (statement after helo is needed but irrelevant)*/
delay(wait); // wait for a response */

client.println(“MAIL FROM: address@gmail.com”); /* identify sender, /
delay(wait); /
wait for a response */

client.println(“RCPT TO: address@gmail.com”); /* identify recipient /
delay(wait); /
wait for a response */

client.println(“DATA”);
delay(wait); /* wait for a response */

client.println(“To: address@gmail.com”); /* identify recipient /
client.println(“Subject: You Have Mail!!”); /
insert subject */

client.println(“Please let me know it worked!!!”); /* insert body /
client.println("."); /
end email */

client.println(“QUIT”); /* terminate connection /
delay(wait); /
wait for a response */

client.println();

} else {
Serial.println(“connection failed”);
}
}

void loop()
{
while (client.available()) {
char c = client.read();
Serial.print(c);
}

if (!client.connected()) {
Serial.println();
Serial.println(“disconnecting.”);
client.stop();
for(;:wink:
;
}
}

Any ideas on what I’m doing wrong? Please help as soon as possible!!
Thanks!

OD

No real answers, but I do have some questions.

                          delay(wait); // wait for a response */

What, exactly, are you waiting for? The server that is receiving the data is orders of magnitude faster than the Arduino. By the time the client.println() function has finished shuffling out the data, the server has practically died of boredom. Now, you want it to wait 2 more seconds before you send it more data? The server will not generate a response until the whole command is received, and you certainly are making no attempt to read any such reply.

You issue 3 commands (HELO, MAIL FROM, and RCPT TO) that the server responded to:

530 5.7.0 Must issue a STARTTLS command first. en1sm464023wbb.1

Looks like a clue, there. I have no idea what it means, but since you (seem to) know all the commands needed to send mail, I would hope that it means something to you.

You issue more commands (DATA, To:, Subject:, QUIT) to which the server replied:

502 5.5.1 Unrecognized command. en1sm464023wbb.1

Again, this looks like a clue. Perhaps the response is due to the fact that the server is not in the mail command mode, and these message might go away when you fix the previous problem.

It sure looks like google isn't interested in a non-secure connection. Unless you have a TLS (aka SSL) library for Arduino, you may need to find another server to send through. Plain SMTP is rarer to find these days, esp. "out in the wild" though your ISP may allow such connections from their customers. If that's not possible, you may need to implement your own SMTP relay, so you can talk plain old SMTP to it, and it can communicate with mail servers farther afield securely.

Thanks for the feedback! :slight_smile:

OD

OK, let me try it this way:

Does anyone have a working code to send an e-mail using ARDUINO UNO ??

It would be juuust great!

OD

Your code might well be fine, but you can't use google as your SMTP server. Given a lack of SSL libraries for Arduino, you need to find (or implement) an SMTP server that doesn't require SSL (TLS).

Thank you, I'll try and find a SMTP server that doesn't require SSL(TLS) 'cus implementing it?.....it doesn't sound too easy...hmm

OD

Thank you, I'll try and find a SMTP server that doesn't require SSL(TLS) 'cus implementing it?.....it doesn't sound too easy...hmm

Your ISP's SMTP server might work as often they don't require login from internal IPs.

I already looked into that...not working...now I manage to connect to the sender address (I found a SMTP server that doesn't require SSL/TLS) so it's trying to send the e-mail but it won't allow the rcpt to receive it (saying 'invalid rcpt' - which of course it's not true) and I receive an error message like "I can cheat too.Goodbye!" ... What I'm thinking.... IT'S ALIIIIVEEEEEE hahahahaha

Sorry, after spending houuuuurssss on this thing I just got a little bit silly! Still, if anyone has an idea, please feel free to post it!

Cheers!

OD

Below is code that works with my ISP’s SMTP server. Bottom is code that may work when a login is required.

/*
SEND AN EMAIL WITH ARDUINO

This code was created by modifying the connect example from arduino.cc and with the help of the YABB forums and their very helpful members.
This code sends an email to any email address and then disconnects.  It will also print on screen (in the serial monitor) the data the server sends the Arduino.  Make sure to replace all the *'s with your own information.
*/

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Arduino's artificial mac address
byte ip[] = { 192, 168, 1, 102 }; // my ip
byte server[] = { ***, ***, ***, *** }; // my smtp.***.net server ip
int time = 5000;
int wait = 2000;


Client client(server, 25);

void setup()
{
 delay(time);
 
 Ethernet.begin(mac, ip);
 Serial.begin(9600);
 
 delay(1000);
 
 Serial.println("connecting...");
 
 if (client.connect()) {
                        Serial.println("connected");
   
                        client.println("HELO itismeletschat"); /* say hello (statement after helo is needed but irrelevant)*/
                          delay(wait); /* wait for a response */

                        client.println("MAIL From: me@home.com"); /* identify sender, this should be the same as the smtp server you are using */
                          delay(wait); /* wait for a response */

                        client.println("RCPT To: you@home.com"); /* identify recipient */
                        delay(wait); /* wait for a response */

                        client.println("DATA"); 
                          delay(wait); /* wait for a response */
 
                        client.println("To: you@home.com"); /* identify recipient */
                        client.println("Subject: You Have Mail!!"); /* insert subject */

                        client.println("Please let me know it worked!!!"); /* insert body */
                        client.println("."); /* end email */
                 
                        client.println("QUIT"); /* terminate connection */
                         delay(wait); /* wait for a response */

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

void loop()
{
 while (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 
 if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for(;;)
     ;
 }
}
 //http://www.motobit.com/util/base64-decoder-encoder.asp e 
 //http://webnet77.com/cgi-bin/helpers/base-64.pl 

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

 // Local network configuration:
 byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x23, 0x5D };
 byte ip[] = { 192,168,0, 177 };

 // Login data:
 String UserName64 = "XXXXXXX";
 String Password64 = "XXXXXXX";

 // SMTP server data:
 // smtp.libero.it : 212.52.84.54
 // smtp.iol.it : 212.52.84.203
 // out.virgilio.it : 62.211.72.20
 byte server[] = { 212, 52, 84, 54 }; // SMTP server
 String ServerName = "libero.it";

 // Mail data:
 String Sender = "XXXXXXX@libero.it";
 String Recipient = "XXXXXXX@libero.it";
 String Subject = "Messaggio inviato da Arduino!";
 String Body = "Testo del messaggio\nsu piu'\ndi una riga.";

 int time = 1000;
 int wait = 1000;
 String ServerResponse="";
 Client client(server, 25);

 void setup()
 {
  Serial.begin(9600);
  Serial.println("Program started, waiting for router...");
  delay(time); /* allow the router to identify the Arduino before the Arduino connects to the internet */

  Serial.println("Starting network module...");
  Ethernet.begin(mac, ip);
  delay(2000);

  Serial.println("connecting...");
  if (client.connect()) {
     Serial.println("connected");

     SendMsg("EHLO " + ServerName); /* say hello*/
     SendMsg("AUTH LOGIN ");
     SendMsg(UserName64); /* Username*/
     SendMsg(Password64); /* Password */
     SendMsg("MAIL From:<" + Sender +">"); /* identify sender */
     SendMsg("RCPT To:<" + Recipient + ">"); /* identify recipient */
     SendMsg("DATA");
     SendMsg("To: " + Recipient); /*  recipient in message header */
     SendMsg("From: " + Sender); /* seder name in message header */
     SendMsg("Subject: "+ Subject); /* insert subject */
     SendMsg(""); /* empty line */
     SendMsg(Body); /* insert body */
     SendMsg(""); /* empty line */
     SendMsg("."); /* end mail */
     SendMsg("QUIT"); /* terminate connection */
     client.println();

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

 void loop()
 {
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
 }

 void GetResponse() {
    if (client.available()) {
         char c = client.read();
         while (client.available()) { // Store command char by char.
           ServerResponse +=c;
           c = client.read();
         }
    Serial.println("<<<" + ServerResponse);
    ServerResponse="";
  }
 }

 void SendMsg(String m) {
   client.println(m);
   Serial.println(">>>" + m);
   delay(wait); /* wait for a response */
   GetResponse();
 }

Zoomkat, why is it necessary to delay after sending an incomplete command?

O_dana:
I already looked into that…not working…now I manage to connect to the sender address (I found a SMTP server that doesn’t require SSL/TLS) so it’s trying to send the e-mail but it won’t allow the rcpt to receive it (saying ‘invalid rcpt’ - which of course it’s not true) and I receive an error message like “I can cheat too.Goodbye!” … What I’m thinking… IT’S ALIIIIVEEEEEE hahahahaha

Sorry, after spending houuuuurssss on this thing I just got a little bit silly! Still, if anyone has an idea, please feel free to post it!

Cheers!

OD

The SMTP server you “found” may only be interested in local recipients. Like if you found that you could talk to asdf.com, it likely doesn’t want to relay mail, but rather accept incoming messages for @asdf.com. You need an open relay that speaks plain vanilla SMTP, or you need to speak to the SMTP server for the recipient’s domain (may or may not work trivially). Best is figuring out how to talk to your ISP’s SMTP server (as both Zoomkat and I mentioned), or implement an SMTP relay on the same local network that the arduino is on, and have the arduino talk to it.

Zoomkat, why is it necessary to delay after sending an incomplete command?

I would assume the delays are actually shortcuts for having to process/evaluate return info from the server prior to sending the next line to the server (assuming that a client/server dialog is involved). Bottom is the output to the serial monitor when I send an email (with server name removed).

connecting...
connected
220 rg25.xxx.net ESMTP Mirapoint Messaging Server MOS 4.1.10-GA Queueing
250 rg25.xxx.net Hello
250 Sender OK
250 Recipient OK
354 Enter your message, followed by a dot on a line by itself
250 ALJ53240 Message accepted for delivery
221 rg25.xxx.net Goodbye

disconnecting.

Did some testing and it appears on my system no delays or dialog timing is required. Even with the added delay at the end, the “disconnecting.” no longer prints to the serial monitor.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Arduino's artificial mac address
byte ip[] = { 192, 168, 1, 102 }; // my ip
byte server[] = { xxx, xxx, xxx, xxx }; // my smtp.xxx.net server ip

//int x=0;
//char lf=10;

Client client(server, 25);

void setup()
{
  Ethernet.begin(mac, ip);
 Serial.begin(9600);

 Serial.println("connecting...");
 
 if (client.connect()) {
                        Serial.println("connected");
   
                        client.println("HELO itismeletschat"); /* say hello (statement after helo is needed but irrelevant)*/
                         /* while (client.available()) {
                          char c = client.read();
                          if (c==lf) x=(x+1);
                          if (x==2) {break;}
                          Serial.print(c); 
                          } */

                        client.println("MAIL From: me@home.com"); /* identify sender, this should be the same as the smtp server you are using */
                          /*while (client.available()) {
                          char c = client.read();
                          if (c==lf) {break;}
                          Serial.print(c); 
                          } */


                        client.println("RCPT To: you@home.com"); /* identify recipient */
                         /* while (client.available()) {
                          char c = client.read();
                          if (c==lf) {break;}
                          Serial.print(c); 
                          } */


                        client.println("DATA"); 
                         /* while (client.available()) {
                          char c = client.read();
                          if (c==lf) {break;}
                          Serial.print(c); 
                          } */

 
                        client.println("To: you@home.com"); /* identify recipient */
                        client.println("Subject: You Have Mail!!"); /* insert subject */

                        client.println("Please let me know it worked!!!"); /* insert body */
                        client.println("."); /* end email */
                 
                        client.println("QUIT"); /* terminate connection */
                         /* while (client.available()) {
                          char c = client.read();
                          if (c==lf) {break;}
                          Serial.print(c); 
                          } */
                        
                        client.println();
   
 } else {
   Serial.println("connection failed");
 }
}

void loop()
{
   delay(10000);
   if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for(;;)
     ;
 }
}

So, I managed to make it work in my smtp network server so THANK YOU FOR THAT!

I am still trying to make it work when an authentication is required!

But really, thank you for all the responses! This is really great!

Cheers,

OD

Ok, what I am trying now is change the byte server[] = {,,,} into a server = "something.domain.com" is this even possible??

Thank you!

OD

One possibility is to use the EthernetDNS library. I've used it with success

http://gkaindl.com/software/arduino-ethernet/dns

Ok, I’m trying the webserver example and it’s not working.

Then, I tried the etherShield_ping example (after downloading and importing etherShield library) and it’s still not working. I can’t manage to ping the ip in the command window.

Any clues?

Cheers,

OD

You might want to look at this workaround

http://code.google.com/p/my-arduino-projects/downloads/detail?name=WebServer_Sensors_Email_LEDControl_3%20-%20Copie.pde&can=2&q=

This is on the way but will mean reencoding the password string when you change it.

I have found a Arduino base-64 en/decoder addon but am working out how it works. v1.1 of my code may incorporate this but it is not a priority for me.

I am working on another sub-project to give me the building blocks to create my drop-in replacement for my Bosch Alarm system.

Thanks for all the replies!

and soo sorry for the delay in mine. I managed to make the webserver work. Actually I am hosting a webpage on my ARduino now.

that's pretty cool,

I think...

Cheers,

OD