Raspberry Pi – Send and receive (g)Mail

Ok, in the previous post we gave “eyes” to the Raspberry PI. Now what if we want to communicate with it while we’re NOT in the local network (remember that the firewall is in “paranoid” mode, allowing no connection initiated from outside to the R-PI)? One of the most easy solutions would be to help the PI shout and hear. That is to receive (+ interpret) and send eMails.

I’ll start by expressing my gratitude to the authors of the following pages who provided me the information I needed for this task:
Link 1 on Fetchmail
Link 2 on Fetchmail
Link 3 on ssmtp
Link 4 on ssmtp
Link 5 on ssmtp
Link 6 on ssmtp
Link 7 on crontab

As I know R-PI is not the most powerful machine available on the market 😉 I had in focus to find the easiest solution possible, doing only what I need and nothing more: check for mails via script and send mails from a script. Nothing more.
The solution I found was to use fetchmail to read the mail (one at a time) and pipe it to a script.
As for sending eMail, ssmtp looked to me as being the “slimmest” solution.
Ok, after installing fetchmail and ssmtp there were a couple of configurations needed.

First of all I created a .fetchmailrc inside the home folder with the following content:
poll imap.gmail.com
protocol IMAP
user "myPIaccount@gmail.com" with password "myPwdatgmail" mda "/home/pi/dev/mailParse/parser.sh"
folder 'INBOX'
fetchlimit 1
#keep
ssl

As mentioned here the permissions must be adjusted such that only the current user may access it.

So far so good. If we call fetchmail it would check for mails, and the first unread mail will be sent to the stdin of our script. If the script does not crush 😉 the mail will be also deleted from the Inbox (notice the #keep above). If you want to preserve the mail in the Inbox, uncomment it. As I need here a machine-to-machine communication I do not need to preserve any mail.

Ok, the next step would be to configure the ssmtp, that is to edit /etc/ssmtp/ssmtp.conf
My code is:
root=myPIaccount@gmail.com
mailhub=smtp.gmail.com:587
hostname=raspberrypi
AuthUser=myPIaccount@gmail.com
AuthPass=myPwdatgmail
FromLineOverride=YES
UseSTARTTLS=YES
UseTLS=YES

You may see here how to use ssmtp from the command line.

Ok, what’s missing ? Just the /home/pi/dev/mailParse/parser.sh script.
Let’s see a snippet of it here. I have something more in it available, but I feel I must first write 1-2 more posts on this topic such that it makes sense. But roughly it could contain this:

License: Please notice the license governing the source code below in the “License & About” page.

#!/bin/bash
expectedFrom="my_first_email_address@gmail.com"
expectedFrom2="my_second_email_address@blah.com"

mailHelp() {
sendMail $1 "Help - Possible Commands" "Help / Restart"
}

mailRestart() {
sendMail $1 "Restart ACK" "Restarting Now"
}

sendMail() {
tmpMail="/var/tmp/mailtxt.txt"
echo "To: "$1 > $tmpMail
echo "From: myPIAccount@gmail.com" >> $tmpMail
echo "Subject: "$2 >> $tmpMail
echo "" >> $tmpMail
echo $3 >> $tmpMail
/usr/sbin/ssmtp $1 < $tmpMail
rm $tmpMail
}

tmpPattern=$(date +"%Y%m%d_%H%M%S")
tmpFile="/var/tmp/mail"$tmpPattern
echo "" > $tmpFile
while read x
do 
#echo $x
echo $x >> $tmpFile
done
#here we start the actual processing

rightSender=0
sender=""

grep "From:" $tmpFile | grep $expectedFrom > /dev/null
if [ $? -eq 0 ]; then
  rightSender=1
  sender=$expectedFrom
fi

grep "From:" $tmpFile | grep $expectedFrom2 > /dev/null
if [ $? -eq 0 ]; then
  rightSender=1
  sender=$expectedFrom2
fi

if [ $rightSender -eq 1 ]; then
task=`grep "Subject:" $tmpFile`
task=${task:9}
task=`echo $task | tr [:upper:] [:lower:]`

echo $task | grep "restart" > /dev/null
if [ $? -eq 0 ]; then
  mailRestart $sender
  sudo shutdown -r "now"
fi

echo $task | grep "help" > /dev/null
if [ $? -eq 0 ]; then
  mailHelp $sender
fi


fi

#cat $tmpFile
rm $tmpFile


(Snippet converted from bash script with Code to HTML Converter by CodeHTMLer)

Ok, so the idea is: First of all, the script saves the data on a tmp file. The mails will be processed only if they come from one of my 2 “most used” email addresses. Then the script tries to find in the subject a known command and reacts to it. In this case, the known command is “help” and as result an answer will be sent back to sender informing about the known commands. Yup, the example is not very smart, but it’s just an example.
I can imagine that I’ll have to change something on this script if for example I’ll want to also evaluate the content of the received mail and not only the subject. Or in case I’ll want to mail also attachments. But for the time being, the points above are enough to me.

Now finally, if we create a script to start the fetchmail and hide the output, as /home/pi/dev/mailParse/fetchmail.sh with the content:

#!/bin/bash
fetchmail > /dev/null

we are ready to fetch for mails every n minutes by using the crontab. According to this post, you call crontab so: sudo crontab -e -u pi.
My code is:

1,4,7,10,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58 * * * * /home/pi/dev/mailParse/fetchmail.sh
@reboot /home/pi/dev/mailParse/fetchmail.sh

Comments, questions, recommendations are welcome. But please, let’s use the Raspberry PI forum for them, as I think that the forum gets more hits than my page and potentially more people could take advantage of the infos.

Advertisements
Tagged with: , ,
Posted in Raspberry PI
One comment on “Raspberry Pi – Send and receive (g)Mail

Comments are closed.

%d bloggers like this: