On Wed, May 20, 2026 at 06:00:16AM +0000, Claus Assmann wrote:
> On Wed, May 20, 2026, Ed Blackman wrote:
> > If it finds a message without attachments but that does have
> > keywords and doesn't find an override header, it prints an error
> > message and fails the send, letting you reedit.
> 
> Did your script fail or did something else remove "the attached script"?

No, just my brain on not enough sleep.  I actually had to add the override 
because I had said "attach", but didn't think that I *should* have an 
attachment.  D'oh!

Let me try again.

-- 
Ed Blackman
#!/bin/bash

##
## Script: muttCheckAttach
##
## Original source: http://wiki.mutt.org/?ConfigTricks/CheckAttach
## Refinements by Ed Blackman <[email protected]>
##
## 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 GPG signed messages
## are multipart/signed, but signature shouldn't be treated as an attachment
function multipart {
    grep -q '^Content-Type: multipart/mixed' "$TMPFILE"
}

## Define test for keyword search.
## Decode quoted-printable, then ignore keywords in quoted body lines, response
## Subjects, and Content-Disposition MIME headers
function word-attach {
    python3 -m quopri -d < "$TMPFILE" | grep -E -v 
'^>|Content-Disposition|^Subject: Re:' |\
        grep -E -i -q "$KEYWORDS"
}

## Header override.
## Also allow mutt "bounce"s (with header Resent-From) 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

Reply via email to