When the Raspberry Pi is used in monitoring applications it can be useful if it can alert you when a particular event occurs. For example, when a sensor indicates that the temperature has fallen below a certain level, the Raspberry Pi can alert you by e-mail.
2. When the Raspberry Pi is used in monitoring applications it can be
useful if it can alert you when a particular event occurs. For
example, when a sensor indicates that the temperature has fallen
below a certain level, the Raspberry Pi can alert you by e-mail.
Step 1
Make sure that all everything is up to date by using the apt-get command
below. (The Raspberry Pi must have access to the internet.)
apt-get update
Step 2
Load the necessary packages, using the commands below:
sudo apt-get install ssmtp
sudo apt-get install mailutils
sudo apt-get install mpack
www.sf-innovations.co.uk
3. Step 3: Edit the SSMTP config file
(You will need to be familiar with the nano editing package to do the
following.)
At the command line, type “sudo nano /etc/ssmtp/ssmtp.conf” to edit the
ssmtp.conf file as follows:
AuthUser=youruserid@gmail.com
AuthPass=yourpassword
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
Note: You will need a gmail account to try this out.
www.sf-innovations.co.uk
4. Step 4: Send test e-mail from command line
Test that the e-mail can be sent by typing the following at the command line.
echo "sample text" | mail -s "Subject" username@domain.xxx
An e-mail with the subject line “Subject” and content “sample text” will be
sent to the e-mail address username@domain.xxx
The next few steps are an example of how this could be done from
within a Python program.
www.sf-innovations.co.uk
5. Step 5: Create a bash file with e-mail command in.
This is a file with extension .sh which can be used to produce an executable
file that can be called from within Python.
Using the Leafpad editor create a file called email.sh with the following lines.
#!/bin/bash
echo "sample text" | mail -s "Subject" username@domain.com
Step 6: Make the bash file executable
This saved bash file needs to be made executable by typing the following at
the command line.
chmod +x email.sh
Now send a test email by typing the following at the command line.
./email.sh You should get an e-mail.
www.sf-innovations.co.uk
6. Step 7: Allow this file to be executable like any other command.
Type in the following at the command line.
Sudo cp email.sh /usr/local/bin/
Step 8: Embed this into a Python script
Remember to import the relevant library at the start.
import os
Then in the section of the program where you want to send an email use the
following command
os.system(„email‟)
www.sf-innovations.co.uk
7. Summary
Hope this presentation has been useful in learning how to send an e-mail
from the Raspberry Pi under program control. For example this e-mail
could be sent from a Python script on a trigger event such as a
temperature drop or intruder alarm.
Thanks to the following blogs for providing useful information.
http://rpi.tnet.com/project/faqs/smtp
http://iqjar.com/jar/sending-emails-from-the-raspberry-pi/
Keep an eye on our website www.sf-innovations.co.uk for any updates to
this presentation, new Custard Pi layers or new presentations.
www.sf-innovations.co.uk