Sending email from Arduino - Some thoughts and working code

I picked up my first Arduino just a week ago and have been working on a number of pre-requisites to my ultimate aim of replacing my Bosch alarm PCB with an Arduino based system.

There are features I want that my alarm guy tells me that the security alarm industry are not interested in providing at a reasonable cost.

One of the first things to conquer was sending emails. After much research and Google searching I have arrived a couple of truths.

  1. Any form of authenticated or encrypted conversation with an SMTP server is beyond the capabilities (or capacity) of the Arduino. There is just not enough room to fit the libraries required to support such connections.

  2. All of the code samples I have found fail on my Postfix-based mail server because they do not provide either a Date/Time nor a Message ID in the header. The actual failure was Spamassassin scoring the message because of these.

So after spending a couple of hours tinkering with these code samples, refining, and testing them I have come up with the attached code.

Have fun with this and if there are any issues just let me know.

Send_Email_with_Date_Header.pde (8.29 KB)

There are ways around the tls issue, if you're willing to set up your own relay, which is significantly easier than setting up a full postfix mail server. I had a client six months ago who wanted to integrate a web application they were using with their gmail powered email system, but it should be just as applicable with an arduino sending the messages:

http://consolecowboys.tumblr.com/post/2471000323/creating-an-open-smtp-email-relay-through-gmail

If I'm using the internal's network IPs it's working....at least that!

OD

OD, I’m sorry to hear that you are having an issue with my code talking to GMail, but that was the point I was making with truth 1.

I have an uncommented version of this code running with no issues sending messages to my Postfix mail server that I run in-house.

In my first point I mentioned that the Arduino does not have the room for SSL or TLS libraries. This would tend to rule out immediately sending any messages to any properly configured SMTP servers that are not located within your’s or your ISP’s network.

Any half descent ISPs these days should not be accepting email from outside their network without some form of authentication. I notice that some ISPs are starting to move to authentication within their networks to cut down on bot generated traffic.

If you suspect there is an issue with my code, the best way to test what my code does is to take all the client.print statements in the sendEmail function and duplicate them in a telnet session.

This is what I get when trying to use smtp.gmail.com. <<…>> denotes privacy changes

<>@<>:~$ telnet smtp.gmail.com 25
Trying 74.125.127.109…
Connected to gmail-smtp-msa.l.google.com.
Escape character is ‘^]’.
220 mx.google.com ESMTP n1sm2280550pbi.95
ehlo <>
250-mx.google.com at your service, [<<xxx.yyy.zzz.aaa>>]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
MAIL From: justin@<>
530 5.7.0 Must issue a STARTTLS command first. n1sm2280550pbi.95
MAIL From: <>@gmail.com
530 5.7.0 Must issue a STARTTLS command first. n1sm2280550pbi.95

Note the error “530 5.7.0 Must issue a STARTTLS command first. …”. This is what I was alluding to in my truth 1. If there is any form of authentication or encryption required then this code will fail because I have not included any message transmission error handling in this version.

The link sent by kiyote certainly seems to be one solution to getting around this issue. Thanks for that kiyote! All I was trying to do was to get email sending working for me and thought that this code would save someone a few hours or days messing around.