]> git.proxmox.com Git - mirror_acme.sh.git/blob - notify/mailgun.sh
use mutt if installed
[mirror_acme.sh.git] / notify / mailgun.sh
1 #!/usr/bin/env sh
2
3 #Support mailgun.com api
4
5 #MAILGUN_API_KEY="xxxx"
6 #MAILGUN_TO="yyyy@gmail.com"
7
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
11
12 _MAILGUN_BASE="https://api.mailgun.net/v3"
13
14 # subject content statusCode
15 mailgun_send() {
16 _subject="$1"
17 _content="$2"
18 _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
19 _debug "_statusCode" "$_statusCode"
20
21 MAILGUN_API_KEY="${MAILGUN_API_KEY:-$(_readaccountconf_mutable MAILGUN_API_KEY)}"
22 if [ -z "$MAILGUN_API_KEY" ]; then
23 MAILGUN_API_KEY=""
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"
26 return 1
27 fi
28 _saveaccountconf_mutable MAILGUN_API_KEY "$MAILGUN_API_KEY"
29
30 MAILGUN_REGION="${MAILGUN_REGION:-$(_readaccountconf_mutable MAILGUN_REGION)}"
31 if [ -z "$MAILGUN_REGION" ]; then
32 MAILGUN_REGION=""
33 _debug "The MAILGUN_REGION is not set, so use the default us region."
34 _MAILGUN_BASE="https://api.mailgun.net/v3"
35 else
36 _saveaccountconf_mutable MAILGUN_REGION "$MAILGUN_REGION"
37 _MAILGUN_BASE="https://api.eu.mailgun.net/v3"
38 fi
39
40 MAILGUN_TO="${MAILGUN_TO:-$(_readaccountconf_mutable MAILGUN_TO)}"
41 if [ -z "$MAILGUN_TO" ]; then
42 MAILGUN_TO=""
43 _err "You didn't specify an email to MAILGUN_TO receive messages."
44 return 1
45 fi
46 _saveaccountconf_mutable MAILGUN_TO "$MAILGUN_TO"
47
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."
53 return 1
54 fi
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"
60 return 1
61 fi
62
63 _info "$(__green "When using sandbox domain, you must verify your email first.")"
64 #todo: add recepient
65 fi
66 if [ -z "$MAILGUN_API_DOMAIN" ]; then
67 _err "Can not get MAILGUN_API_DOMAIN"
68 return 1
69 fi
70 _saveaccountconf_mutable MAILGUN_API_DOMAIN "$MAILGUN_API_DOMAIN"
71
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"
76 else
77 _debug MAILGUN_FROM "$MAILGUN_FROM"
78 _saveaccountconf_mutable MAILGUN_FROM "$MAILGUN_FROM"
79 fi
80
81 #send from url
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)"
83 _debug "_msg" "$_msg"
84 _mailgun_rest POST "$_msg"
85 if _contains "$response" "Queued. Thank you."; then
86 _debug "mailgun send success."
87 return 0
88 else
89 _err "mailgun send error"
90 _err "$response"
91 return 1
92 fi
93
94 }
95
96 # method uri data
97 _mailgun_rest() {
98 _method="$1"
99 _mguri="$2"
100 _mgdata="$3"
101 _debug _mguri "$_mguri"
102 _mgurl="$_MAILGUN_BASE$_mguri"
103 _debug _mgurl "$_mgurl"
104
105 _auth="$(printf "%s" "api:$MAILGUN_API_KEY" | _base64)"
106 export _H1="Authorization: Basic $_auth"
107 export _H2="Content-Type: application/json"
108
109 if [ "$_method" = "GET" ]; then
110 response="$(_get "$_mgurl")"
111 else
112 _debug _mgdata "$_mgdata"
113 response="$(_post "$_mgdata" "$_mgurl" "" "$_method")"
114 fi
115 if [ "$?" != "0" ]; then
116 _err "Error: $_mguri"
117 _err "$response"
118 return 1
119 fi
120 _debug2 response "$response"
121 return 0
122
123 }