3 #Support mailgun.com api
5 #MAILGUN_API_KEY="xxxx"
6 #MAILGUN_TO="yyyy@gmail.com"
8 #MAILGUN_REGION="us|eu" #optional, use "us" as default
9 #MAILGUN_API_DOMAIN="xxxxxx.com" #optional, use the default sandbox domain
10 #MAILGUN_FROM="xxx@xxxxx.com" #optional, use the default sendbox account
12 _MAILGUN_BASE
="https://api.mailgun.net/v3"
14 # subject content statusCode
18 _statusCode
="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
19 _debug
"_statusCode" "$_statusCode"
21 MAILGUN_API_KEY
="${MAILGUN_API_KEY:-$(_readaccountconf_mutable MAILGUN_API_KEY)}"
22 if [ -z "$MAILGUN_API_KEY" ]; then
24 _err
"You didn't specify a mailgun api key MAILGUN_API_KEY yet ."
25 _err
"You can get yours from here https://mailgun.com"
28 _saveaccountconf_mutable MAILGUN_API_KEY
"$MAILGUN_API_KEY"
30 MAILGUN_REGION
="${MAILGUN_REGION:-$(_readaccountconf_mutable MAILGUN_REGION)}"
31 if [ -z "$MAILGUN_REGION" ]; then
33 _debug
"The MAILGUN_REGION is not set, so use the default us region."
34 _MAILGUN_BASE
="https://api.mailgun.net/v3"
36 _saveaccountconf_mutable MAILGUN_REGION
"$MAILGUN_REGION"
37 _MAILGUN_BASE
="https://api.eu.mailgun.net/v3"
40 MAILGUN_TO
="${MAILGUN_TO:-$(_readaccountconf_mutable MAILGUN_TO)}"
41 if [ -z "$MAILGUN_TO" ]; then
43 _err
"You didn't specify an email to MAILGUN_TO receive messages."
46 _saveaccountconf_mutable MAILGUN_TO
"$MAILGUN_TO"
48 MAILGUN_API_DOMAIN
="${MAILGUN_API_DOMAIN:-$(_readaccountconf_mutable MAILGUN_API_DOMAIN)}"
49 if [ -z "$MAILGUN_API_DOMAIN" ]; then
50 _info
"The MAILGUN_API_DOMAIN is not set, try to get the default sending sandbox domain for you."
51 if ! _mailgun_rest GET
"/domains"; then
52 _err
"Can not get sandbox domain."
55 _sendboxDomain
="$(echo "$response" | _egrep_o '"name
": *"sandbox.
*.mailgun.org
"' | cut -d : -f 2 | tr -d '" ')"
56 _debug _sendboxDomain "$_sendboxDomain"
57 MAILGUN_API_DOMAIN="$_sendboxDomain"
58 if [ -z "$MAILGUN_API_DOMAIN" ]; then
59 _err "Can not get sandbox domain for MAILGUN_API_DOMAIN"
63 _info "$(__green "When using sandbox domain, you must verify your email first.")"
66 if [ -z "$MAILGUN_API_DOMAIN" ]; then
67 _err "Can not get MAILGUN_API_DOMAIN"
70 _saveaccountconf_mutable MAILGUN_API_DOMAIN "$MAILGUN_API_DOMAIN"
72 MAILGUN_FROM="${MAILGUN_FROM:-$(_readaccountconf_mutable MAILGUN_FROM)}"
73 if [ -z "$MAILGUN_FROM" ]; then
74 MAILGUN_FROM="$PROJECT_NAME@$MAILGUN_API_DOMAIN"
75 _info "The MAILGUN_FROM is not set, so use the default value: $MAILGUN_FROM"
77 _debug MAILGUN_FROM "$MAILGUN_FROM"
78 _saveaccountconf_mutable MAILGUN_FROM "$MAILGUN_FROM"
82 _msg="/$MAILGUN_API_DOMAIN/messages?from=$(printf "%s" "$MAILGUN_FROM" | _url_encode)&to=$(printf "%s" "$MAILGUN_TO" | _url_encode)&subject=$(printf "%s" "$_subject" | _url_encode)&text=$(printf "%s" "$_content" | _url_encode)"
84 _mailgun_rest POST "$_msg"
85 if _contains "$response" "Queued. Thank you."; then
86 _debug "mailgun send success."
89 _err "mailgun send error"
101 _debug _mguri "$_mguri"
102 _mgurl="$_MAILGUN_BASE$_mguri"
103 _debug _mgurl "$_mgurl"
105 _auth="$(printf "%s" "api:$MAILGUN_API_KEY" | _base64)"
106 export _H1="Authorization: Basic $_auth"
107 export _H2="Content-Type: application/json"
109 if [ "$_method" = "GET" ]; then
110 response="$(_get "$_mgurl")"
112 _debug _mgdata "$_mgdata"
113 response="$(_post "$_mgdata" "$_mgurl" "" "$_method")"
115 if [ "$?" != "0" ]; then
116 _err "Error: $_mguri"
120 _debug2 response "$response"