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 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'service/pixelated/adapter/model/mail.py') 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()}, -- cgit v1.2.3