[FIX] mail: do not shadow SMTP errors with QUIT

When an exception is raised during the course of a normal SMTP session,
smtplib's `sendmail()` method will close the channel automatically,
so any further attempt at sending SMTP commands will fail with an
SMTPServerDisconnected error, even just calling `smtp.quit()`.
This will shadow the original error, and make debugging harder.

Fixes #1493
This commit is contained in:
Olivier Dony
2018-06-06 10:26:22 +02:00
parent 6ef27bb913
commit c72eec46f9
+7 -9
View File
@@ -447,15 +447,13 @@ class IrMailServer(models.Model):
try:
message_id = message['Message-Id']
smtp = smtp_session
try:
smtp = smtp or self.connect(
smtp_server, smtp_port, smtp_user, smtp_password,
smtp_encryption, smtp_debug, mail_server_id=mail_server_id)
smtp.sendmail(smtp_from, smtp_to_list, message.as_string())
finally:
# do not quit() a pre-established smtp_session
if smtp is not None and not smtp_session:
smtp.quit()
smtp = smtp or self.connect(
smtp_server, smtp_port, smtp_user, smtp_password,
smtp_encryption, smtp_debug, mail_server_id=mail_server_id)
smtp.sendmail(smtp_from, smtp_to_list, message.as_string())
# do not quit() a pre-established smtp_session
if not smtp_session:
smtp.quit()
except Exception as e:
params = (ustr(smtp_server), e.__class__.__name__, ustr(e))
msg = _("Mail delivery failed via SMTP server '%s'.\n%s: %s") % params