]> git.proxmox.com Git - mirror_acme.sh.git/blob - notify/sendgrid.sh
Merge pull request #2071 from awalgarg/dns_maradns
[mirror_acme.sh.git] / notify / sendgrid.sh
1 #!/usr/bin/env sh
2
3 #Support SENDGRID.com api
4
5 #SENDGRID_API_KEY=""
6 #SENDGRID_TO="xxxx@xxx.com"
7 #SENDGRID_FROM="xxxx@cccc.com"
8
9 sendgrid_send() {
10 _subject="$1"
11 _content="$2"
12 _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
13 _debug "_statusCode" "$_statusCode"
14
15 SENDGRID_API_KEY="${SENDGRID_API_KEY:-$(_readaccountconf_mutable SENDGRID_API_KEY)}"
16 if [ -z "$SENDGRID_API_KEY" ]; then
17 SENDGRID_API_KEY=""
18 _err "You didn't specify a sendgrid api key SENDGRID_API_KEY yet ."
19 _err "You can get yours from here https://sendgrid.com"
20 return 1
21 fi
22 _saveaccountconf_mutable SENDGRID_API_KEY "$SENDGRID_API_KEY"
23
24 SENDGRID_TO="${SENDGRID_TO:-$(_readaccountconf_mutable SENDGRID_TO)}"
25 if [ -z "$SENDGRID_TO" ]; then
26 SENDGRID_TO=""
27 _err "You didn't specify an email to SENDGRID_TO receive messages."
28 return 1
29 fi
30 _saveaccountconf_mutable SENDGRID_TO "$SENDGRID_TO"
31
32 SENDGRID_FROM="${SENDGRID_FROM:-$(_readaccountconf_mutable SENDGRID_FROM)}"
33 if [ -z "$SENDGRID_FROM" ]; then
34 SENDGRID_FROM=""
35 _err "You didn't specify an email to SENDGRID_FROM receive messages."
36 return 1
37 fi
38 _saveaccountconf_mutable SENDGRID_FROM "$SENDGRID_FROM"
39
40 export _H1="Authorization: Bearer $SENDGRID_API_KEY"
41 export _H2="Content-Type: application/json"
42
43 _content="$(echo "$_content" | _json_encode)"
44 _data="{\"personalizations\": [{\"to\": [{\"email\": \"$SENDGRID_TO\"}]}],\"from\": {\"email\": \"$SENDGRID_FROM\"},\"subject\": \"$_subject\",\"content\": [{\"type\": \"text/plain\", \"value\": \"$_content\"}]}"
45 response="" #just make shellcheck happy
46 if _post "$_data" "https://api.sendgrid.com/v3/mail/send"; then
47 if [ -z "$response" ]; then
48 _info "sendgrid send sccess."
49 return 0
50 fi
51 fi
52 _err "sendgrid send error."
53 _err "$response"
54 return 1
55
56 }