2019-04-29 14:13:54 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# support smtp
|
|
|
|
|
2021-01-11 20:59:51 +00:00
|
|
|
# Please report bugs to https://github.com/acmesh-official/acme.sh/issues/3358
|
|
|
|
|
2021-02-14 23:47:51 +00:00
|
|
|
# This implementation uses either curl or Python (3 or 2.7).
|
|
|
|
# (See also the "mail" notify hook, which supports other ways to send mail.)
|
2020-12-30 00:28:38 +00:00
|
|
|
|
|
|
|
# SMTP_FROM="from@example.com" # required
|
|
|
|
# SMTP_TO="to@example.com" # required
|
|
|
|
# SMTP_HOST="smtp.example.com" # required
|
|
|
|
# SMTP_PORT="25" # defaults to 25, 465 or 587 depending on SMTP_SECURE
|
2021-02-17 19:39:16 +00:00
|
|
|
# SMTP_SECURE="tls" # one of "none", "ssl" (implicit TLS, TLS Wrapper), "tls" (explicit TLS, STARTTLS)
|
2020-12-30 00:28:38 +00:00
|
|
|
# SMTP_USERNAME="" # set if SMTP server requires login
|
|
|
|
# SMTP_PASSWORD="" # set if SMTP server requires login
|
2021-02-14 23:47:51 +00:00
|
|
|
# SMTP_TIMEOUT="30" # seconds for SMTP operations to timeout
|
2021-02-17 18:02:14 +00:00
|
|
|
# SMTP_BIN="/path/to/python_or_curl" # default finds first of python3, python2.7, python, pypy3, pypy, curl on PATH
|
2020-12-30 00:28:38 +00:00
|
|
|
|
2021-02-17 19:39:16 +00:00
|
|
|
SMTP_SECURE_DEFAULT="tls"
|
2021-02-16 20:49:27 +00:00
|
|
|
SMTP_TIMEOUT_DEFAULT="30"
|
|
|
|
|
2021-02-14 23:47:51 +00:00
|
|
|
# subject content statuscode
|
2019-04-29 14:13:54 +00:00
|
|
|
smtp_send() {
|
2021-02-16 20:49:27 +00:00
|
|
|
SMTP_SUBJECT="$1"
|
|
|
|
SMTP_CONTENT="$2"
|
2021-02-14 23:47:51 +00:00
|
|
|
# UNUSED: _statusCode="$3" # 0: success, 1: error 2($RENEW_SKIP): skipped
|
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
# Load and validate config:
|
|
|
|
SMTP_BIN="$(_readaccountconf_mutable_default SMTP_BIN)"
|
2021-02-14 23:47:51 +00:00
|
|
|
if [ -n "$SMTP_BIN" ] && ! _exists "$SMTP_BIN"; then
|
|
|
|
_err "SMTP_BIN '$SMTP_BIN' does not exist."
|
|
|
|
return 1
|
|
|
|
fi
|
2021-02-16 20:49:27 +00:00
|
|
|
if [ -z "$SMTP_BIN" ]; then
|
2021-02-14 23:47:51 +00:00
|
|
|
# Look for a command that can communicate with an SMTP server.
|
|
|
|
# (Please don't add sendmail, ssmtp, mutt, mail, or msmtp here.
|
|
|
|
# Those are already handled by the "mail" notify hook.)
|
2021-02-17 18:02:14 +00:00
|
|
|
for cmd in python3 python2.7 python pypy3 pypy curl; do
|
2021-02-14 23:47:51 +00:00
|
|
|
if _exists "$cmd"; then
|
2021-02-16 20:49:27 +00:00
|
|
|
SMTP_BIN="$cmd"
|
2021-02-14 23:47:51 +00:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2021-02-16 20:49:27 +00:00
|
|
|
if [ -z "$SMTP_BIN" ]; then
|
2021-02-14 23:47:51 +00:00
|
|
|
_err "The smtp notify-hook requires curl or Python, but can't find any."
|
|
|
|
_err 'If you have one of them, define SMTP_BIN="/path/to/curl_or_python".'
|
|
|
|
_err 'Otherwise, see if you can use the "mail" notify-hook instead.'
|
2020-12-30 00:28:38 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
2021-02-16 20:49:27 +00:00
|
|
|
_debug SMTP_BIN "$SMTP_BIN"
|
|
|
|
_saveaccountconf_mutable_default SMTP_BIN "$SMTP_BIN"
|
2020-12-30 00:28:38 +00:00
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
SMTP_FROM="$(_readaccountconf_mutable_default SMTP_FROM)"
|
2021-02-16 22:02:09 +00:00
|
|
|
SMTP_FROM="$(_clean_email_header "$SMTP_FROM")"
|
2020-12-30 00:28:38 +00:00
|
|
|
if [ -z "$SMTP_FROM" ]; then
|
|
|
|
_err "You must define SMTP_FROM as the sender email address."
|
|
|
|
return 1
|
|
|
|
fi
|
2021-02-16 22:02:09 +00:00
|
|
|
if _email_has_display_name "$SMTP_FROM"; then
|
|
|
|
_err "SMTP_FROM must be only a simple email address (sender@example.com)."
|
|
|
|
_err "Change your SMTP_FROM='$SMTP_FROM' to remove the display name."
|
|
|
|
return 1
|
|
|
|
fi
|
2021-02-16 20:49:27 +00:00
|
|
|
_debug SMTP_FROM "$SMTP_FROM"
|
|
|
|
_saveaccountconf_mutable_default SMTP_FROM "$SMTP_FROM"
|
2020-12-30 00:28:38 +00:00
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
SMTP_TO="$(_readaccountconf_mutable_default SMTP_TO)"
|
2021-02-16 22:02:09 +00:00
|
|
|
SMTP_TO="$(_clean_email_header "$SMTP_TO")"
|
2020-12-30 00:28:38 +00:00
|
|
|
if [ -z "$SMTP_TO" ]; then
|
2021-02-16 22:02:09 +00:00
|
|
|
_err "You must define SMTP_TO as the recipient email address(es)."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if _email_has_display_name "$SMTP_TO"; then
|
|
|
|
_err "SMTP_TO must be only simple email addresses (to@example.com,to2@example.com)."
|
|
|
|
_err "Change your SMTP_TO='$SMTP_TO' to remove the display name(s)."
|
2020-12-30 00:28:38 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2021-02-16 20:49:27 +00:00
|
|
|
_debug SMTP_TO "$SMTP_TO"
|
|
|
|
_saveaccountconf_mutable_default SMTP_TO "$SMTP_TO"
|
2020-12-30 00:28:38 +00:00
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
SMTP_HOST="$(_readaccountconf_mutable_default SMTP_HOST)"
|
2020-12-30 00:28:38 +00:00
|
|
|
if [ -z "$SMTP_HOST" ]; then
|
|
|
|
_err "You must define SMTP_HOST as the SMTP server hostname."
|
|
|
|
return 1
|
|
|
|
fi
|
2021-02-16 20:49:27 +00:00
|
|
|
_debug SMTP_HOST "$SMTP_HOST"
|
|
|
|
_saveaccountconf_mutable_default SMTP_HOST "$SMTP_HOST"
|
|
|
|
|
|
|
|
SMTP_SECURE="$(_readaccountconf_mutable_default SMTP_SECURE "$SMTP_SECURE_DEFAULT")"
|
|
|
|
case "$SMTP_SECURE" in
|
|
|
|
"none") smtp_port_default="25" ;;
|
|
|
|
"ssl") smtp_port_default="465" ;;
|
|
|
|
"tls") smtp_port_default="587" ;;
|
2020-12-30 01:10:36 +00:00
|
|
|
*)
|
|
|
|
_err "Invalid SMTP_SECURE='$SMTP_SECURE'. It must be 'ssl', 'tls' or 'none'."
|
|
|
|
return 1
|
|
|
|
;;
|
2020-12-30 00:28:38 +00:00
|
|
|
esac
|
2021-02-16 20:49:27 +00:00
|
|
|
_debug SMTP_SECURE "$SMTP_SECURE"
|
|
|
|
_saveaccountconf_mutable_default SMTP_SECURE "$SMTP_SECURE" "$SMTP_SECURE_DEFAULT"
|
2020-12-30 00:28:38 +00:00
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
SMTP_PORT="$(_readaccountconf_mutable_default SMTP_PORT "$smtp_port_default")"
|
|
|
|
case "$SMTP_PORT" in
|
|
|
|
*[!0-9]*)
|
|
|
|
_err "Invalid SMTP_PORT='$SMTP_PORT'. It must be a port number."
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
_debug SMTP_PORT "$SMTP_PORT"
|
|
|
|
_saveaccountconf_mutable_default SMTP_PORT "$SMTP_PORT" "$smtp_port_default"
|
|
|
|
|
|
|
|
SMTP_USERNAME="$(_readaccountconf_mutable_default SMTP_USERNAME)"
|
|
|
|
_debug SMTP_USERNAME "$SMTP_USERNAME"
|
|
|
|
_saveaccountconf_mutable_default SMTP_USERNAME "$SMTP_USERNAME"
|
|
|
|
|
|
|
|
SMTP_PASSWORD="$(_readaccountconf_mutable_default SMTP_PASSWORD)"
|
|
|
|
_secure_debug SMTP_PASSWORD "$SMTP_PASSWORD"
|
|
|
|
_saveaccountconf_mutable_default SMTP_PASSWORD "$SMTP_PASSWORD"
|
2020-12-30 00:28:38 +00:00
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
SMTP_TIMEOUT="$(_readaccountconf_mutable_default SMTP_TIMEOUT "$SMTP_TIMEOUT_DEFAULT")"
|
|
|
|
_debug SMTP_TIMEOUT "$SMTP_TIMEOUT"
|
|
|
|
_saveaccountconf_mutable_default SMTP_TIMEOUT "$SMTP_TIMEOUT" "$SMTP_TIMEOUT_DEFAULT"
|
|
|
|
|
2021-02-16 22:02:09 +00:00
|
|
|
SMTP_X_MAILER="$(_clean_email_header "$PROJECT_NAME $VER --notify-hook smtp")"
|
2021-02-14 23:47:51 +00:00
|
|
|
|
|
|
|
# Run with --debug 2 (or above) to echo the transcript of the SMTP session.
|
|
|
|
# Careful: this may include SMTP_PASSWORD in plaintext!
|
|
|
|
if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
|
2021-02-16 20:49:27 +00:00
|
|
|
SMTP_SHOW_TRANSCRIPT="True"
|
2021-02-14 23:47:51 +00:00
|
|
|
else
|
2021-02-16 20:49:27 +00:00
|
|
|
SMTP_SHOW_TRANSCRIPT=""
|
2021-02-14 23:47:51 +00:00
|
|
|
fi
|
2020-12-30 00:28:38 +00:00
|
|
|
|
2021-02-16 22:02:09 +00:00
|
|
|
SMTP_SUBJECT=$(_clean_email_header "$SMTP_SUBJECT")
|
2021-02-16 20:49:27 +00:00
|
|
|
_debug SMTP_SUBJECT "$SMTP_SUBJECT"
|
|
|
|
_debug SMTP_CONTENT "$SMTP_CONTENT"
|
|
|
|
|
2021-01-11 19:46:26 +00:00
|
|
|
# Send the message:
|
2021-02-16 20:49:27 +00:00
|
|
|
case "$(basename "$SMTP_BIN")" in
|
2021-02-14 23:47:51 +00:00
|
|
|
curl) _smtp_send=_smtp_send_curl ;;
|
|
|
|
py*) _smtp_send=_smtp_send_python ;;
|
|
|
|
*)
|
2021-02-16 20:49:27 +00:00
|
|
|
_err "Can't figure out how to invoke '$SMTP_BIN'."
|
2021-02-15 03:56:23 +00:00
|
|
|
_err "Check your SMTP_BIN setting."
|
2021-02-14 23:47:51 +00:00
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if ! smtp_output="$($_smtp_send)"; then
|
2021-02-16 20:49:27 +00:00
|
|
|
_err "Error sending message with $SMTP_BIN."
|
2021-02-15 03:56:23 +00:00
|
|
|
if [ -n "$smtp_output" ]; then
|
|
|
|
_err "$smtp_output"
|
|
|
|
fi
|
2021-01-11 19:46:26 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2020-12-30 00:28:38 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:02:09 +00:00
|
|
|
# Strip CR and NL from text to prevent MIME header injection
|
|
|
|
# text
|
|
|
|
_clean_email_header() {
|
|
|
|
printf "%s" "$(echo "$1" | tr -d "\r\n")"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Simple check for display name in an email address (< > or ")
|
|
|
|
# email
|
|
|
|
_email_has_display_name() {
|
|
|
|
_email="$1"
|
|
|
|
expr "$_email" : '^.*[<>"]' >/dev/null
|
|
|
|
}
|
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
##
|
|
|
|
## curl smtp sending
|
|
|
|
##
|
|
|
|
|
|
|
|
# Send the message via curl using SMTP_* variables
|
2021-02-14 23:47:51 +00:00
|
|
|
_smtp_send_curl() {
|
2021-02-15 03:56:23 +00:00
|
|
|
# Build curl args in $@
|
2021-02-16 20:49:27 +00:00
|
|
|
case "$SMTP_SECURE" in
|
2021-02-15 03:56:23 +00:00
|
|
|
none)
|
2021-02-16 20:49:27 +00:00
|
|
|
set -- --url "smtp://${SMTP_HOST}:${SMTP_PORT}"
|
2021-02-15 03:56:23 +00:00
|
|
|
;;
|
|
|
|
ssl)
|
2021-02-16 20:49:27 +00:00
|
|
|
set -- --url "smtps://${SMTP_HOST}:${SMTP_PORT}"
|
2021-02-15 03:56:23 +00:00
|
|
|
;;
|
|
|
|
tls)
|
2021-02-16 20:49:27 +00:00
|
|
|
set -- --url "smtp://${SMTP_HOST}:${SMTP_PORT}" --ssl-reqd
|
2021-02-15 03:56:23 +00:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
# This will only occur if someone adds a new SMTP_SECURE option above
|
|
|
|
# without updating this code for it.
|
2021-02-16 20:49:27 +00:00
|
|
|
_err "Unhandled SMTP_SECURE='$SMTP_SECURE' in _smtp_send_curl"
|
2021-02-15 03:56:23 +00:00
|
|
|
_err "Please re-run with --debug and report a bug."
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
set -- "$@" \
|
|
|
|
--upload-file - \
|
2021-02-16 20:49:27 +00:00
|
|
|
--mail-from "$SMTP_FROM" \
|
|
|
|
--max-time "$SMTP_TIMEOUT"
|
2021-02-15 03:56:23 +00:00
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
# Burst comma-separated $SMTP_TO into individual --mail-rcpt args.
|
|
|
|
_to="${SMTP_TO},"
|
2021-02-15 03:56:23 +00:00
|
|
|
while [ -n "$_to" ]; do
|
|
|
|
_rcpt="${_to%%,*}"
|
|
|
|
_to="${_to#*,}"
|
|
|
|
set -- "$@" --mail-rcpt "$_rcpt"
|
|
|
|
done
|
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
_smtp_login="${SMTP_USERNAME}:${SMTP_PASSWORD}"
|
2021-02-15 03:56:23 +00:00
|
|
|
if [ "$_smtp_login" != ":" ]; then
|
|
|
|
set -- "$@" --user "$_smtp_login"
|
|
|
|
fi
|
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
if [ "$SMTP_SHOW_TRANSCRIPT" = "True" ]; then
|
2021-02-15 03:56:23 +00:00
|
|
|
set -- "$@" --verbose
|
|
|
|
else
|
|
|
|
set -- "$@" --silent --show-error
|
|
|
|
fi
|
|
|
|
|
|
|
|
raw_message="$(_smtp_raw_message)"
|
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
_debug2 "curl command:" "$SMTP_BIN" "$*"
|
2021-02-15 03:56:23 +00:00
|
|
|
_debug2 "raw_message:\n$raw_message"
|
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
echo "$raw_message" | "$SMTP_BIN" "$@"
|
2021-02-15 03:56:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 22:02:09 +00:00
|
|
|
# Output an RFC-822 / RFC-5322 email message using SMTP_* variables.
|
|
|
|
# (This assumes variables have already been cleaned for use in email headers.)
|
2021-02-15 03:56:23 +00:00
|
|
|
_smtp_raw_message() {
|
2021-02-16 20:49:27 +00:00
|
|
|
echo "From: $SMTP_FROM"
|
|
|
|
echo "To: $SMTP_TO"
|
|
|
|
echo "Subject: $(_mime_encoded_word "$SMTP_SUBJECT")"
|
2021-02-16 21:13:26 +00:00
|
|
|
echo "Date: $(_rfc2822_date)"
|
2021-02-15 03:56:23 +00:00
|
|
|
echo "Content-Type: text/plain; charset=utf-8"
|
2021-02-16 20:49:27 +00:00
|
|
|
echo "X-Mailer: $SMTP_X_MAILER"
|
2021-02-15 03:56:23 +00:00
|
|
|
echo
|
2021-02-16 20:49:27 +00:00
|
|
|
echo "$SMTP_CONTENT"
|
2021-02-15 03:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Convert text to RFC-2047 MIME "encoded word" format if it contains non-ASCII chars
|
|
|
|
# text
|
|
|
|
_mime_encoded_word() {
|
|
|
|
_text="$1"
|
|
|
|
# (regex character ranges like [a-z] can be locale-dependent; enumerate ASCII chars to avoid that)
|
|
|
|
_ascii='] $`"'"[!#%&'()*+,./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ~^_abcdefghijklmnopqrstuvwxyz{|}~-"
|
|
|
|
if expr "$_text" : "^.*[^$_ascii]" >/dev/null; then
|
|
|
|
# At least one non-ASCII char; convert entire thing to encoded word
|
|
|
|
printf "%s" "=?UTF-8?B?$(printf "%s" "$_text" | _base64)?="
|
|
|
|
else
|
|
|
|
# Just printable ASCII, no conversion needed
|
|
|
|
printf "%s" "$_text"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-02-16 21:13:26 +00:00
|
|
|
# Output current date in RFC-2822 Section 3.3 format as required in email headers
|
|
|
|
# (e.g., "Mon, 15 Feb 2021 14:22:01 -0800")
|
|
|
|
_rfc2822_date() {
|
|
|
|
# Notes:
|
|
|
|
# - this is deliberately not UTC, because it "SHOULD express local time" per spec
|
|
|
|
# - the spec requires weekday and month in the C locale (English), not localized
|
|
|
|
# - this date format specifier has been tested on Linux, Mac, Solaris and FreeBSD
|
|
|
|
_old_lc_time="$LC_TIME"
|
|
|
|
LC_TIME=C
|
|
|
|
date +'%a, %-d %b %Y %H:%M:%S %z'
|
|
|
|
LC_TIME="$_old_lc_time"
|
|
|
|
}
|
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
##
|
|
|
|
## Python smtp sending
|
|
|
|
##
|
|
|
|
|
|
|
|
# Send the message via Python using SMTP_* variables
|
2021-02-14 23:47:51 +00:00
|
|
|
_smtp_send_python() {
|
2021-02-16 20:49:27 +00:00
|
|
|
_debug "Python version" "$("$SMTP_BIN" --version 2>&1)"
|
2020-12-30 00:28:38 +00:00
|
|
|
|
|
|
|
# language=Python
|
2021-02-16 20:49:27 +00:00
|
|
|
"$SMTP_BIN" <<PYTHON
|
2020-12-30 00:28:38 +00:00
|
|
|
# This code is meant to work with either Python 2.7.x or Python 3.4+.
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
from email.message import EmailMessage
|
2021-02-17 17:57:44 +00:00
|
|
|
from email.policy import default as email_policy_default
|
2020-12-30 00:28:38 +00:00
|
|
|
except ImportError:
|
2021-02-17 17:57:44 +00:00
|
|
|
# Python 2 (or < 3.3)
|
|
|
|
from email.mime.text import MIMEText as EmailMessage
|
|
|
|
email_policy_default = None
|
2021-02-17 17:46:13 +00:00
|
|
|
from email.utils import formatdate as rfc2822_date
|
2020-12-30 00:28:38 +00:00
|
|
|
from smtplib import SMTP, SMTP_SSL, SMTPException
|
|
|
|
from socket import error as SocketError
|
|
|
|
except ImportError as err:
|
|
|
|
print("A required Python standard package is missing. This system may have"
|
|
|
|
" a reduced version of Python unsuitable for sending mail: %s" % err)
|
|
|
|
exit(1)
|
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
show_transcript = """$SMTP_SHOW_TRANSCRIPT""" == "True"
|
2020-12-30 00:28:38 +00:00
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
smtp_host = """$SMTP_HOST"""
|
|
|
|
smtp_port = int("""$SMTP_PORT""")
|
|
|
|
smtp_secure = """$SMTP_SECURE"""
|
|
|
|
username = """$SMTP_USERNAME"""
|
|
|
|
password = """$SMTP_PASSWORD"""
|
|
|
|
timeout=int("""$SMTP_TIMEOUT""") # seconds
|
|
|
|
x_mailer="""$SMTP_X_MAILER"""
|
2020-12-30 00:28:38 +00:00
|
|
|
|
2021-02-16 20:49:27 +00:00
|
|
|
from_email="""$SMTP_FROM"""
|
|
|
|
to_emails="""$SMTP_TO""" # can be comma-separated
|
|
|
|
subject="""$SMTP_SUBJECT"""
|
|
|
|
content="""$SMTP_CONTENT"""
|
2020-12-30 00:28:38 +00:00
|
|
|
|
|
|
|
try:
|
2021-02-17 17:57:44 +00:00
|
|
|
msg = EmailMessage(policy=email_policy_default)
|
2020-12-30 00:28:38 +00:00
|
|
|
msg.set_content(content)
|
|
|
|
except (AttributeError, TypeError):
|
|
|
|
# Python 2 MIMEText
|
|
|
|
msg = EmailMessage(content)
|
|
|
|
msg["Subject"] = subject
|
|
|
|
msg["From"] = from_email
|
|
|
|
msg["To"] = to_emails
|
2021-02-17 17:46:13 +00:00
|
|
|
msg["Date"] = rfc2822_date(localtime=True)
|
2021-02-15 20:23:48 +00:00
|
|
|
msg["X-Mailer"] = x_mailer
|
2020-12-30 00:28:38 +00:00
|
|
|
|
|
|
|
smtp = None
|
|
|
|
try:
|
|
|
|
if smtp_secure == "ssl":
|
|
|
|
smtp = SMTP_SSL(smtp_host, smtp_port, timeout=timeout)
|
|
|
|
else:
|
|
|
|
smtp = SMTP(smtp_host, smtp_port, timeout=timeout)
|
2021-02-14 23:47:51 +00:00
|
|
|
smtp.set_debuglevel(show_transcript)
|
2020-12-30 00:28:38 +00:00
|
|
|
if smtp_secure == "tls":
|
|
|
|
smtp.starttls()
|
|
|
|
if username or password:
|
|
|
|
smtp.login(username, password)
|
|
|
|
smtp.sendmail(msg["From"], msg["To"].split(","), msg.as_string())
|
|
|
|
|
|
|
|
except SMTPException as err:
|
|
|
|
# Output just the error (skip the Python stack trace) for SMTP errors
|
|
|
|
print("Error sending: %r" % err)
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
except SocketError as err:
|
|
|
|
print("Error connecting to %s:%d: %r" % (smtp_host, smtp_port, err))
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
if smtp is not None:
|
|
|
|
smtp.quit()
|
2021-02-16 20:49:27 +00:00
|
|
|
PYTHON
|
|
|
|
}
|
|
|
|
|
|
|
|
##
|
|
|
|
## Conf helpers
|
|
|
|
##
|
|
|
|
|
|
|
|
#_readaccountconf_mutable_default name default_value
|
|
|
|
# Given a name like MY_CONF:
|
|
|
|
# - if MY_CONF is set and non-empty, output $MY_CONF
|
|
|
|
# - if MY_CONF is set _empty_, output $default_value
|
|
|
|
# (lets user `export MY_CONF=` to clear previous saved value
|
|
|
|
# and return to default, without user having to know default)
|
2021-02-16 22:41:21 +00:00
|
|
|
# - otherwise if _readaccountconf_mutable MY_CONF is non-empty, return that
|
2021-02-16 20:49:27 +00:00
|
|
|
# (value of SAVED_MY_CONF from account.conf)
|
|
|
|
# - otherwise output $default_value
|
|
|
|
_readaccountconf_mutable_default() {
|
|
|
|
_name="$1"
|
|
|
|
_default_value="$2"
|
|
|
|
|
|
|
|
eval "_value=\"\$$_name\""
|
2021-02-16 22:41:21 +00:00
|
|
|
eval "_name_is_set=\"\${${_name}+true}\""
|
|
|
|
# ($_name_is_set is "true" if $$_name is set to anything, including empty)
|
|
|
|
if [ -z "${_value}" ] && [ "${_name_is_set:-}" != "true" ]; then
|
2021-02-16 20:49:27 +00:00
|
|
|
_value="$(_readaccountconf_mutable "$_name")"
|
|
|
|
fi
|
|
|
|
if [ -z "${_value}" ]; then
|
|
|
|
_value="$_default_value"
|
|
|
|
fi
|
|
|
|
printf "%s" "$_value"
|
|
|
|
}
|
|
|
|
|
|
|
|
#_saveaccountconf_mutable_default name value default_value base64encode
|
|
|
|
# Like _saveaccountconf_mutable, but if value is default_value
|
|
|
|
# then _clearaccountconf_mutable instead
|
|
|
|
_saveaccountconf_mutable_default() {
|
|
|
|
_name="$1"
|
|
|
|
_value="$2"
|
|
|
|
_default_value="$3"
|
|
|
|
_base64encode="$4"
|
|
|
|
|
|
|
|
if [ "$_value" != "$_default_value" ]; then
|
|
|
|
_saveaccountconf_mutable "$_name" "$_value" "$_base64encode"
|
|
|
|
else
|
|
|
|
_clearaccountconf_mutable "$_name"
|
|
|
|
fi
|
2019-04-29 14:13:54 +00:00
|
|
|
}
|