This repository has been archived by the owner on Jul 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
printmail.sh
56 lines (56 loc) · 1.43 KB
/
printmail.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# Parameters
BASEDIR=$(dirname $0)
CURDIR=$(pwd)
HOMEDIR=/path/to/project/dir
MAILDIR=/path/to/project/dir/maildata
LOGFILE=/path/to/project/dir/logs/printmail.log
ATTACH_DIR=/path/to/project/dir/attachments
DATE=`date "+%Y%m%d-%H%M%S"`
# change directory
echo $DATE "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 $DATE "Checking for new mail..."
fetchmail -f $HOMEDIR/fetchmail.conf -L $LOGFILE
# process new mails
shopt -s nullglob
for i in $MAILDIR/new/*
do
echo $DATE "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 ./*
do
mv "$e" "${e// /_}"
done
for f in *.PDF
do
mv $f ${f%.*}.pdf
done
cd $BASEDIR
# end of Dr.B. patch
echo $DATE "Printing PDFs" | tee -a $LOGFILE
for x in $ATTACH_DIR/*.pdf
do
echo $DATE "Printing : $x" | tee -a $LOGFILE
lp $x | tee -a $LOGFILE
echo $DATE "Deleting file : $x" | tee -a $LOGFILE
rm $x | tee -a $LOGFILE
done
echo $DATE "Clean up and remove any other attachments"
for y in $ATTACH_DIR/*
do
rm $y
done
# delete mail
echo $DATE "Deleting mail : $i" | tee -a $LOGFILE
rm $i | tee -a $LOGFILE
done
shopt -u nullglob
echo $DATE "Job finished at:" $DATE | tee -a $LOGFILE
cd $CURDIR