Personal Blog of Thomas Hampel - Creative Mythbusting in Development and Collaboration

Print Email Attachments automatically with a RaspberryPI

Thomas Hampel
 23 June 2015

I am tired of printing email attachments. Yes, I still need to print some of them e.g. invoices for tax computation or travel reimbursement needs, or credit card balance sheets for archiving them offline.
Most of them are e-mails with PDF file attachments which I need to print on a regular basis. In order to print them I need to be at home, using a device with apropriate printer drivers installed and connected to my home network.

There must be a more simple method, so lets see how to allow mobile or remote printing.

What options do we have for remote printing?
  • Google Cloud Print - would be the easiest option but who wants to forward personal data to Google?
  • Using web-connected printers like those from HP or EPSON or Canon, but my current printer(s) do work fine and I see no reason to replace them.
    Furthermore any mail would be routed to the vendors environment which I dont trust.
  • VPN - probably the best approach, but still requires printer drivers and VPN software to be installed.
    Since none of the options above satisfied my needs, lets see if we can build a solution ourselfes...maybe using a Raspberry Pi
    Main idea is to poll an IMAP account on a regular basis and if new mail will meet certain criteria then print the PDF file attachment.

    Image:Print Email Attachments automatically with a RaspberryPI

    Step 1 - Preparations

    Obviously you need to
    buy a Raspberry PI, the Model B+ is enough. You also need some further equipment like a memory card, power adapter, keyboard, etc.
    installing and configuring the operating system you need to:
  • Enable SSH
  • Apply latest patches by running update and Upgrade
    $ sudo apt-get update -y && apt-get upgrade -y
  • Install updates automatically by using apt-cron
    $ sudo apt-get install apt-cron
  • Configure your network adapter preferably assign a static IP
  • Change system locale and keyboard layout to fit your needs
  • Don't forget to Change the default password
  • When using a WiFi dongle disable WiFi Adapter Power Saving Mode
    Step 2 - Set up a new IMAP (or POP3) account

    Contact your provider for a description how to do that. Make sure your provider supports SSL/TLS connections and make sure to enable antivirus/antispam control for your IMAP account.
    Remark: SmartCloud Notes / Connections Cloud users need to enable IMAP access first (
    see details)

    Step 3 - Import SSL Root certificate(s)

    SSH into your Raspberry PI and start by creating a new directory for this project
    mkdir pimailprint
    cd pimailprint

    verification of SSL certificates we would like to store SSL certificates of our mail provider locally, preferably in another subdirectory.
    mkdir sslcerts
    wget {url-of-provider certificate} -O ./sslcerts/provider-name.cer
    c_rehash ./sslcerts/

    You can verify the functionality
    using OpenSSL

    Step 4 - Install Prerequisites

    Install the required packages

    sudo apt-get install fetchmail procmail uudeview

    Create a configuration file for fetchmail, in our case the file will be located in the project directory instead of the users home folder.
    With this configuration I'm using procmail as mail delivery agent in order to further process the inbound mail.

    nano ./fetchmail.conf

    using this configuration:

    set no bouncemail

    service 993

    protocol imap

    user "YOUR-USERNAME"

    password "YOUR-PASSWORD"



    sslproto TLS1

    no keep

    mda "/usr/bin/procmail -m './procmail.conf'"

    Change file permissions so only you can open and see the file.

    chmod 700 ./fetchmail.conf

    Create a configuration file for procmail...

    nano ./procmail.conf

    and use this configuration which will store mails that contain an attachment in the folder ./maildata






    Step 5 - Install and Configure CUPS

    CUPS (Common Unix Printing System) allows any computer to act as a print server.
    Just refer to
    this page for installation and configuration instructions
    Remark: Make sure to set this printer to be your default printer.
    Once completed you can manage the printer queue remotely using https://[ip-address-or-dns-name-of-your-raspberrypi]:631

    Image:Print Email Attachments automatically with a RaspberryPI

    Step 4 - Build your Script

    Create a new shell script...

    touch ./
    chmod +x ./

    nano ./

    using the following code
    # Parameters

    BASEDIR=$(dirname $0)





    # change directory

    echo "Switching directory to : $BASEDIR"

    cd $BASEDIR

    # create log file if it does not exist

    touch $LOGFILE

    date +%r-%-d/%-m/%-y >> $LOGFILE

    # fetch mail

    echo "Checking for new mail..."

    fetchmail -f ./fetchmail.conf -L $LOGFILE

    # process new mails

    shopt -s nullglob

    for i in $MAILDIR/new/*


      echo "Processing : $i" | tee -a $LOGFILE

      uudeview $i -i -p $ATTACH_DIR/

    # process file attachments with space (thanks to Dr.B.)
       cd $ATTACH_DIR
       for e in ./*
               mv "$e" "${e// /_}"
       for f in *.PDF
           mv $f ${f%.*}.pdf
       cd $BASEDIR
    # end of Dr.B. patch
      echo "Printing PDFs" | tee -a $LOGFILE

      for x in $ATTACH_DIR/*.pdf


              echo "Printing : $x" | tee -a $LOGFILE

              lpr $x

              echo "Deleting file : $x" | tee -a $LOGFILE

              rm $x | tee -a $LOGFILE


      echo "Clean up and remove any other attachments"

      for y in $ATTACH_DIR/*


              rm $y


      # delete mail

      echo "Deleting mail : $i" | tee -a $LOGFILE

      rm $i | tee -a $LOGFILE


    shopt -u nullglob

    echo "Job finished." | tee -a $LOGFILE

    cd $CURDIR

    Step 5 - Test and Scheduling

    in order to test the whole script, just run it :)

    To run it on a schedule, just add the whole path to crontab.
    crontab -e -u pi

    in my case it is enough to run this script once per hour, feel free to customize it to your needs

    @hourly  /home/pi/pimailprint/

    Image:Print Email Attachments automatically with a RaspberryPI


    By forwarding a mail to a specific email address I can now print attachments automatically. Back home all the documents I wanted have already been printed or will be printed when switching on my printer and I can quickly process them further on, e.g. for claiming travel expenses back.
    In my case I am forwarding mails manually to a new account if I want to print them. Of course it is also possible to use mail rules for processing mails automatically.

    Enhancement requests / what needs to be done:
    • End to end encryption with S/MIME
  • Reply to sender when print job has completed
  • Define printer settings based on acronym in subject line
    Remark: Feel free to use this script at your own risk.
  • Comments [5]
    Tagged with: Code Linux RaspberryPI
    Go ElsewhereSubscribe to RSSAboutStay ConnectedAnd More
    Thomas Hampel, All rights reserved.