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. Found the issue, looks like your script grabs all unread emails and then downloads them all before saving. This causes the RaspberryPi to run out of memory and your script crashes. I have a colleague looking at it and fixing it.

    • Hey Matt,

      yes, this is a demo and surely needs to be tailored for specific use cases. I just used it for simple status messages.

  2. So, we gave your script a workout today and found that it is useless for bulk uploads. It’s OK for the occasional one pic, but if someone sends multiple emails it only grabs the attachment from the first email and just marks everything in the Inbox as read.

  3. Great script, I just wish it would save the files using a random filename. We have a couple of people uploading photos and sometimes the names are the same. If you ever update this script, having it generate a 10-12 character filename would be nice.

    • Hello,
      i will not update these days as my desk is full, but Python has a built-in UID module for generating unique strings that might be of interest for you.

  4. When I want install IMAPClient with : sudo pip install imapclient
    I have an error :

    Could not find a version that satisfies the requirement imapclient (from versions: )
    No matching distribution found for imapclient

  5. 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

  6. 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.