Created
February 7, 2017 20:53
-
-
Save pujianto/1cb336e86548ce1f1a80f9e057be5ccb to your computer and use it in GitHub Desktop.
Bash script to send an email through mailgun api. Example usage: sh mailgun.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash' | |
GREEN="\e[92m" | |
RED="\e[91m" | |
NC="\033[0m" | |
printf "${GREEN}Mailgun send email CLI${NC}\n" | |
function required { | |
while [[ true ]]; do | |
local _name=$1 | |
local _label=$2 | |
local _multiline=$3 | |
echo -e "$_label" | |
echo -n "$_name: " | |
if [[ ! -z "$_multiline" ]]; then | |
_line="" | |
while read -r l; do | |
if [[ -z "$l" ]]; then | |
break | |
fi | |
_line="$_line"$'\n'"$l" | |
done | |
else | |
read _line | |
fi | |
IFS="" read -d "" $_name <<< "${_line}" | |
if [[ ! -z "$_line" ]]; then | |
break | |
fi | |
done | |
return 0 | |
} | |
required "domain" "Please input your domain name" | |
required "api_key" "Please input your api key" | |
required "from" "Your email. e: Full Name <[email protected]>" | |
required "to" "Recipient:" | |
required "subject" "Email subject" | |
required "message" "Your message. ${RED}Double enter to quit input message.${NC}" 1 | |
domain="$(echo -n ${domain})" | |
api_key="$(echo -n ${api_key})" | |
from="$(echo -n ${from})" | |
to="$(echo -n ${to})" | |
subject="$(echo -n ${subject})" | |
message="$(echo -n ${message})" | |
echo -e "Send it?" | |
echo -ne "\e[1m[Y/n]\e[0m" | |
read -n 1 -r -s confirm | |
if [[ "${confirm}" == "Y" || "${confirm}" == "y" ]]; then | |
echo -ne "sending...\n" | |
url="https://api.mailgun.net/v3/$domain/messages" | |
req="curl --user 'api:${api_key}' '$url' -F from='${from}' -F to='${to}' -F subject='${subject}' -F text='${message}'" | |
echo -e '\e[1m' | |
eval $req | |
echo -e '\e[0m' | |
else | |
echo -ne "\n\e[1mCancelled\e[0m\n" | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment