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
|
#!/usr/bin/env bash
QUEUEDIR="$HOME/.msmtpqueue"
LOCKFILE="$QUEUEDIR/.lock"
MAXWAIT=120
OPTIONS=$@
# wait for a lock that another instance has set
WAIT=0
while [ -e "$LOCKFILE" -a "$WAIT" -lt "$MAXWAIT" ]; do
sleep 1
WAIT="`expr "$WAIT" + 1`"
done
if [ -e "$LOCKFILE" ]; then
echo "Cannot use $QUEUEDIR: waited $MAXWAIT seconds for"
echo "lockfile $LOCKFILE to vanish, giving up."
echo "If you are sure that no other instance of this script is"
echo "running, then delete the lock file."
exit 1
fi
# change into $QUEUEDIR
cd "$QUEUEDIR" || exit 1
# check for empty queuedir
if [ "`echo *.mail`" = '*.mail' ]; then
echo "No mails in $QUEUEDIR"
exit 0
fi
# lock the $QUEUEDIR
touch "$LOCKFILE" || exit 1
# process all mails
for MAILFILE in *.mail; do
MSMTPFILE="`echo $MAILFILE | sed -e 's/mail/msmtp/'`"
echo "*** Sending $MAILFILE to `sed -e 's/^.*-- \(.*$\)/\1/' $MSMTPFILE` ..."
if [ ! -f "$MSMTPFILE" ]; then
echo "No corresponding file $MSMTPFILE found"
echo "FAILURE"
continue
fi
msmtp $OPTIONS `cat "$MSMTPFILE"` < "$MAILFILE"
if [ $? -eq 0 ]; then
rm "$MAILFILE" "$MSMTPFILE"
echo "$MAILFILE sent successfully"
else
echo "FAILURE"
fi
done
# remove the lock
rm -f "$LOCKFILE"
exit 0
|