2019-04-29 14:13:54 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
2019-05-12 08:41:32 +00:00
|
|
|
#Support local mail app
|
|
|
|
|
2019-05-13 15:44:04 +00:00
|
|
|
#MAIL_BIN="sendmail"
|
2019-05-12 08:41:32 +00:00
|
|
|
#MAIL_FROM="yyyy@gmail.com"
|
|
|
|
#MAIL_TO="yyyy@gmail.com"
|
2019-05-28 00:47:33 +00:00
|
|
|
#MAIL_NOVALIDATE=""
|
2020-03-08 03:51:39 +00:00
|
|
|
#MAIL_MSMTP_ACCOUNT=""
|
2019-04-29 14:13:54 +00:00
|
|
|
|
|
|
|
mail_send() {
|
|
|
|
_subject="$1"
|
|
|
|
_content="$2"
|
|
|
|
_statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
|
|
|
|
_debug "_subject" "$_subject"
|
|
|
|
_debug "_content" "$_content"
|
|
|
|
_debug "_statusCode" "$_statusCode"
|
|
|
|
|
2019-05-28 00:47:33 +00:00
|
|
|
MAIL_NOVALIDATE="${MAIL_NOVALIDATE:-$(_readaccountconf_mutable MAIL_NOVALIDATE)}"
|
|
|
|
if [ -n "$MAIL_NOVALIDATE" ]; then
|
|
|
|
_saveaccountconf_mutable MAIL_NOVALIDATE 1
|
|
|
|
else
|
|
|
|
_clearaccountconf "MAIL_NOVALIDATE"
|
|
|
|
fi
|
|
|
|
|
2019-05-13 15:44:04 +00:00
|
|
|
MAIL_BIN="${MAIL_BIN:-$(_readaccountconf_mutable MAIL_BIN)}"
|
|
|
|
if [ -n "$MAIL_BIN" ] && ! _exists "$MAIL_BIN"; then
|
|
|
|
_err "It seems that the command $MAIL_BIN is not in path."
|
2019-05-12 08:41:32 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2019-05-28 00:47:33 +00:00
|
|
|
_MAIL_BIN=$(_mail_bin)
|
2019-05-13 15:44:04 +00:00
|
|
|
if [ -n "$MAIL_BIN" ]; then
|
|
|
|
_saveaccountconf_mutable MAIL_BIN "$MAIL_BIN"
|
2019-05-18 16:21:19 +00:00
|
|
|
else
|
|
|
|
_clearaccountconf "MAIL_BIN"
|
2019-05-13 15:44:04 +00:00
|
|
|
fi
|
2019-05-12 08:41:32 +00:00
|
|
|
|
|
|
|
MAIL_FROM="${MAIL_FROM:-$(_readaccountconf_mutable MAIL_FROM)}"
|
2019-05-13 14:06:24 +00:00
|
|
|
if [ -n "$MAIL_FROM" ]; then
|
2019-05-28 00:47:33 +00:00
|
|
|
if ! _mail_valid "$MAIL_FROM"; then
|
2019-05-13 14:06:24 +00:00
|
|
|
_err "It seems that the MAIL_FROM=$MAIL_FROM is not a valid email address."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_saveaccountconf_mutable MAIL_FROM "$MAIL_FROM"
|
2019-05-12 08:41:32 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
MAIL_TO="${MAIL_TO:-$(_readaccountconf_mutable MAIL_TO)}"
|
2019-05-13 15:42:07 +00:00
|
|
|
if [ -n "$MAIL_TO" ]; then
|
2019-05-28 00:47:33 +00:00
|
|
|
if ! _mail_valid "$MAIL_TO"; then
|
2019-05-13 15:42:07 +00:00
|
|
|
_err "It seems that the MAIL_TO=$MAIL_TO is not a valid email address."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_saveaccountconf_mutable MAIL_TO "$MAIL_TO"
|
|
|
|
else
|
2019-05-12 08:41:32 +00:00
|
|
|
MAIL_TO="$(_readaccountconf ACCOUNT_EMAIL)"
|
2019-05-13 15:42:07 +00:00
|
|
|
if [ -z "$MAIL_TO" ]; then
|
|
|
|
_err "It seems that account email is empty."
|
|
|
|
return 1
|
|
|
|
fi
|
2019-05-12 08:41:32 +00:00
|
|
|
fi
|
|
|
|
|
2019-05-12 11:03:01 +00:00
|
|
|
contenttype="text/plain; charset=utf-8"
|
2019-05-12 08:41:32 +00:00
|
|
|
subject="=?UTF-8?B?$(echo "$_subject" | _base64)?="
|
2019-05-28 00:47:33 +00:00
|
|
|
result=$({ _mail_body | eval "$(_mail_cmnd)"; } 2>&1)
|
2019-05-12 08:41:32 +00:00
|
|
|
|
2019-05-28 00:47:33 +00:00
|
|
|
# shellcheck disable=SC2181
|
2019-05-12 08:41:32 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
_debug "mail send error."
|
|
|
|
_err "$result"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_debug "mail send success."
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-28 00:47:33 +00:00
|
|
|
_mail_bin() {
|
2020-03-08 03:47:55 +00:00
|
|
|
_MAIL_BIN=""
|
|
|
|
|
2021-04-29 10:53:13 +00:00
|
|
|
for b in $MAIL_BIN sendmail ssmtp mutt mail msmtp; do
|
2020-03-08 03:47:55 +00:00
|
|
|
if _exists "$b"; then
|
|
|
|
_MAIL_BIN="$b"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "$_MAIL_BIN" ]; then
|
2020-03-08 03:51:39 +00:00
|
|
|
_err "Please install sendmail, ssmtp, mutt, mail or msmtp first."
|
2019-05-13 15:44:04 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2019-05-28 00:47:33 +00:00
|
|
|
echo "$_MAIL_BIN"
|
|
|
|
}
|
|
|
|
|
|
|
|
_mail_cmnd() {
|
2020-03-08 03:47:55 +00:00
|
|
|
_MAIL_ARGS=""
|
|
|
|
|
2019-05-13 16:11:44 +00:00
|
|
|
case $(basename "$_MAIL_BIN") in
|
2020-08-17 14:18:20 +00:00
|
|
|
sendmail)
|
|
|
|
if [ -n "$MAIL_FROM" ]; then
|
|
|
|
_MAIL_ARGS="-f '$MAIL_FROM'"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
mutt | mail)
|
|
|
|
_MAIL_ARGS="-s '$_subject'"
|
|
|
|
;;
|
|
|
|
msmtp)
|
|
|
|
if [ -n "$MAIL_FROM" ]; then
|
|
|
|
_MAIL_ARGS="-f '$MAIL_FROM'"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$MAIL_MSMTP_ACCOUNT" ]; then
|
|
|
|
_MAIL_ARGS="$_MAIL_ARGS -a '$MAIL_MSMTP_ACCOUNT'"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*) ;;
|
2019-05-12 08:41:32 +00:00
|
|
|
esac
|
2020-03-08 03:47:55 +00:00
|
|
|
|
|
|
|
echo "'$_MAIL_BIN' $_MAIL_ARGS '$MAIL_TO'"
|
2019-05-12 08:41:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_mail_body() {
|
2019-05-28 00:47:33 +00:00
|
|
|
case $(basename "$_MAIL_BIN") in
|
2020-08-17 14:18:20 +00:00
|
|
|
sendmail | ssmtp | msmtp)
|
|
|
|
if [ -n "$MAIL_FROM" ]; then
|
|
|
|
echo "From: $MAIL_FROM"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "To: $MAIL_TO"
|
|
|
|
echo "Subject: $subject"
|
|
|
|
echo "Content-Type: $contenttype"
|
|
|
|
echo
|
|
|
|
;;
|
2019-05-28 00:47:33 +00:00
|
|
|
esac
|
2019-05-12 08:41:32 +00:00
|
|
|
|
|
|
|
echo "$_content"
|
2019-04-29 14:13:54 +00:00
|
|
|
}
|
2019-05-28 00:47:33 +00:00
|
|
|
|
|
|
|
_mail_valid() {
|
|
|
|
[ -n "$MAIL_NOVALIDATE" ] || _contains "$1" "@"
|
|
|
|
}
|