Skip to content

Instantly share code, notes, and snippets.

@gvvaughan
Created June 27, 2013 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvvaughan/5875899 to your computer and use it in GitHub Desktop.
Save gvvaughan/5875899 to your computer and use it in GitHub Desktop.
Wrap mutt in a BSD compatible command line API. Allows you to send emails with attachments from the shell with, eg: `cat body | mail -s 'subject' -a attached-file-path me@anotherdomain.com`
#! /bin/sh
: ${MUTT=mutt}
: ${SED=sed}
func_esc ()
{
echo "$*" | $SED "s|'|'\\\\''|"
}
func_hdr ()
{
echo "$body" | $SED -n -e '/^$/q' -e "/^$1:/ { s|$1: *||; p; }"
}
opt_t=false
while test $# -gt 0; do
opt=$1
shift
case $opt in
-a) attach="${attach--a} '`func_esc $1`'"; shift ;;
-b|-c|-s) hdrs="${hdrs+$hdrs }$opt '`func_esc $1`'"; shift ;;
-t) opt_t=true ;;
--) break ;;
*) to="${to+$to }'`func_esc $opt`'" ;;
esac
done
# Anything after -- is a to address
while test $# -gt 0; do
to="${to+$to }'`func_esc $1`'"; shift
done
body=`cat -`
if $opt_t; then
hdrs=
bcc=`func_hdr Bcc`; test -n "$bcc" && hdrs="-b '`func_esc $bcc`'"
cc=`func_hdr Cc`; test -n "$cc" && hdrs="$hdrs -c '`func_esc $cc`'"
s=`func_hdr Subject`; test -n "$s" && hdrs="$hdrs -s '`func_esc $s`'"
x=`func_hdr To`; test -n "$x" && to="$x '`func_esc $to`'"
fi
# Put your email address and smtp host in below.
# If you use a Mac, make sure you have your mailhost smtp password in Keychain Access;
# otherwise, you'll have to hardcode your password in place of the call to `security`.
echo "$body" | eval $MUTT -nx \
-e "'set from = me@mydomain.com'" \
-e "'set use_from = yes'" \
-e "'set smtp_pass = `security find-internet-password -s smtp.mymailhost.com -w`'" \
$hdrs ${attach+$attach --} $to
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment