Commit b78bc3ff authored by Cecylia Bocovich's avatar Cecylia Bocovich 💬
Browse files

Ignore emails from Mail Delivery System

This ignores emails with the username "MAILER-DAEMON", a common email
for mail delivery systems. The goal of this is to avoid getting into a
loop with mail delivery system autoresponders after a bounced email.
parent 3670e782
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -84,6 +84,11 @@ class EmailParser(object):
                "Email address normalized and validated.",
                system="email parser"
            )

            # Add a check for auto-generated mail-daemon emails
            if "mailer-daemon@" in norm_addr.lower():
                raise AddressError("Received mail from Mail Delivery System {}"
                        .format(msg['From']))
            return True

        else:
@@ -206,7 +211,8 @@ class EmailParser(object):
        try:
            self.validate(norm_addr, msg)
        except AddressError as e:
            log.message("Address error: {}".format(e.args))
            log.msg("Address error: {}".format(e.args))
            return {}

        hid = hashlib.sha256(norm_addr.encode('utf-8'))
        log.msg(
+7 −0
Original line number Diff line number Diff line
@@ -289,6 +289,13 @@ class EmailServiceTests(unittest.TestCase):
                ">\n")
        self.assertEqual(request["command"], "help")

    def test_bounce(self):
        ep = conftests.EmailParser(self.settings, "gettor@torproject.org")
        request = ep.parse("From: MAILER-DAEMON@mx1.riseup.net\n"
                "Subject: Undelivered Mail Returned to Sender\r\n"
                "To: gettor@torproject.org\n osx en\n")

        self.assertEqual(request, {})

if __name__ == "__main__":
    unittest.main()