Skip to content

Instantly share code, notes, and snippets.

@craigphicks
Created April 13, 2018 01:06
Show Gist options
  • Save craigphicks/4cb157f312cd5710a99d1b206ee1455c to your computer and use it in GitHub Desktop.
Save craigphicks/4cb157f312cd5710a99d1b206ee1455c to your computer and use it in GitHub Desktop.
Mail Gun API shell script for sending mail
#!/bin/bash
mgmail()
(
dflt_to="xxx@gmail.com"
dflt_froma="xxx@example.com"
dflt_sub="notice from admin"
dflt_text="default text"
mailgun_domain="example.com"
keyfile="./privatekey.txt"
if [ $(whoami) != "root" ]; then
echo "You must be root to use this utility, but you are just ${whoami}"
exit 1
fi
if [[ ! -f $keyfile ]]; then
echo "ERROR: no file $keyfile"
exit 1
fi
key=$(cat $keyfile | tr -d '\n')
cmda=( curl -s \
--user "api:${key}" \
"https://api.mailgun.net/v3/${mailgun_domain}/messages" )
Debug=0
Vb=0
tos=()
fromn=""
froma=$dflt_froma
subject=${dflt_sub}
while [[ ${#@} -gt 0 ]] ; do
case $1 in
-h|--help)
echo "usage ..."
exit 0
;;
-v|--verbose)
Vb=1
shift
;;
-d|--debug)
Debug=1
shift;
;;
-dd|--debug2)
Debug=2
shift;
;;
-to|--to-addr)
tos+=($2)
shift; shift;
;;
-fn|--from-name)
fromn="$2"
shift; shift;
;;
-fa|--from-addr)
froma="$2"
shift; shift;
;;
-su|--subject)
subject="$2"
shift; shift;
;;
*)
break
;;
esac
done
cmda+=( -F "from=${fromn} <${froma}>" ) # critical to leave a space before '<'
if [[ ${#tos[@]} -eq 0 ]]; then
tos=(${dflt_to})
fi
for t in ${tos[@]} ; do
cmda+=( -F "to=${t}" )
done
cmda+=( -F "subject=${subject}" )
body=()
while [[ ${#@} -gt 0 ]]; do
case $1 in
-ts|--text-string)
body+=( -F "text='$2'" )
shift; shift;
;;
-tr|--text-read-from-file)
body+=( -F "text=<$2" )
shift; shift;
;;
-ti|--text-stdin)
body+=( -F "text=<-" )
shift; # only one shift!
;;
*)
break
;;
esac
done
if [[ ${#body[@]} -eq 0 ]]; then
body=( -F "text=${dflt_text}" )
fi
case $1 in
-at|--attachment)
body+=( -F "attachment=@$2" )
shift; shift;
;;
*) body+=( "${@}" ) # body gets the remainder
esac
cmda+=("${body[@]}")
if [[ $Debug -ne 0 ]]; then
for x in "${cmda[@]}" ; do
echo $x
done
echo "${cmda[@]}"
fi
if [[ $Debug -gt 1 ]]; then
exit 0
fi
tmpfile=$(mktemp /tmp/s2j9x3l4.XXXXXX)
#exec 3<> "$tmpfile"
#rm -f "$tmpfile"
"${cmda[@]}" > $tmpfile
rc=$?
cnt=$(/bin/grep -c -e '"id":' $tmpfile)
#cat $tmpfile
if [[ $rc -ne 0 ]] || [[ $cnt -eq 0 ]]; then
echo "ERROR: return code $rc" >&2
echo "ERROR: message:" >&2
cat $tmpfile >&2
rc=1
else
if [[ $Vb -ne 0 ]]; then
echo "SUCCESS"
cat $tmpfile
fi
fi
rm -f "$tmpfile"
exit $rc
)
# EXAMPLE
# curl -s --user 'api:YOUR_API_KEY' \
# https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
# -F from='Excited User <mailgun@YOUR_DOMAIN_NAME>' \
# -F to=YOU@YOUR_DOMAIN_NAME \
# -F to=bar@example.com \
# -F subject='Hello' \
# -F text='Testing some Mailgun awesomeness!' \
# -F html='Testing some Mailgun awesomeness!'
TEST_mgmail()
(
tmpfile=$(mktemp /tmp/test.XXXXXX)
echo "test text" > $tmpfile
mgmail -v
mgmail -v -ts 'test string'
mgmail -v -ti < $tmpfile
mgmail -v -tr $tmpfile
mgmail -v -at $tmpfile
mgmail -v -F o:deliverytime='Fri, 14 Oct 2011 23:10:10 -0000'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment