From c5f29f136f293a51d2fd42fcfd8fc18a39f09cfa Mon Sep 17 00:00:00 2001 From: Patrick Maia Date: Fri, 13 Feb 2015 19:00:41 -0300 Subject: Issue #174 - extracts bounced address from bounce message --- service/pixelated/adapter/model/mail.py | 19 +++++++++++++++++-- service/test/unit/adapter/test_mail.py | 1 + 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'service') diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py index b3d2e3a5..a8d752c5 100644 --- a/service/pixelated/adapter/model/mail.py +++ b/service/pixelated/adapter/model/mail.py @@ -382,8 +382,23 @@ class PixelatedMail(Mail): @property def bounced(self): - content_type = self.hdoc.content["headers"].get("Content-Type", '') - return re.compile('delivery-status').search(content_type) + content_type = self.hdoc.content["headers"].get("Content-Type", '') + if re.compile('delivery-status').search(content_type): + return self._extract_bounced_address(self.hdoc.content) + + return False + + def _extract_bounced_address(self, part): + part_header = dict(part.get('headers', {})) + if 'Final-Recipient' in part_header: + return part_header['Final-Recipient'].split(';')[1].strip() + elif 'part_map' in part: + for subpart in part['part_map'].values(): + result = self._extract_bounced_address(subpart) + if result: + return result + else: + continue def as_dict(self): dict_mail = {'header': {k.lower(): v for k, v in self.headers.items()}, diff --git a/service/test/unit/adapter/test_mail.py b/service/test/unit/adapter/test_mail.py index 72bb6d6d..a5d4fe24 100644 --- a/service/test/unit/adapter/test_mail.py +++ b/service/test/unit/adapter/test_mail.py @@ -224,6 +224,7 @@ class TestPixelatedMail(unittest.TestCase): not_bounced_mail = PixelatedMail.from_soledad(*not_bounced_leap_mail, soledad_querier=self.querier) self.assertTrue(bounced_mail.bounced) + self.assertEquals('inexistentaccount@domain.com', bounced_mail.bounced) self.assertFalse(not_bounced_mail.bounced) def _create_bdoc(self, raw): -- cgit v1.2.3