Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting attachment.content fails for files in attached message > "FileAttachment must have an account" #210

Closed
jorgekl opened this issue Aug 17, 2017 · 2 comments

Comments

@jorgekl
Copy link

jorgekl commented Aug 17, 2017

I'm trying to get all enclosed files (doc, pdf, etc.) from an email. In order to reach those files when inside an enclosed message, I'm using a recursive function:

...
for MailItem in FilteredMail:
   ReadAttachments(MailItem)

def ReadAttachments(MailItem)
    for Attachment in MailItem.attachments
        if hasattr(Attachment, 'item'):    #attachment is a message
            ReadAttachments(Atachment.item)
        else:     #attachment is a file (e.g. PDF)
            print Attachment.name + ':' + Attachment.size
            <do whatever with Attachment.content>

This works OK for regular emails, but fails for files inside other messages (Email1 > contains Email 2 > contains file.pdf). It's curious that in these cases Attachment.name and Attachment.size are displayed properly, but any action with Attachment.content results in a "FileAttachment must have an account" error.

Thanks in advance!!

@ecederstrand
Copy link
Owner

That's a bug. Try adding Attachment.parent_item = MailItem as the first line in your function until this is fixed.

@jorgekl
Copy link
Author

jorgekl commented Aug 17, 2017

Worked like a charm. Thanks a lot!! The resulting code below for reference.

...
for MailItem in FilteredMail:
   ReadAttachments(MailItem, MailItem)

def ReadAttachments(MailItem, RootParent)
    for Attachment in MailItem.attachments
        Attachment.parent_item = RootParent
        if hasattr(Attachment, 'item'):    #attachment is a message
            ReadAttachments(Atachment.item, RootParent)
        else:     #attachment is a file (e.g. PDF)
            print Attachment.name + ':' + Attachment.size
            <do whatever with Attachment.content>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants