Skip to content

Instantly share code, notes, and snippets.

@mrw34

mrw34/mailx.sh

Last active Feb 29, 2016
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@mrw34 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