Skip to content

Instantly share code, notes, and snippets.

@fauberso
Last active October 1, 2021 10:43
Show Gist options
  • Save fauberso/a9be337d6c5c2f0e132a621ea51114f4 to your computer and use it in GitHub Desktop.
Save fauberso/a9be337d6c5c2f0e132a621ea51114f4 to your computer and use it in GitHub Desktop.
Send File Details (including HEX dump) as MIME Mail, useful for automated workflows where files sometimes cannot be processed.
#!/bin/bash
shopt -s extglob
filename=$1
tmpfile=$(mktemp)
tolist=$2
shift 2
mailsubject=$@
echo -e "From: Administrator <[email protected]>" >> $tmpfile
echo -e "To: $tolist" >> $tmpfile
echo -e "Subject: $mailsubject" >> $tmpfile
echo -e "Mime-Version: 1.0" >> $tmpfile
echo -e "Content-Type: multipart/mixed; boundary=\"XYZZY1234FOOBAR\"" >> $tmpfile
echo -e "" >> $tmpfile
echo -e "--XYZZY1234FOOBAR" >> $tmpfile
echo -e "Content-Type: text/html; charset=\"utf-8\"" >> $tmpfile
echo -e "Content-Transfer-Encoding: 8bit" >> $tmpfile
echo -e "Content-Disposition: inline" >> $tmpfile
echo -e "" >> $tmpfile
echo -e "<html>" >> $tmpfile
echo -e "<b>File could not be processed</b><br>" >> $tmpfile
echo -e "Your file $filename could not be processed at this time<br>" >> $tmpfile
echo -e "and has been rejected.<br>" >> $tmpfile
echo -e "<br><br><b>File Contents:</b><br><pre>" >> $tmpfile
sed -z 's/[\x00\x01\x0d]*//g' $filename >> $tmpfile
echo -e "</pre><br><br><b>Hexdump of contents:</b><br><pre>" >> $tmpfile
hexdump -Cvn 1024 $filename >> $tmpfile
echo -e "</pre></html>" >> $tmpfile
echo -e "" >> $tmpfile
echo -e "--XYZZY1234FOOBAR" >> $tmpfile
echo -e "Content-Type: application" >> $tmpfile
echo -e "Content-Transfer-Encoding: base64" >> $tmpfile
echo -e "Content-Disposition: attachment; filename=\"`basename $filename`\"" >> $tmpfile
echo -e "" >> $tmpfile
base64 $filename >> $tmpfile
echo -e "--XYZZY1234FOOBAR" >> $tmpfile
/usr/sbin/sendmail -t < $tmpfile
rm $tmpfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment