commit 3a878213ba18178005238b0ded179fa84c5e1129 Author: Cecylia Bocovich cohosh@torproject.org Date: Mon Apr 27 14:36:58 2020 -0400
Check SMTP error code and remove request from db
This adds an extra check on SMTP errors to see if it's due to a bad recipient address and then removes the request from the database. This prevents one bad request from hanging up the processing of other requests (see https://bugs.torproject.org/34027). --- gettor/services/email/sendmail.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)
diff --git a/gettor/services/email/sendmail.py b/gettor/services/email/sendmail.py index 4d908b3..c4fdc95 100644 --- a/gettor/services/email/sendmail.py +++ b/gettor/services/email/sendmail.py @@ -18,19 +18,13 @@ import configparser from email.mime.text import MIMEText
from twisted.internet import defer -from twisted.mail.smtp import sendmail +from twisted.mail import smtp
from ...utils.db import SQLite3 as DB from ...utils.commons import log from ...utils import strings
-class SMTPError(Exception): - """ - Error if we can't send emails. - """ - pass - from email.mime.text import MIMEText class Sendmail(object): """ @@ -70,7 +64,7 @@ class Sendmail(object): Errback if we don't/can't send the message. """ log.debug("Could not send email.") - raise SMTPError("{}".format(error)) + raise error
def sendmail(self, email_addr, subject, body): """ @@ -93,7 +87,7 @@ class Sendmail(object):
log.debug("Calling asynchronous sendmail.")
- return sendmail( + return smtp.sendmail( self.settings.get("sendmail_host"), self.settings.get("sendmail_addr"), email_addr, message, requireTransportSecurity=True ).addCallback(self.sendmail_callback).addErrback(self.sendmail_errback) @@ -217,7 +211,14 @@ class Sendmail(object): id=id, service="email", date=date )
- except SMTPError as e: + except smtp.SMTPClientError as e: + if e.code == 501: # Bad recipient address syntax + yield self.conn.remove_request( + id=id, service="email", date=date + ) + log.info("Error sending email: {}.".format(e)) + + except Exception as e: log.info("Error sending email: {}.".format(e))
elif link_requests: @@ -270,7 +271,14 @@ class Sendmail(object): id=id, service="email", date=date )
- except SMTPError as e: + except smtp.SMTPClientError as e: + if e.code == 501: # Bad recipient address syntax + yield self.conn.remove_request( + id=id, service="email", date=date + ) + log.info("Error sending email: {}.".format(e)) + + except Exception as e: log.info("Error sending email: {}.".format(e)) else: log.debug("No pending email requests. Keep waiting.")