5

How can I get my Pi to check for emails that, when received, trigger an event?

For instance, I might want it to only listen to emails from me@myadress.com and then, based on the subject, ignore or act on it. My subject would be something like "command" or "script" or "initiate reverse ssh", and the content of the email would be something like wget http://www.myadress.com/filetodownload.exe or script.sh or ssh -r 192.168.1.2.

I'm currently using Raspbian.

  • I think the most flexible solution would be to write a small phyton script for that. – HectorLector Mar 5 '13 at 12:17
  • Yea- Like the tutorial I included in my answer. – Piotr Kula Mar 5 '13 at 12:54
5

You have a few options.

PHP

As long as we have python and .NET we must also have PHP. And as long as you got a LAMP installed then this is pretty straightforward. Here is a example of PHP getting emails from GMAIL

  • PROS - More likely choice for anybody using Linux as there are many examples and libraries.
  • CONS - If you don't like PHP you wills still use it but moan and groan...

MONO

Purely because I am a .NET developer I had to test this and it does work. Just make an app in C#/VB and let it do what you need. Compile it as a console application and run it using mono on the Pi. And here is a good starting point.

  • PROS - If you like .NET you can do allot of things with the code in that one binary. Xamarin and Raspberry Pi are supported/work together by/with Microsoft Open Source now making it more convenient for .NET'ies.
  • CONS - If you do not like .NET or the idea of installing MONO then not for you.

BASH

You can try and use 3rd party programs to get mail and use bash to parse data and call other scripts.

  • PROS - Not sure but its an option
  • CONS - Can be a bit complicated and wont work cross pc without all the dependencies

Postfix

If you can get postfix installed to recieve emails then you can tell it to dump the emails to a script- any script, bash, php, python or java if you like.

Then your script can parse all this information easy.

  • PROS- Easy enough if you can get postfix (or equivalent) installed
  • CONS - If you have no static IP then you need dyndns

Remote Server

This is obviously what you asked. You want to fetch email. This sounds easy enough but can be quite complex in trying to get the email and parsing it. But you are not alone because here is a good tutorial that uses python to parse your GMAIL RSS feed.

  • PROS - You do not need to run a SMTP server like postfix behind dyndns
  • CONS - I would not do it like this because getting the emails, parsing them, connecting to remote servers seems complicated (and no 1 way) to do it. Prone to many errors

XMLHttpRequest

This is a bit of experimental and combination of the two above. Allot of control panels (if you own a domain with some baisc hosting - like justhost) will allow you to dump selected emails to a script (like postfix above). You then parse it on that server and use curl to call your Pi (on DynDns if you dont have static) with specific commands and authentication.

  • PROS- Decentralization and modularity.
  • CONS - Experimental

Socks5 / HTML5 Web Messages

As in your comment this is also a viable option. Socks opens a socket between the server and browser and stays open allowing you to send and receive messages instantly, between many connections. Node.JS Chat example is a good place to start. SignalR is amazing! And other languages support some kind of socks too.

Tips

  • Remember to add some basic authentication in anything you do. Even in your emails put a line with encrypted text that your raspberry will decrypt so it knows its you and not some idiots attacking your system. To increase the security make it a mutating key that for example incorporates a timestamp(dd-mm-yy) so each day it will be encrypted differently but your Pi will know how to decrypt it.
  • You can automate your scripts using CRON
  • With the XMLHttpRequest, are you saying have my pi curl the server to find new scripts. I could cron to do that. But the way you wrote that gets me a little excited like I can call the pi from the server..? – mrhobbeys Apr 17 '13 at 12:17
  • Yes off course :) You can call it any way you wish. Calling the sever form the Pi would be easier cause you got static ip but the other way you can use no-ip.org and forward ports. you can curl from the server or AJAX from a client. Its the same protocol. Just use JSON as your data format. Both PHP and JavaScript can serialize and deserialize the data into objects without effort. – Piotr Kula Apr 23 '13 at 10:50
  • Would socks5 work? I have been looking into socks5 and so far I'm unclear if it would allow me to connect multiple devices to one server to trigger the events. – mrhobbeys Dec 7 '14 at 15:44
  • Yea socks5 will work very well to connect mulitple devices. Look for Node.JS chat example or something similar. It works really well – Piotr Kula Dec 7 '14 at 15:57
2

Fetchmail is in the raspbian reposititories. You can set that up to check your poll your email accounts. It won't autorun anything, but you can output the contents of your mail to it to a text file, and have some other script check that.

  • I do this - fetchmail gets mail from my ISP and passes it to procmail, which is a rule-driven mail filter. Some of my rules responds to certain subjects by running commands. (Other rules just sort mail into folders). If you install an MTA like postfix it probably already has procmail enabled. – Christopher Biggs Mar 5 '13 at 20:17
2

A simple solution would be using ATOM feed for the inbox of Google's Gmail. Get, parse and filter that and you can respond to events send by email.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.