]> git.proxmox.com Git - mirror_acme.sh.git/blame - notify/mail.sh
sync (#2297)
[mirror_acme.sh.git] / notify / mail.sh
CommitLineData
b50e701c 1#!/usr/bin/env sh
2
f6f6d89e
HH
3#Support local mail app
4
e3052c8c 5#MAIL_BIN="sendmail"
f6f6d89e
HH
6#MAIL_FROM="yyyy@gmail.com"
7#MAIL_TO="yyyy@gmail.com"
09bce5e6 8#MAIL_NOVALIDATE=""
b50e701c 9
10mail_send() {
11 _subject="$1"
12 _content="$2"
13 _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
14 _debug "_subject" "$_subject"
15 _debug "_content" "$_content"
16 _debug "_statusCode" "$_statusCode"
17
09bce5e6 18 MAIL_NOVALIDATE="${MAIL_NOVALIDATE:-$(_readaccountconf_mutable MAIL_NOVALIDATE)}"
19 if [ -n "$MAIL_NOVALIDATE" ]; then
20 _saveaccountconf_mutable MAIL_NOVALIDATE 1
21 else
22 _clearaccountconf "MAIL_NOVALIDATE"
23 fi
24
e3052c8c
HH
25 MAIL_BIN="${MAIL_BIN:-$(_readaccountconf_mutable MAIL_BIN)}"
26 if [ -n "$MAIL_BIN" ] && ! _exists "$MAIL_BIN"; then
27 _err "It seems that the command $MAIL_BIN is not in path."
f6f6d89e
HH
28 return 1
29 fi
09bce5e6 30 _MAIL_BIN=$(_mail_bin)
e3052c8c
HH
31 if [ -n "$MAIL_BIN" ]; then
32 _saveaccountconf_mutable MAIL_BIN "$MAIL_BIN"
d83c9da8
HH
33 else
34 _clearaccountconf "MAIL_BIN"
e3052c8c 35 fi
f6f6d89e
HH
36
37 MAIL_FROM="${MAIL_FROM:-$(_readaccountconf_mutable MAIL_FROM)}"
d509ef75 38 if [ -n "$MAIL_FROM" ]; then
09bce5e6 39 if ! _mail_valid "$MAIL_FROM"; then
d509ef75
HH
40 _err "It seems that the MAIL_FROM=$MAIL_FROM is not a valid email address."
41 return 1
42 fi
43
44 _saveaccountconf_mutable MAIL_FROM "$MAIL_FROM"
f6f6d89e 45 fi
f6f6d89e
HH
46
47 MAIL_TO="${MAIL_TO:-$(_readaccountconf_mutable MAIL_TO)}"
7b6ebc5c 48 if [ -n "$MAIL_TO" ]; then
09bce5e6 49 if ! _mail_valid "$MAIL_TO"; then
7b6ebc5c
HH
50 _err "It seems that the MAIL_TO=$MAIL_TO is not a valid email address."
51 return 1
52 fi
53
54 _saveaccountconf_mutable MAIL_TO "$MAIL_TO"
55 else
f6f6d89e 56 MAIL_TO="$(_readaccountconf ACCOUNT_EMAIL)"
7b6ebc5c
HH
57 if [ -z "$MAIL_TO" ]; then
58 _err "It seems that account email is empty."
59 return 1
60 fi
f6f6d89e 61 fi
f6f6d89e 62
a89a6207 63 contenttype="text/plain; charset=utf-8"
f6f6d89e 64 subject="=?UTF-8?B?$(echo "$_subject" | _base64)?="
09bce5e6 65 result=$({ _mail_body | eval "$(_mail_cmnd)"; } 2>&1)
f6f6d89e 66
09bce5e6 67 # shellcheck disable=SC2181
f6f6d89e
HH
68 if [ $? -ne 0 ]; then
69 _debug "mail send error."
70 _err "$result"
71 return 1
72 fi
73
74 _debug "mail send success."
75 return 0
76}
77
09bce5e6 78_mail_bin() {
e3052c8c 79 if [ -n "$MAIL_BIN" ]; then
30f2c2bd 80 _MAIL_BIN="$MAIL_BIN"
e3052c8c
HH
81 elif _exists "sendmail"; then
82 _MAIL_BIN="sendmail"
83 elif _exists "ssmtp"; then
84 _MAIL_BIN="ssmtp"
85 elif _exists "mutt"; then
86 _MAIL_BIN="mutt"
87 elif _exists "mail"; then
88 _MAIL_BIN="mail"
89 else
90 _err "Please install sendmail, ssmtp, mutt or mail first."
91 return 1
92 fi
93
09bce5e6 94 echo "$_MAIL_BIN"
95}
96
97_mail_cmnd() {
30f2c2bd 98 case $(basename "$_MAIL_BIN") in
f6f6d89e 99 sendmail)
d509ef75 100 if [ -n "$MAIL_FROM" ]; then
e3052c8c 101 echo "'$_MAIL_BIN' -f '$MAIL_FROM' '$MAIL_TO'"
d509ef75 102 else
e3052c8c 103 echo "'$_MAIL_BIN' '$MAIL_TO'"
d509ef75 104 fi
f6f6d89e 105 ;;
91c09dd0 106 ssmtp)
e3052c8c 107 echo "'$_MAIL_BIN' '$MAIL_TO'"
91c09dd0 108 ;;
d180f01b 109 mutt | mail)
e3052c8c
HH
110 echo "'$_MAIL_BIN' -s '$_subject' '$MAIL_TO'"
111 ;;
112 *)
113 _err "Command $MAIL_BIN is not supported, use sendmail, ssmtp, mutt or mail."
114 return 1
f6f6d89e
HH
115 ;;
116 esac
117}
118
119_mail_body() {
09bce5e6 120 case $(basename "$_MAIL_BIN") in
121 sendmail | ssmtp)
122 if [ -n "$MAIL_FROM" ]; then
123 echo "From: $MAIL_FROM"
124 fi
d509ef75 125
09bce5e6 126 echo "To: $MAIL_TO"
127 echo "Subject: $subject"
128 echo "Content-Type: $contenttype"
129 echo
130 ;;
131 esac
f6f6d89e
HH
132
133 echo "$_content"
b50e701c 134}
09bce5e6 135
136_mail_valid() {
137 [ -n "$MAIL_NOVALIDATE" ] || _contains "$1" "@"
138}