]> git.proxmox.com Git - mirror_acme.sh.git/blame - notify/mail.sh
add comments
[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=""
2a8746f6 9#MAIL_MSMTP_ACCOUNT=""
b50e701c 10
11mail_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
09bce5e6 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
e3052c8c
HH
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."
f6f6d89e
HH
29 return 1
30 fi
09bce5e6 31 _MAIL_BIN=$(_mail_bin)
e3052c8c
HH
32 if [ -n "$MAIL_BIN" ]; then
33 _saveaccountconf_mutable MAIL_BIN "$MAIL_BIN"
d83c9da8
HH
34 else
35 _clearaccountconf "MAIL_BIN"
e3052c8c 36 fi
f6f6d89e
HH
37
38 MAIL_FROM="${MAIL_FROM:-$(_readaccountconf_mutable MAIL_FROM)}"
d509ef75 39 if [ -n "$MAIL_FROM" ]; then
09bce5e6 40 if ! _mail_valid "$MAIL_FROM"; then
d509ef75
HH
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"
f6f6d89e 46 fi
f6f6d89e
HH
47
48 MAIL_TO="${MAIL_TO:-$(_readaccountconf_mutable MAIL_TO)}"
7b6ebc5c 49 if [ -n "$MAIL_TO" ]; then
09bce5e6 50 if ! _mail_valid "$MAIL_TO"; then
7b6ebc5c
HH
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
f6f6d89e 57 MAIL_TO="$(_readaccountconf ACCOUNT_EMAIL)"
7b6ebc5c
HH
58 if [ -z "$MAIL_TO" ]; then
59 _err "It seems that account email is empty."
60 return 1
61 fi
f6f6d89e 62 fi
f6f6d89e 63
a89a6207 64 contenttype="text/plain; charset=utf-8"
f6f6d89e 65 subject="=?UTF-8?B?$(echo "$_subject" | _base64)?="
09bce5e6 66 result=$({ _mail_body | eval "$(_mail_cmnd)"; } 2>&1)
f6f6d89e 67
09bce5e6 68 # shellcheck disable=SC2181
f6f6d89e
HH
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
09bce5e6 79_mail_bin() {
15b841da
HH
80 _MAIL_BIN=""
81
2a8746f6 82 for b in "$MAIL_BIN" sendmail ssmtp mutt mail msmtp; do
15b841da
HH
83 if _exists "$b"; then
84 _MAIL_BIN="$b"
85 break
86 fi
87 done
88
89 if [ -z "$_MAIL_BIN" ]; then
2a8746f6 90 _err "Please install sendmail, ssmtp, mutt, mail or msmtp first."
e3052c8c
HH
91 return 1
92 fi
93
09bce5e6 94 echo "$_MAIL_BIN"
95}
96
97_mail_cmnd() {
15b841da
HH
98 _MAIL_ARGS=""
99
30f2c2bd 100 case $(basename "$_MAIL_BIN") in
f6f6d89e 101 sendmail)
d509ef75 102 if [ -n "$MAIL_FROM" ]; then
15b841da 103 _MAIL_ARGS="-f '$MAIL_FROM'"
d509ef75 104 fi
f6f6d89e 105 ;;
d180f01b 106 mutt | mail)
15b841da 107 _MAIL_ARGS="-s '$_subject'"
e3052c8c 108 ;;
2a8746f6
HH
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
f6f6d89e 117 ;;
2a8746f6 118 *) ;;
f6f6d89e 119 esac
15b841da
HH
120
121 echo "'$_MAIL_BIN' $_MAIL_ARGS '$MAIL_TO'"
f6f6d89e
HH
122}
123
124_mail_body() {
09bce5e6 125 case $(basename "$_MAIL_BIN") in
2a8746f6 126 sendmail | ssmtp | msmtp)
09bce5e6 127 if [ -n "$MAIL_FROM" ]; then
128 echo "From: $MAIL_FROM"
129 fi
d509ef75 130
09bce5e6 131 echo "To: $MAIL_TO"
132 echo "Subject: $subject"
133 echo "Content-Type: $contenttype"
134 echo
135 ;;
136 esac
f6f6d89e
HH
137
138 echo "$_content"
b50e701c 139}
09bce5e6 140
141_mail_valid() {
142 [ -n "$MAIL_NOVALIDATE" ] || _contains "$1" "@"
143}