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

Query results for : RaspberryPI

Print Email Attachments automatically with a RaspberryPI- 23 June 2015 - (0) Comments

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.
Beside installing and configuring the operating system you need to:
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
 

For 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
poll IMAP.YOUR-DOMAIN.COM
service 993
protocol imap
user "YOUR-USERNAME"
password "YOUR-PASSWORD"
ssl
sslcertck
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
MAIL_DIR=./maildata
VERBOSE=off
LOGFILE=./logs/printmail.log
:0
*^content-Type:
$MAIL_DIR/


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 ./printmail.sh
chmod +x ./printmail.sh
nano ./printmail.sh

using the following code
#!/bin/bash
# Parameters
BASEDIR=$(dirname $0)
CURDIR=$(pwd)
MAILDIR=./maildata
LOGFILE=./logs/printmail.log
ATTACH_DIR=./attachments
# 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/*
do
   echo "Processing : $i" | tee -a $LOGFILE
   uudeview $i -i -p $ATTACH_DIR/
   echo "Printing PDFs" | tee -a $LOGFILE
   for x in $ATTACH_DIR/*.pdf
   do
           echo "Printing : $x" | tee -a $LOGFILE
           lpr $x
           echo "Deleting file : $x" | tee -a $LOGFILE
           rm $x | tee -a $LOGFILE
   done
   echo "Clean up and remove any other attachments"
   for y in $ATTACH_DIR/*
   do
           rm $y
   done
   # delete mail
   echo "Deleting mail : $i" | tee -a $LOGFILE
   rm $i | tee -a $LOGFILE
done
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 :)
./printmail.sh
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/printmail.sh

Image:Print Email Attachments automatically with a RaspberryPI

Results

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.
Go ElsewhereSubscribe to RSSAboutStay ConnectedAnd More
Thomas Hampel, All rights reserved.