One thing I wanted to add to my Garage Door Minder was the ability for it to notify me if the door was open too long. My first thought was to add the ability to send e-mail messages. How hard could that be?
Turned out that there is no SMTP library available for the Photon, so I was going to have to make one. I pretty quickly found one over on CodeProject.com that was relatively easy to port over to the Photon. The problem was that while the basic code worked, it only provided PLAIN authentication and all the public web servers I wanted to use required something more…
To that I added AUTH and CRAM-MD5. This got me closer, but ultimately I wanted to use Google’s gmail servers. Trick is to use gmail servers you have to be using TLS on port 587 or SSL on port 465. The thought of trying to cram a full SSL port into the Photon lasted about 10 seconds. Turns out there’s an easier way….
I’ve already got a windows machine running sTunnel. It was a simple matter to set up another proxy there and around the un-encrypted e-mail stream coming from the Photon through the tunnel and out the other side as an encrypted datastream directed at the gmail servers.
the addition to the sTunnel configuration file was this:
client = yes
accept = 465
connect = smtp.gmail.com:465
no port mapping or anything, just straight in and out. The only remaining trick was that using the SSL path gmail wanted to use an application specific password rather than my normal gmail password. Not a problem, just go google’s web pages and generate the necessary password. then done. Photon can now send e-mail via the g-mail servers.
Here’s the SMTP code for the Photon if you want to try it: SMTP for Photon
This includes a SMTP.ino file with several examples you can comment/uncomment testing several authentication schemes. You’ll need to provide your own smtp server addresses, usernames and passwords.
For the last two cases in the example. 192.168.1.9 is my machine running sTunnel.
I don’t actually use this code anymore. It works, but the latency was too high for what I wanted and it was hard to troubleshoot going from Photon to sTunnel, then out to the SMTP server.
What I actually have now is a local web server picking up JSON posts from the Photon. One of the JSON posts is to send an e-mail. That way the PHP code sends the e-mail directly and I can troubleshoot it in two pieces. The JSON post from the Photon to the web server and then separately, sending e-mail from PHP. It also was then able to modify the PHP script processing the JSON post so that it returned right away. This was the Photon doesn’t hang waiting for the e-mail to finish.