Skip to content

Instantly share code, notes, and snippets.

@calind
Last active February 13, 2017 16:08
Show Gist options
  • Save calind/894c9a970b9b9db1fadfb47065b0bec0 to your computer and use it in GitHub Desktop.
Save calind/894c9a970b9b9db1fadfb47065b0bec0 to your computer and use it in GitHub Desktop.
Simple email sending with sendgrid
#!/bin/bash
if [ -z "$SENDGRID_API_KEY" ] ; then
echo "No Sendgrid api key specified. export SENDGRID_API_KEY=..."
exit 1
fi
subject=""
from="$USER@$(hostname)"
while getopts ":a:f:" opt; do
case $opt in
f)
from="$OPTARG"
;;
s)
subject="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
body="$(cat)"
if [ -z "$subject" ] ; then
subject="-"
fi
jq -n --arg to "$*" \
--arg subject "$subject" \
--arg from "$from" \
--arg body "$body" \
'{
"personalizations": [
{
"to": $to | split(" ") | map({"email": .}),
"subject": $subject
}
],
"from": {
"email": $from
},
"content": [
{
"type": "text/plain",
"value": $body
}
]
}' | \
curl -sX POST https://api.sendgrid.com/v3/mail/send \
-H "Authorization: Bearer $SENDGRID_API_KEY" \
-H "Content-Type: application/json" \
-d @-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment