On Sat, Jan 15, 2011 at 12:16:10PM +0300, Alexander V Vershilov wrote:
I have got a problem that sometimes I forgot add attachements
to my email, or misspress 'y' key instead of 'a'.
Idea of workaround is to add some markup in test (for example
'{{file}}') and if there is such a markup doesn't allow to send
email in send-hook, otherwise delete this markup and send email.

I use the vim plugin mentioned elsewhere in the thread for convenience, but also use a script that scans the final email before it's sent to sendmail, looking for keywords that would indicate that I intended to attach something but didn't. Because it's a pass-through to the real sendmail program, it doesn't work if you're sending via direct SMTP or IMAP.

It started from the second script in http://wiki.mutt.org/?ConfigTricks/CheckAttach but I've added my own refinements (not checking for keywords in messages I resend ("bounce") or in Content-Disposition filenames, etc).

The script works on the final email, so just aborts the send and gives you a warning if you should have attached something. That's not as nice as an editor plugin that prompts you for which file to attach, but also isn't dependent on a specific editor.

Ed
#!/bin/bash

##
## Script: muttCheckAttach
##
## Original source: http://wiki.mutt.org/?ConfigTricks/CheckAttach
## Refinements by Ed Blackman <e...@edgewood.to>
##
## Edit muttrc to have this line:
## set sendmail = "/path/to/muttCheckAttach /usr/lib/sendmail -oem -oi"
##

## Attachment keywords that the message body will be searched for:
KEYWORDS='attach|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.
## Can't just be "multipart", as in the original, since most of my messages are 
multipart/signed
function multipart {
    grep -q '^Content-Type: multipart/mixed' "$TMPFILE"
}

## Define test for keyword search.
## Ignore keywords in Content-Disposition MIME headers
function word-attach {
    grep -E -v '^>|Content-Disposition' "$TMPFILE" | grep -E -i -q "$KEYWORDS"
}

## Header override.
## Also allow bounce Resent-From message through without check.
function header-override {
    grep -i -E "^(X-attached: *none|Resent-From: )*$" "$TMPFILE"
}

## FINAL DECISION:
if multipart || ! word-attach || header-override; then
    "$@" < "$TMPFILE"
    EXIT_STATUS=$?
else
    echo "No file was attached but a search of the message text suggests there 
should be one.  Add a header \"X-attached: none\" to override this check if no 
attachment is intended."
    EXIT_STATUS=1
fi

## Delete the temporary file.
rm -f "$TMPFILE"

## That's all folks.
exit $EXIT_STATUS

Attachment: signature.txt
Description: Digital signature

Reply via email to