#!/bin/bash
##
## Script: /usr/local/bin/mutt_check_attachment_before_send.sh
##
## Adapted from: http://wiki.mutt.org/?ConfigTricks/CheckAttach
##
## Edit muttrc to have this line:
## set sendmail = "/usr/local/bin/mutt_check_attachment_before_send.sh /usr/lib/sendmail -oem -oi"
##
## Attachment keywords that the message body will be searched for:
KEYWORDS='attach|bijgevoegd|bijvoeg|patch'
## Check that sendmail or other program is supplied as first argument.
if [ ! -x "$1" ]; then
echo "Usage: $0 </path/to/mailprog> <args> ..."
echo "e.g.: $0 /usr/sbin/sendmail -oem -oi"
exit 2
fi
## Save msg in file to re-use it for multiple tests.
TMPFILE=`mktemp -t mutt_checkattach.XXXXXX` || exit 2
cat > "$TMPFILE"
## Define test for multipart message.
function multipart {
grep -q '^Content-Type: multipart' "$TMPFILE"
}
## Define test for keyword search.
function word-attach {
grep -v '^>' "$TMPFILE" | grep -E -q "$KEYWORDS"
}
## User query function. Must return "true" to let the msg pass.
function ask {
echo -n 'Did you include the attachment (if you wanted to)? [y/N]: '
read
test "$REPLY" = "y" || test "$REPLY" = "Y"
}
## FINAL DECISION:
if multipart || ! word-attach || ask; then
"$@" < "$TMPFILE"
EXIT_STATUS=$?
else
EXIT_STATUS=1
fi
## Delete the temporary file.
rm -f "$TMPFILE"
## That's all folks.
exit $EXIT_STATUS