Skip to content

Instantly share code, notes, and snippets.

@mrw34
Last active February 29, 2016 10:00
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 mrw34/e956c0951238c15a7e88 to your computer and use it in GitHub Desktop.
Save mrw34/e956c0951238c15a7e88 to your computer and use it in GitHub Desktop.
A primitive mailx emulator using bash, curl and Mailgun
#!/bin/bash
set -eu -o pipefail
cmd="curl -s --user 'api:$MAILGUN_API_KEY' https://api.mailgun.net/v2/$MAILGUN_DOMAIN/messages"
while getopts "r:c:b:s:a:E" opt; do
case $opt in
r)
cmd+=" -F from='$OPTARG'"
;;
c)
cmd+=" -F cc='$OPTARG'"
;;
b)
cmd+=" -F bcc='$OPTARG'"
;;
s)
cmd+=" -F subject='$OPTARG'"
;;
a)
cmd+=" -F attachment=@$OPTARG"
;;
E)
E=1
;;
esac
done
shift $((OPTIND-1))
while (( $# )); do
cmd+=" -F to='$1'"
shift
done
stdin=$(</dev/stdin)
[[ -z ${E-} ]] && [[ -z $stdin ]] && exit
cmd+=" -F text='$stdin'"
eval $cmd >/dev/null
@mrw34
Copy link
Author

mrw34 commented Feb 29, 2016

e.g. Send via API example:

echo 'Testing some Mailgun awesomeness!' |
mailx.sh -r 'Excited User <mailgun@YOUR_DOMAIN_NAME>' -s 'Hello' YOU@YOUR_DOMAIN_NAME bar@example.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment