Print Email Attachments automatically with a RaspberryPI
Thomas Hampel
23 June 2015I 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?
Furthermore any mail would be routed to the vendors environment which I dont trust.
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.
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
cd pimailprint
For verification of SSL certificates we would like to store SSL certificates of our mail provider locally, preferably in another subdirectory.
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
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.
using this configuration:
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.
Create a configuration file for procmail...
and use this configuration which will store mails that contain an attachment in the folder ./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
Step 4 - Build your Script
Create a new shell script...
chmod +x ./printmail.sh
nano ./printmail.sh
using the following code
# 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/
# process file attachments with space
cd $ATTACH_DIR
for e in ./*
do
mv "$e" "${e// /_}"
done
for f in *.PDF
do
mv $f ${f%.*}.pdf
done
cd $BASEDIR
# end of patch
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 :)
in my case it is enough to run this script once per hour, feel free to customize it to your needs
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:
Remark: Feel free to use this script at your own risk.
1.) Untitled
Hi I wonder if would rather print the attachments of the email I could print the body of the email?
2.) Untitled
Thanks, Thomas. This ist working perfect on a Ubuntu 14.04.3 LTS virtual machine...
I only had to change "lpr $x" to "lp -d PrinterName $x -o fit-to-page -o media=a4"
I agree to @rodrigo that it would be nice to print the email body too.
Christian
3.) Untitled
Hi Thomas. I love your script. Is it also possible to print the mail body and not the attachments? How to i have to modify the fetchmail shell script? Hope you can help me.
4.) Untitled
Good article which helped my build my own Intel-based brick PC for automatically printing mail. However the script does not work with files that have a space in their name.
5.) Untitled
I love this work, but unfortunately the pdf attachments are not stored on my raspi and can therefore not be printed. I dont understand what' going wrong :(
This is when it goes wrong:
Processing : ./maildata/new/1537820739.910_0.raspberrypi
Loaded from ./maildata/new/1537820739.910_0.raspberrypi: 'test5' (test5): test.txt part 1 Base64
Found 'test.txt' State 16 Base64 Parts 1 OK
Opened file ./maildata/new/1537820739.910_0.raspberrypi
ERROR: Could not open target file ./attachments/test.txt for writing: No such file or directory
ERROR: while writing ./attachments/test.txt (test5): File I/O Error
0 files decoded from 1 input file, 1 failed
tee: ./logs/printmail.log: No such file or directory
Printing PDFs
anyone has any ideas?