From 46c471fb6d889ba337216c299df0c14577ee5682 Mon Sep 17 00:00:00 2001 From: Ahmon Dancy Date: Wed, 9 Oct 2019 14:24:47 -0700 Subject: [PATCH 1/3] Avoid cascading error on SMTPNotSupportedError If delivery failed due to a SMTPNotSupportedError exception, the exception handler in _deliver would try to invoke the decode() method on the class object, resulting in a cascading error. This commit fixes that problem. --- mailproxy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mailproxy.py b/mailproxy.py index 7dbed8d..8ebcb89 100644 --- a/mailproxy.py +++ b/mailproxy.py @@ -61,7 +61,7 @@ def _deliver(self, envelope): # All recipients were refused. If the exception had an associated # error code, use it. Otherwise, fake it with a SMTP 554 status code. errcode = getattr(e, 'smtp_code', 554) - errmsg = getattr(e, 'smtp_error', e.__class__) + errmsg = getattr(e, 'smtp_error', e.__class__.__name__.encode()) raise smtplib.SMTPResponseException(errcode, errmsg.decode()) From dc07ec3ac3e6a1132fc5ce9a5894550e98174916 Mon Sep 17 00:00:00 2001 From: Ahmon Dancy Date: Wed, 26 Jan 2022 09:21:42 -0800 Subject: [PATCH 2/3] Workaround for https://bugs.python.org/issue36094 Using mailproxy with Python 3.7+ results in the following error when s.starttls() is called: ValueError: server_hostname cannot be an empty string or start with a leading dot. The workaround for this problem is to pass the target host and port to the smtplib.SMTP() or smtplib.SMTP_SSL() constructors. --- mailproxy.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mailproxy.py b/mailproxy.py index 8ebcb89..ca9d6c9 100644 --- a/mailproxy.py +++ b/mailproxy.py @@ -39,10 +39,9 @@ def _deliver(self, envelope): refused = {} try: if self._use_ssl: - s = smtplib.SMTP_SSL() + s = smtplib.SMTP_SSL(self._host, self._port) else: - s = smtplib.SMTP() - s.connect(self._host, self._port) + s = smtplib.SMTP(self._host, self._port) if self._starttls: s.starttls() s.ehlo() From 9ff1de5239acb89c4e447db8b5cb9069ed6b117a Mon Sep 17 00:00:00 2001 From: Ahmon Dancy Date: Mon, 6 May 2024 11:52:28 -0700 Subject: [PATCH 3/3] Ignore SMTPServerDisconnected during QUIT Avoid a cascading error by catching and ignoring smtplib.SMTPServerDisconnected error when processing the 'finally' clause of MailProxyHandler._deliver(). --- mailproxy.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mailproxy.py b/mailproxy.py index ca9d6c9..319daeb 100644 --- a/mailproxy.py +++ b/mailproxy.py @@ -54,7 +54,10 @@ def _deliver(self, envelope): envelope.original_content ) finally: - s.quit() + try: + s.quit() + except smtplib.SMTPServerDisconnected: + pass except (OSError, smtplib.SMTPException) as e: logging.exception('got %s', e.__class__) # All recipients were refused. If the exception had an associated