3

I'm using python 2.7 and trying to parse emails that are being forwarded as an attachment to an inbox. I can't seem to parse the headers/email of the attached message. I see it as 'message/rfc822' but when i try to parse it, it shows empty.

I pull the messages via a generator and it works fine, as below:

res, data = self.imap_client.fetch(msg_id, '(RFC822)')
msg = email.message_from_string(data[0][1])
yield msg

This msg is then pushed into another function where i pull the attachments (via walk())

attachments = []
for part in msg.walk():
    content_disposition = part.get('Content-Disposition', None)
    if content_disposition:
        attachments.append(Attachment(part, email_date))
return attachments

The Attachment class is as follows:

class Attachment(object):
    def __init__(self, attachment, email_date=None):
        self.content_type = attachment.get_content_type()
        self.attachment = attachment
        fname = None
        if self.content_type.startswith('message/'):
            self.data = attachment.as_string()
            fname = 'mail.eml'
        else:
            self.data = attachment.get_payload(decode=True)
        try:
            self.size = len(self.data)
        except TypeError:
            self.size = 0
        if self.attachment.get_filename():
            fname = self.attachment.get_filename().strip().replace(' ', '-')
        self.name = re.sub(r'(?u)[^-\w.]', '', fname)
        self.date = email_date

I have tried to pull the attachment using get_payload and passing it (as above) using just as_string(), but to no avail. When I try to parse the email attachment, it don't get any data:

if attachment.content_type == "message/rfc822":
    msg = email.message_from_string(attachment.data)
    print msg.get('Subject', None)
callingconvention
  • 183
  • 1
  • 1
  • 8

0 Answers0