Go Down

Topic: Sending email from the Arduino...is all I need... (Read 1 time) previous topic - next topic

ofransen

I'm new to the networking lark so apologies for the simplicity of the question.

For hardware I need an Ethernet shield, any reccommendations?

For software presumably an Ethernet library, I've been browsing the forum and seen <ethernet.h>. Is that all I need software wise?

I'd initially be doing this from home, but later and for monitoring machines where I work. Presumably I'll setup my router here and ask the admin to setup their router there.

And finally is there an example of sending email from an Arduino with the above setup somewhere?

Again apologies for the simplicty of the questions...!
Owen F. Ransen

zoomkat

I recomend for you to get a wiznet w5100 dased ethernet shield due to ease of use. Below is some simple email code.

Code: [Select]

/*
SEND AN EMAIL WITH ARDUINO
9/23/11
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.
*/

#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, yyy, zzz, aaa }; // my smtp.xxx.net server ip

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)*/

                        client.println("MAIL From: me@my-isp.net"); /* identify sender, this should be the same as the smtp server you are using */

                        client.println("RCPT To: you@your-isp.net"); /* identify recipient */

                        client.println("DATA");

                        client.println("To: you@your-isp.net"); /* identify recipient */
                        client.println("Subject: You Have Arduino Mail!!"); /* insert subject */

                        client.println("Please let me know it worked!!!"); /* insert body */
                        client.println("."); /* end email */
                 
                        client.println("QUIT"); /* terminate connection */
                       
                        client.println();
   
} else {
   Serial.println("connection failed");
}
}

void loop()
{
   delay(10000);
   if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for(;;)
     ;
}
}
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

ofransen


I recomend for you to get a wiznet w5100 dased ethernet shield due to ease of use. Below is some simple email code.


Many thanks for that! And the source code example.

Owen F. Ransen

ofransen

My Arduino Ethernet has arrived and I've been able to make it connect to google and do a search, so it is working pretty well.

I'm having a problem sending email though.

I believe the three magic numbers are correct:

Code: [Select]

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x6A, 0xC7 };  // Taken from the back of the board
byte ip[] = {192,168,1,101}; // ip of the Arduino on my router, works ok with the google example
byte server[] = {217,15,217,10} ; // mail.nemo.it from domaintoiup.com


Then I do this...

Code: [Select]

         Serial.println("connected");
         client.println("HELO itismeletschat"); /* say hello (statement after helo is needed but irrelevant)*/
         client.println("MAIL From: xxxx@nemo.it"); /* identify sender, this should be the same as the smtp server you are using */
         client.println("RCPT To: help@ransen.com"); /* identify recipient */
         client.println("DATA");
         client.println("To: help@ransen.com"); /* identify recipient */
         client.println("Subject: You Have Arduino Mail!!"); /* insert subject */
         client.println("Please let me know it worked!!!"); /* insert body */
         client.println("."); /* end email */
         client.println("QUIT"); /* terminate connection */
         client.println();           
         Serial.println ("Message sent\n") ;


I get "connected" and "Message sent" on the serial monitor, but no message arrives.

I suspect that I need a login and password...? And if so what is the sequence ?

Or am I missing something else?




Owen F. Ransen

zoomkat

Usually if you are sending email thru your ISP's email server, user name and password are not needed. Below is some previously posted code that has provisions for name and password. I think the name and password need to be base64 encoded, which may be obtained via the links in the code.

Code: [Select]

//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();
}
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

ofransen


Usually if you are sending email thru your ISP's email server, user name and password are not needed. Below is some previously posted code that has provisions for name and password. I think the name and password need to be base64 encoded, which may be obtained via the links in the code.


Thanks for the reply, but I'm now wondering why I do not need usename and password?

I use theBat! as my email client and in the setup I must put in the username and password else it does not work. When I use a WEB based email client I have to give username and password.

Owen F. Ransen

zoomkat

Quote
I use theBat! as my email client and in the setup I must put in the username and password else it does not work. When I use a WEB based email client I have to give username and password.


It generally depends on the owner of the email server you are trying to access. I don't need to use my name and password to access the server while at home because the originating IP address is within their domain. If I were to try to access it from another system, then the name and password would be required.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

SurferTim

#7
Nov 22, 2011, 03:11 am Last Edit: Nov 22, 2011, 03:35 am by SurferTim Reason: 1
Quote
SendMsg("EHLO " + ServerName); /* say hello*/

The email server responds here. What does it tell you?
Quote
SendMsg("MAIL From:<" + Sender +">"); /* identify sender */

The email server responds with ok or fail on the sender. What does it say?
Quote
SendMsg("RCPT To:<" + Recipient + ">"); /* identify recipient */

The server responds here with whether it can take mail for that address. What does the server tell you?
Quote
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 */

The server responds with the status here. What does it tell you?

There is no error checking in that code, so it is almost impossible to tell what has happened. Reply#24 in the following link has email sending with error checking. Then I can help you.
http://arduino.cc/forum/index.php/topic,69647.msg558516.html#msg558516

ofransen


There is no error checking in that code, so it is almost impossible to tell what has happened. Reply#24 in the following link has email sending with error checking. Then I can help you.
http://arduino.cc/forum/index.php/topic,69647.msg558516.html#msg558516


Thanks, I'll investigate further and document the replies.

Owen F. Ransen

Go Up