diff options
author | Patrick Maia <pmaia@thoughtworks.com> | 2015-02-13 19:00:41 -0300 |
---|---|---|
committer | Patrick Maia <pmaia@thoughtworks.com> | 2015-02-20 11:15:11 -0300 |
commit | c5f29f136f293a51d2fd42fcfd8fc18a39f09cfa (patch) | |
tree | 5ba6fdc2a678533bf001088f5c56c2237603711f /service | |
parent | bb723e2cd21339f82c63ede3151d33022c0f029f (diff) |
Issue #174 - extracts bounced address from bounce message
Diffstat (limited to 'service')
-rw-r--r-- | service/pixelated/adapter/model/mail.py | 19 | ||||
-rw-r--r-- | service/test/unit/adapter/test_mail.py | 1 |
2 files changed, 18 insertions, 2 deletions
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): |