use with ... for smtp

This commit is contained in:
Son 2021-12-29 17:09:24 +01:00
parent d80ecfb068
commit c14a7b4f7a
1 changed files with 25 additions and 19 deletions

View File

@ -1297,26 +1297,32 @@ def sl_sendmail(
return return
try: try:
# to avoid creating SMTP server every time if POSTFIX_SUBMISSION_TLS:
smtp = get_smtp_server() smtp_port = 587
else:
smtp_port = POSTFIX_PORT
# smtp.send_message has UnicodeEncodeError with SMTP(POSTFIX_SERVER, smtp_port) as smtp:
# encode message raw directly instead if POSTFIX_SUBMISSION_TLS:
LOG.d( smtp.starttls()
"Sendmail mail_from:%s, rcpt_to:%s, header_from:%s, header_to:%s, header_cc:%s",
from_addr, # smtp.send_message has UnicodeEncodeError
to_addr, # encode message raw directly instead
msg[headers.FROM], LOG.d(
msg[headers.TO], "Sendmail mail_from:%s, rcpt_to:%s, header_from:%s, header_to:%s, header_cc:%s",
msg[headers.CC], from_addr,
) to_addr,
smtp.sendmail( msg[headers.FROM],
from_addr, msg[headers.TO],
to_addr, msg[headers.CC],
to_bytes(msg), )
mail_options, smtp.sendmail(
rcpt_options, from_addr,
) to_addr,
to_bytes(msg),
mail_options,
rcpt_options,
)
except (SMTPServerDisconnected, SMTPRecipientsRefused) as e: except (SMTPServerDisconnected, SMTPRecipientsRefused) as e:
if retries > 0: if retries > 0:
LOG.w( LOG.w(