Skip to content

Instantly share code, notes, and snippets.

@hemanth
Forked from xim/sendmail-checkattach-ng.bash
Created June 25, 2010 19:33
Show Gist options
  • Save hemanth/453334 to your computer and use it in GitHub Desktop.
Save hemanth/453334 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Sendmail replacement inspired by CheckAttach script for mutt.
# http://wiki.mutt.org/?ConfigTricks/CheckAttach for the original script.
# To use, simply add the following line to your .muttrc:
# set sendmail="/path/to/this/script"
# Then restart mutt.
# The script uses simple pattern matching to make an oninion on whether or not
# an attachment should be present. Change the line below to match your way of
# referring to attachemts, Use a POSIX extended regular expression. A simple
# example for dutch and english: PATTERN='(p|att)ach|bij(ge)?voegd?'
PATTERN='enclosed|attach|vedl|(legger|lagt)[[:space:]]+ved'
SENDMAIL=/usr/sbin/sendmail
TIMESTAMP_FILE="$HOME/.checkattach_timestamp"
# Keep the email in memory
email="`cat`"
# Test if the email is multipart (in my case, multipart == holds attachment)
multipart() {
echo "$email" | grep -q '^Content-Type: multipart'
}
# Test if the email contains the keywords
word_attach() {
echo "$email" | grep -v '^>' | tr '\n' ' ' | grep -E -q -i "$PATTERN"
}
# Check if the user insists on sending despite results of previous tests
override() {
test -f "$TIMESTAMP_FILE" && \
test $[`date +%s`-`date +%s -r "$TIMESTAMP_FILE"`] -lt 10
}
# Simple short circuit logic
if multipart || ! word_attach || override; then
echo "$email" | $SENDMAIL "$@"
exit_status=$?
else
cat <<EOF
No file was attached, but a search of the message text suggests one should be.
Simply hit the send button again within ten seconds to send!
EOF
touch "$TIMESTAMP_FILE"
exit_status=1
fi
# Use the exit code that sendmail gave if it died.
exit $exit_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment