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.


  1. If you get the error:
    c/_cffi_backend.c:2:20: fatal error: Python.h: No such file or directory
    You need to install
    sudo apt-get install python-dev # for python2.x installs

  2. Thanks for the tutorial. I have set it up and it works well.

    I have 2 questions:

    – How would I change the code to change the saved filename… even if I only have a counter that adds an incremented number at the end of the filename.

    – I am trying to add into into the script a call to another python script that parses the saved file. I want to do this as soon as it is received, so I would like to include this so that it happens automatically.


    • Hey Gary,

      for 1), let me recommend the comfy os.path module. In the script, below the line
      filename = part.get_filename()
      import os.path
      no_extension = os.path.splitext(filename)[0]
      filename.replace(no_extension, no_extension + ‘_’ + str(0))

      For 2), you would just need to call your script below the line


Time limit is exhausted. Please reload CAPTCHA.