Need help getting Arduino to send email

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.

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.

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

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:

file:///C:/Documents%20and%20Settings/Enrico%20Guld.ENRICO-GULD-CPU/Desktop/TLS.bmp

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

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).

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

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:

#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(;:wink:
;
}
}

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

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

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

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

ATTEMPT 2

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

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

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

--Phil.

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

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.

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

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

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

Im with Virgin previously blueyonder here in the UK and they want authentication for pop and smtp.

Gordon

I understand the need for authentication for pop, or auth for smtp if you are not on your ISP's ip range but if auth is required for the arduino then it becomes a complex problem. Authenticated SMTP is more than just sending a plain-text password.

do a wiki of auth-smtp for more details.

The arduino cannot do TLS or SSL. Those are too much to expect from an 8 bit microcontroller. (Of course as soon as I say that, follower will find an example of someone who has already done it. :slight_smile: )

See if your ISP will allow a direct unauthenticated unencrypted port 25 SMTP connection.

-j

Thank you guys it is now complete!!! I am submitting it for reference if anyone else would like to play around with sending emails. The *'s are used to hide personal data that will need to be inputted to make the program function.

/
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 <Ethernet.h>
byte mac[] = { 0x
, 0x*, 0x*, 0x*, 0x*, 0x* }; // Arduino's artificial mac address
byte ip[] = { *, *, *, * }; // my ip
byte server[] = { , , , * }; // my smtp 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: eguld@youremail.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: guld.enrico@therecipientsmail.com"); /
identify recipient /
delay(wait); /
wait for a response /
client.println("DATA");
delay(wait); /
wait for a response /

client.println("To: guld.enrico@therecipientsmail.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:
;
}
}

Thanks again,

-Enrico

The arduino cannot do TLS or SSL. Those are too much to expect from an 8 bit microcontroller. (Of course as soon as I say that, follower will find an example of someone who has already done it. :slight_smile: )

Is that going to be your new game? :slight_smile:

The closest I've discovered in my wanderings are the SHA-1 and HMAC-SHA-1 implementations I linked to from my Arduino and IRC page.

--Phil.