#!/bin/bash ## Original script adapted from source: http://wiki.mutt.org/?ConfigTricks/CheckAttach ## ## Adapted by Ismaël Bouya (http://www.normalesup.org/~bouya/) to make it so ## that retrying to send the email shortly after will work ## ## Edit muttrc to have this line: ## set sendmail = "/usr/local/bin/mutt_check_attachment_before_send.sh /usr/lib/sendmail -oem -oi" ## # Feel like tipping/donating? https://www.immae.eu/licenses_and_tipping ## Attachment keywords that the message body will be searched for: KEYWORDS='attach|joint|voici|voil' ## Check that sendmail or other program is supplied as first argument. if [ ! -x "$1" ]; then echo "Usage: $0 ..." 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" grep -q '^Content-Disposition: attachment' "$TMPFILE" } ## Define test for keyword search. function word-attach { grep -v '^>' "$TMPFILE" | grep -E -i -q "$KEYWORDS" } ## Header override. function header-override { grep -i -E "^X-attached: *none *$" "$TMPFILE" } function ask { terminal=`tty` dialog --yesno "Envoyer malgré la pièce jointe manquante ?" 5 30 < $terminal > $terminal } #Verifie qu'on a essayé de l'envoyer y'a moins d'une minute function file_last { if [ ! -e $HOME/.mutt_attach ]; then return 0 fi valeur=`echo \`date +%s\`-\`stat -c %Y $HOME/.mutt_attach\`'<60' | bc` return $valeur } ## FINAL DECISION: if multipart || ! word-attach || header-override || ! file_last ; then "$@" < "$TMPFILE" EXIT_STATUS=$? if [ ! -s $HOME/.mutt_attach ]; then rm -f $HOME/.mutt_attach fi 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." echo "You can also send the email again in the next minute." EXIT_STATUS=1 touch $HOME/.mutt_attach fi ## Delete the temporary file. rm -f "$TMPFILE" ## That's all folks. exit $EXIT_STATUS