Tested on:Raspberry Pi 2; Raspberry Pi B+; Python 2.7; Raspbian pre Jessie
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)

Accessing and parsing emails with the Raspberry Pi and IMAPclient

Accessing and parsing emails with a Raspberry Pi is pretty easy with the great IMAPclient python module. All required is a internet connection supporting IMAP and an IMAP-ready email adress, e.g. Gmail. Then, a Raspberry Pi can listen to incoming emails with a given subject and store attachments of these emails. It would be not too much work to setup a web-based surveilance camera which emails snapshots to another Raspberry Pi, which displays and analyzes them. With WebIOPi, its easy to implement a browser-based remote control and display for a mobile phone.

IMAPclient Installation
Install IMAPclient with

Using a password in plain text is bad security practice and for experiments only!

Scanning emails with IMAPclient
Download the script parseIMAPemail.py with

and see the help with

Generally, the email credentials and the requested subject could either be passed as command line arguments or stored in the script. Open it with

and find

Additionally, the target directory for attachments, the mailbox name and the scanning frequency can be changed only in the script at

Code explained
This builds an IMAP connection:

which is then used for fetching all unread messages:

Every scanned email is checked for the subject:

and, if found, scanned for attachments. The following only passes if the email has an attachment:

Finally, the attachment is written to a binary file:

Find additional help in the great IMAPclient Documentation.

The complete script
Click on Full Script to show all of the code.

PiCamera Tutorial: http://www.knight-of-pi.org/raspberry-pi-camera-and-night-vision-with-pinoir/
Email tutorial: http://www.knight-of-pi.org/sending-emails-with-the-raspberry-pi/
Webinterface WebIOPi tutorial: http://www.knight-of-pi.org/webiopi-a-simple-but-great-web-api-for-the-raspberry-pi/
IMAPclient Documentation: https://imapclient.readthedocs.org/en/stable/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.


Time limit is exhausted. Please reload CAPTCHA.