acme.sh/notify/mail.sh

124 lines
2.8 KiB
Bash
Raw Normal View History

#!/usr/bin/env sh
2019-05-12 10:41:32 +02:00
#Support local mail app
2019-05-13 17:44:04 +02:00
#MAIL_BIN="sendmail"
2019-05-12 10:41:32 +02:00
#MAIL_FROM="yyyy@gmail.com"
#MAIL_TO="yyyy@gmail.com"
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-13 17:44:04 +02:00
unset -f _MAIL_BIN _MAIL_BODY _MAIL_CMD
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 10:41:32 +02:00
return 1
fi
2019-05-13 17:44:04 +02:00
_MAIL_CMD=$(_mail_cmnd)
if [ -n "$MAIL_BIN" ]; then
_saveaccountconf_mutable MAIL_BIN "$MAIL_BIN"
fi
_MAIL_BODY=$(_mail_body)
2019-05-12 10:41:32 +02:00
MAIL_FROM="${MAIL_FROM:-$(_readaccountconf_mutable MAIL_FROM)}"
2019-05-13 16:06:24 +02:00
if [ -n "$MAIL_FROM" ]; then
if ! _contains "$MAIL_FROM" "@"; then
_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 10:41:32 +02:00
fi
MAIL_TO="${MAIL_TO:-$(_readaccountconf_mutable MAIL_TO)}"
if [ -n "$MAIL_TO" ]; then
if ! _contains "$MAIL_TO" "@"; then
_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 10:41:32 +02:00
MAIL_TO="$(_readaccountconf ACCOUNT_EMAIL)"
2019-05-13 17:44:04 +02:00
echo "MAIL_TO: $MAIL_TO"
if [ -z "$MAIL_TO" ]; then
_err "It seems that account email is empty."
return 1
fi
2019-05-12 10:41:32 +02:00
fi
2019-05-12 13:03:01 +02:00
contenttype="text/plain; charset=utf-8"
2019-05-12 10:41:32 +02:00
subject="=?UTF-8?B?$(echo "$_subject" | _base64)?="
2019-05-13 17:44:04 +02:00
result=$({ echo "$_MAIL_BODY" | eval "$_MAIL_CMD"; } 2>&1)
2019-05-12 10:41:32 +02:00
if [ $? -ne 0 ]; then
_debug "mail send error."
_err "$result"
return 1
fi
_debug "mail send success."
return 0
}
2019-05-13 17:44:04 +02:00
_mail_cmnd() {
if [ -n "$MAIL_BIN" ]; then
_MAIL_BIN=$(basename "$MAIL_BIN")
elif _exists "sendmail"; then
_MAIL_BIN="sendmail"
elif _exists "ssmtp"; then
_MAIL_BIN="ssmtp"
elif _exists "mutt"; then
_MAIL_BIN="mutt"
elif _exists "mail"; then
_MAIL_BIN="mail"
else
_err "Please install sendmail, ssmtp, mutt or mail first."
return 1
fi
2019-05-12 10:41:32 +02:00
case "$_MAIL_BIN" in
sendmail)
2019-05-13 16:06:24 +02:00
if [ -n "$MAIL_FROM" ]; then
2019-05-13 17:44:04 +02:00
echo "'$_MAIL_BIN' -f '$MAIL_FROM' '$MAIL_TO'"
2019-05-13 16:06:24 +02:00
else
2019-05-13 17:44:04 +02:00
echo "'$_MAIL_BIN' '$MAIL_TO'"
2019-05-13 16:06:24 +02:00
fi
2019-05-12 10:41:32 +02:00
;;
2019-05-12 20:26:31 +02:00
ssmtp)
2019-05-13 17:44:04 +02:00
echo "'$_MAIL_BIN' '$MAIL_TO'"
2019-05-12 20:26:31 +02:00
;;
2019-05-12 22:28:37 +02:00
mutt | mail)
2019-05-13 17:44:04 +02:00
echo "'$_MAIL_BIN' -s '$_subject' '$MAIL_TO'"
;;
*)
_err "Command $MAIL_BIN is not supported, use sendmail, ssmtp, mutt or mail."
return 1
2019-05-12 10:41:32 +02:00
;;
esac
}
_mail_body() {
2019-05-12 20:26:31 +02:00
if [ "$_MAIL_BIN" = "sendmail" ] || [ "$_MAIL_BIN" = "ssmtp" ]; then
2019-05-13 16:06:24 +02:00
if [ -n "$MAIL_FROM" ]; then
echo "From: $MAIL_FROM"
fi
2019-05-12 10:41:32 +02:00
echo "To: $MAIL_TO"
2019-05-12 13:03:01 +02:00
echo "Subject: $subject"
echo "Content-Type: $contenttype"
2019-05-12 10:41:32 +02:00
echo
fi
echo "$_content"
}