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,8 +1297,14 @@ def sl_sendmail(
return
try:
# to avoid creating SMTP server every time
smtp = get_smtp_server()
if POSTFIX_SUBMISSION_TLS:
smtp_port = 587
else:
smtp_port = POSTFIX_PORT
with SMTP(POSTFIX_SERVER, smtp_port) as smtp:
if POSTFIX_SUBMISSION_TLS:
smtp.starttls()
# smtp.send_message has UnicodeEncodeError
# encode message raw directly instead