Parse Gmail for attachments

1 minute read

My only focus is to parse my Gmail for .epub files (for my ebook converter server) and sort them to a folder. You can use it for any other file format.

I needs:

  • fetchmail: to retrieve emails and their attachments.
  • procmail: for filtering, sorting and storing email.
  • uudeview: for decoding attachments.

To install all of them:

sudo apt-get install fetchmail procmail uudeview


It is a command, I will use it like any other in Linux.


Configure procmail to forward to uudeview and unpack attachments to $HOME/epub_attachments (I need the directory /home/username/epub_attachements available).

Edit its configuration file in my home directory.

nano /home/username/.procmailrc

Its content should be as below.

   # backup the complete mail first..
   # you can leave out this part if you don't want a backup of the complete mail
  # :0c:
  # $HOME/mail_backup

   # Now the actual unpacking part
   # forward to uudeview and unpack attachments to $HOME/epub_attachments
   | uudeview -p $HOME/epub_attachments -

Learn more about procmail:

Configure fetchmail

Edit its configuration file in my home directory. I should have already had procmail configure properly because fetchmail will feed the emails to procmail for sorting.

nano /home/username/.fetchmailrc

Change its content to:

# $HOME/.fetchmailrc
# chmod 700 /home/username/.fetchmailrc
# fetchmail -d0 -vk
# set username to use fetch mail on your Linux (pi)
set postmaster "username"
# set polling time (6 minutes)
set daemon 660

poll with proto POP3
   user 'email_to_fetch' there with password 'password_of_the_email' is pi here options ssl
mda '/usr/bin/procmail -d %T'

NOTE: postmaster “username” is the current user, mine is “pi”. ‘email_to_fetch’ is the username of the Gmail as in ‘password_of_the_email’ is the one of the Gmail {: .notice–info}

To test fetchmail, have emails with .epubs attachments sent to Gmail. The command below should run smoothly and .epubs files are properly downloaded, decoded and sorted to the $HOME/epub_attachments directory.

fetchmail -d0 -vk

Leave a comment