Pages: [1] 2   Go Down
Author Topic: Need help getting Arduino to send email  (Read 16632 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am very new to programing in general.  At this point I have no issues with making LED's blink and what not, and I am now hoping to get my Arduino to connect and communicate via SMTP with an email provider like Gmail, and send an email.  However, all I am currently able to do is run the connect example, due to my lack of knowledge (it is hard to find information on this application).  As a result I am posting this plea, hoping that someone will be able to show me what this code would look like in the Arduino code, maybe even there own example.
Logged

New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Protocols like this are text based, so--for example--when you have a web client connect it sends some text to make a request for a URL and receives a text response which it must then read and interpret.

You want to find out to which port you need to connect to make a SMTP connection and then what text you need to send and receive in order to send your email.

--Phil.
Logged

London
Offline Offline
Sr. Member
****
Karma: 1
Posts: 330
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


As follower stated.

and here are some examples found by googling "telnet smtp example"


http://www.activexperts.com/activemail/telnet/

http://evolvedcode.net/content/doc_alttelnet/index_p5.asp

http://www.yuki-onna.co.uk/email/smtp.html

Gordon
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



I have taken your advices and I am now attempting to use telnet.  Can I use the same language i would use in telnet (helo, mail from, mail to) with the Arduino?  Because ideally I want the Arduino to send the email without the use of a computer (I have an Ethernet shield).


from my computer (windows xp) I do this:

start -> run -> telnet smtp.gmail.com 25 ->open


then i type:

helo letschat (everyhting is ok still)

mail from: myemailaddress@gmail.com (then i get "530 5.7.0 must issue a STARTTLS command first", so i took the hint)

STARTTLS (it says "220 2.2.0 ready to start TLS")[/b]


and now it is expecting me to negotiate a TLS connection (I have tried to attach a screen shot below), but I cannot figure out how to do this, and think I am either over complicating this or have done something very wrong.  All the examples I read from the links posted make the proccess look very straight forward:



HELO arduino

MAIL From:<me@gmail.com>

RCPT To:<you@gmail.com>

DATA

To: you@gmail.com
Subject: You Have Mail!!

Please let me know it worked!!!

.

QUIT


Any ideas on where I go from here?

 I really appreciate the help you have given me so far and hopefully will keep giving me.

-Enrico
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

gmail/google probaly requires tls/ssl auth.

Try using your internet service provider´s SMTP server instead,since that dont have to auth you (since the ip is in their range).
Logged

London
Offline Offline
Sr. Member
****
Karma: 1
Posts: 330
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


If you look at the client examples. You will see all they are doing is sending text to the remote server.
So they are effectively telnetting but on a different port.
You just need to change the port and emulate the text you are typing at the telnet prompt.
You will also need to check the responses as well.

Gordon
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I think I am starting to see how this is going to work.  I have put together some code that would ideally send an email, right now it connects and tells me 220 then disconnects im not sure how to get the responses to show up on my serial monitor.  I essentially just modified the Ethernet example for connecting to google and changed the IP address and port.  I have left it as Gmail but I will change it to my ISP's address and port (if different).  Also I have used "*" to mask my own IP address and MAC address (which I tested and they work).

here is my code:

[glow]#include <Ethernet.h>

byte mac[] = { 0x*, 0x*, 0x*, 0x*, 0x*, 0x* };
byte ip[] = { *, *, 1, * };
byte server[] = { 64, 233, 167, 111 }; // Gmail
int time = 5000;
int wait = 1000;

Client client(server, 25);

void setup()
{
  delay(time); /* allow the router to identify the Arduino before the Arduino connects to the internet */
  
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting...");
  
  if (client.connect()) {
                          Serial.println("connected");
    
                         client.println("HELO thisisme@gmail.com"); /* say hello*/
                           delay(wait); /* wait for a response */
    
                         client.println("MAIL From:<thisisme@gmail.com>"); /* identify sender */
                           delay(wait); /* wait for a response */
    
                         client.println("RCPT To: thisisyou@gmail.com>"); /* identify recipient */
                           delay(wait); /* wait for a response */
    
                         client.println("DATA");
                           delay(wait); /* wait for a response */
    
                          client.println("To: thisisyou@gmail.com"); /* identify recipient */
                          client.println("Subject: You Have Mail!!"); /* insert subject */

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

                          client.println("QUIT"); /* terminate connection */
                           delay(wait); /* wait for a response */
    
  
    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(;smiley-wink
      ;
  }
}
[/glow]


now at this point I still need the responses from the isp to be displayed on the screen so that I can tell if there is a problem and where.

right now I see:

connecting...
connected
220
disconnecting


Furthermore, I do not receive an email or at least it hasn't arrived yet, but the connected line does sound promising.  Also is client.println() the right function to use to communicate with the server?

-Enrico
Logged

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any email provider worth spit will require some sort of authentication.  There is an SMTP authentication protocol, as well as various schemes like POP-before-SMTP.

I don't know what gmail users, but the authentication is probably what is stopping you.

-j

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am now trying to monitor more closely what is going on.  I have tried using the serial.println() function (shown below) but with some drawbacks....


ATTEMPT 1

[glow]char d = client.read();
    Serial.println(d);    

client.println("HELO thisisme@gmail.com"); /* say hello*/
                          delay(wait); /* wait for a response */
  
char d = client.read();
    Serial.println(d);


                        client.println("MAIL From:<thisisme@gmail.com>"); /* identify sender */
                          delay(wait); /* wait for a response */
  
char e = client.read();
    Serial.println(e);


                        client.println("RCPT To: thisisyou@gmail.com>"); /* identify recipient */
                          delay(wait); /* wait for a response */
[/glow]


ATTEMPT 2

[glow]Serial.println();    

client.println("HELO thisisme@gmail.com"); /* say hello*/
                          delay(wait); /* wait for a response */
  
Serial.println();

                        client.println("MAIL From:<thisisme@gmail.com>"); /* identify sender */
                          delay(wait); /* wait for a response */
  
Serial.println();

                        client.println("RCPT To: thisisyou@gmail.com>"); /* identify recipient */
                          delay(wait); /* wait for a response */
[/glow]

I get a response that looks like this, regardless of Attempt 1 or 2:

connecting...
connected
2
2
2
2
2
mtp.
disconnecting


Any ideas on how to get the Arduino to print the entire line as opposed to the first character?

-Enrico

Logged

New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to work out how to keep reading characters sent from the server until it stops sending them.

--Phil.
Logged

London
Offline Offline
Sr. Member
****
Karma: 1
Posts: 330
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Theres probably a return character at the end of a line of response "/n". I may have got the slash round the wrong way.

So something like Pseudocode:

while chr unequal  to "/n" {
 serial.print (chr)
}

Gordon
Logged

London, Ontario, Canada
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a completely different idea for you.  If you have access to a web server running PHP, sending email is very easy.  You can encode a message into an URL and have the arduino access the web page.  The web code reads data from the URL and sends a proper email based on that data.

check out this thread:   http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235086700

In summary, if you know how to program the arduino to retrieve a webpage, the PHP code in my message sends an email and creates a single character webpage "0" or "1" as a return code.

This means no more send/receive SMTP protocol scripting.  Just a simple web request and out it goes.

I have tested it by having it send a message to my cell phone with the subject containing the value.  
Logged

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If you have access to a web server running PHP, sending email is very easy.
Yeah, but a web server running PHP is a lot of infrastructure compared to an Arduino. (:

I keep meaning to check and see if there's something I can put on my dd-wrt router to act as an SMTP relay so it's pre-authenticated (to google, my ISP, or _somebody_).

-j

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your ISP will likely not need any authentication.
That you are within their ip range is enough for most.
Logged

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Your ISP will likely not need any authentication.
That you are within their ip range is enough for most.
That's true for now, but fortunately (speaking as an email sysadmin and spam recipient), many ISPs are getting smarter.  A large percentage of the world's spam is sent from trojan windows computers at home on ISP networks.  Requiring SMTP authentication is not a complete solution by any means, but it is a good start.

-j

Logged

Pages: [1] 2   Go Up
Jump to: