admin管理员组

文章数量:1336289

Normally a file is attached to an email I save the attachment to feed to a program that picks it up.

Now sometimes the attachment is an email with the attachment. I do not know how to fetch the attachment of the attached message.

This is my code:

from O365 import Account, Message
import O365
import uuid


class Mail:
    def __init__(self):
        self.credentials = ('xxx', 'xxx')
        self.account = Account(self.credentials, auth_flow_type='credentials', tenant_id='xxx')   
        if self.account.authenticate():
            print('Authenticated!')


        self.mailbox = self.account.mailbox(resource='[email protected]')
        #self.root_folder = my_drive.get_root_folder()
        self.inbox          = self.mailbox.get_folder(folder_name='Afgehandeld') 
        self.afgehandeld    = self.mailbox.get_folder(folder_name='Afgehandeld2') 
 
    def GetAttachmentFromMessage( self, message ):
        print('getting attachment from message')
        item = None

        if message.has_attachments:
            message.attachments.download_attachments()
            
            attachments =  message.attachments

            if 'Original Message Attached' in message.body:
                print('LET OP, message is bijlage!')
                


                for item in attachments:
                    filename = str(uuid.uuid4())
                    api = item.to_api_data()
                    print(api)

                    # this doesnt work
                    item.save('mail/',filename)
                    
                    # This doesnt work as its an attachment and not a message
                    #self.GetAttachmentFromMessage(item)

            for item in attachments:
                #filename = str(uuid.uuid4())
                #filename = item.attachment_name()
                item.save('bvg/',)        

        return item

    def ListMessagesFromFolder(self, folder):
        messageList = self.inbox.get_messages()
        
        return messageList

    def MoveMessage(self, message, folder):
            message.move(folder)


    

if __name__ == "__main__":
    mail = Mail()

    messages    = mail.ListMessagesFromFolder('inbox')

    for message in messages:
        #print(message)
        data = mail.GetAttachmentFromMessage(message)
    

Problem:

If the attachment is a message and not a file this is a problem. Not only because then the attachment isnt saved. But also that i have no way to iterate through attachments to get to the attachment of the attachment.

            if 'Original Message Attached' in message.body:
                print('LET OP, message is bijlage!')

This does work to find if the attachment is a message (it seems its the spamfilter) so when the text is there apparently the message is attached and forwarded.

tried:

            item.attachments.download_attachments()
            
            attachments =  item.attachments

This doesnt work as item is of type BaseAttachment which doesnt have these methods.

Also tried:

mail.GetAttachment(attachment.id)

Obviously this doesnt work as you could get the message by id (but not in this way) but the attachment object has no member that gives the ID either.

So I'm stuck, I cant get the spamfilter to not forward as attachment allthough im still trying. On the other hand if the sending party does this it would be great if the program still works.

I'd rather change the program so that it can iterate through attachements even if the attachment is a message that also has an attachment.

Question:

Is there anyway with the O365 package to get the attachment from a message if the message is attached to a message? Or do I need to move to something else?

本文标签: pythonHow to get attachment from attached messageStack Overflow