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