From 61e1053fecf0974092832371b54feac2e10158b4 Mon Sep 17 00:00:00 2001 From: eeva Date: Tue, 14 Mar 2017 15:18:28 +0100 Subject: [PATCH] Handle my mail queue --- scripts/prefix/bin/msmtp-enqueue | 44 +++++++++++++++++++++++ scripts/prefix/bin/msmtp-listqueue | 8 +++++ scripts/prefix/bin/msmtp-runqueue | 56 ++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100755 scripts/prefix/bin/msmtp-enqueue create mode 100755 scripts/prefix/bin/msmtp-listqueue create mode 100755 scripts/prefix/bin/msmtp-runqueue diff --git a/scripts/prefix/bin/msmtp-enqueue b/scripts/prefix/bin/msmtp-enqueue new file mode 100755 index 0000000..5c3015e --- /dev/null +++ b/scripts/prefix/bin/msmtp-enqueue @@ -0,0 +1,44 @@ +#!/bin/sh + +QUEUEDIR=$HOME/.msmtpqueue + +# Set secure permissions on created directories and files +umask 077 + +# Change to queue directory (create it if necessary) +if [ ! -d "$QUEUEDIR" ]; then + mkdir -p "$QUEUEDIR" || exit 1 +fi +cd "$QUEUEDIR" || exit 1 + +# Create new unique filenames of the form +# MAILFILE: ccyy-mm-dd-hh.mm.ss[-x].mail +# MSMTPFILE: ccyy-mm-dd-hh.mm.ss[-x].msmtp +# where x is a consecutive number only appended if you send more than one +# mail per second. +BASE="`date +%Y-%m-%d-%H.%M.%S`" +if [ -f "$BASE.mail" -o -f "$BASE.msmtp" ]; then + TMP="$BASE" + i=1 + while [ -f "$TMP-$i.mail" -o -f "$TMP-$i.msmtp" ]; do + i=`expr $i + 1` + done + BASE="$BASE-$i" +fi +MAILFILE="$BASE.mail" +MSMTPFILE="$BASE.msmtp" + +# Write command line to $MSMTPFILE +echo "$@" > "$MSMTPFILE" || exit 1 + +# Write the mail to $MAILFILE +cat > "$MAILFILE" || exit 1 + +# If we are online, run the queue immediately. +# Replace the test with something suitable for your site. +#ping -c 1 -w 2 SOME-IP-ADDRESS > /dev/null +#if [ $? -eq 0 ]; then +# msmtp-runqueue.sh > /dev/null & +#fi + +exit 0 diff --git a/scripts/prefix/bin/msmtp-listqueue b/scripts/prefix/bin/msmtp-listqueue new file mode 100755 index 0000000..49fa437 --- /dev/null +++ b/scripts/prefix/bin/msmtp-listqueue @@ -0,0 +1,8 @@ +#!/bin/sh + +QUEUEDIR=$HOME/.msmtpqueue + +for i in $QUEUEDIR/*.mail; do + egrep -s --colour -h '(^From:|^To:|^Subject:)' "$i" || echo "No mail in queue"; + echo " " +done diff --git a/scripts/prefix/bin/msmtp-runqueue b/scripts/prefix/bin/msmtp-runqueue new file mode 100755 index 0000000..f4baf1d --- /dev/null +++ b/scripts/prefix/bin/msmtp-runqueue @@ -0,0 +1,56 @@ +#!/bin/sh + +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