From 0cec3fee1267f46d651380f8c38cc616c3ac7268 Mon Sep 17 00:00:00 2001 From: Nicolas Iooss Date: Sat, 12 Oct 2013 12:46:52 +0200 Subject: [PATCH] NL bounces: try findAddressInPlainBounce if findAddressInBounce fails because the bounce does not include a message/delivery-status part Signed-off-by: Nicolas Iooss --- bin/newsletter.bounces.processor.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/bin/newsletter.bounces.processor.py b/bin/newsletter.bounces.processor.py index 95110b9..a9f108a 100755 --- a/bin/newsletter.bounces.processor.py +++ b/bin/newsletter.bounces.processor.py @@ -143,6 +143,15 @@ def findAddressInBounce(bounce): print('! Not a valid bounce (expected at least 2 parts, found %d).' % num_payloads) return None status = bounce.get_payload(1) + + # If the second part is of type "message/rfc822" it is the undelivered message. + # Let's try to understand the text part + if status.get_content_type() == 'message/rfc822': + text_bounce = bounce.get_payload(0) + if text_bounce.get_content_type() == 'text/plain': + return findAddressInPlainBounce(text_bounce, bounce) + # If it's not a text message, let's continue to the next error message + if status.get_content_type() != 'message/delivery-status': print('! Not a valid bounce (expected message/delivery-status, found %s).' % status.get_content_type()) return None @@ -198,16 +207,18 @@ def findAddressInBounce(bounce): return None -def findAddressInPlainBounce(bounce): +def findAddressInPlainBounce(bounce, real_bounce=None): """Finds the faulty email address in a non-RFC-1894 bounced email """ - if 'MAILER-DAEMON@' not in bounce['From'].upper(): + # real_bounce is the full email and bounce only the text/plain part, if email have several MIME parts + real_bounce = real_bounce or bounce + if 'MAILER-DAEMON@' not in real_bounce['From'].upper(): print('! Not a valid plain bounce (expected from MAILER-DAEMON, found %s).' % bounce['From']) return None if bounce.get_content_type() != 'text/plain': print('! Not a valid plain bounce (expected text/plain, found %s).' % bounce.get_content_type()) return None - subject = findSubject(bounce).lower() + subject = findSubject(real_bounce).lower() known_subjects = [ "delivery status notification (failure)", "failure notice", -- 2.1.4