python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

mail_folder.get_message(object_id=... returns the original mail and not the attachment

Open pdelporte opened this issue 4 years ago • 9 comments

If you have an attachment that is an attached mail (attachment_type=='item'), and if you try to get the message with mail_folter.get_message(object_id=attachemnent.attachment_id) it returns the container mail and not the attached mail

if msg.has_attachments:
    for att in msg.attachments:
        if not att.is_inline and att.attachment_type == 'item':
            print(att)
            att_dl = folder.get_message(object_id=att.attachment_id) # Not getting the attached mail

pdelporte avatar Mar 24 '21 07:03 pdelporte

You can do it but I must admit it's a little obscure.

if msg.has_attachments:
    for att in msg.attachments:
        if not att.is_inline and att.attachment_type == 'item':
            msg.attachments.save_as_eml(att, to_path='my path')

alejcas avatar Mar 24 '21 08:03 alejcas

Thanks for the answer, but it is not exactly the behaviour I am looking for. I don't want to save the mail, I want the read it and analyse it.

pdelporte avatar Mar 24 '21 10:03 pdelporte

The Ms Graph Api only returns data from attached messages as an "eml" file with it's structure. It's quite difficult to re-construct an O365 Message object from a 'eml' file format.

alejcas avatar Mar 24 '21 10:03 alejcas

Yes, indeed, but what I was looking was the ability to call something like : https://graph.microsoft.com/v1.0/users/{user_id}/messages/{messages_id}/attachments/{attachement_id}/?$expand=microsoft.graph.itemattachment/item

pdelporte avatar Mar 25 '21 07:03 pdelporte

Are you sure this works on message attachments?

alejcas avatar Mar 30 '21 20:03 alejcas

did you guys find a solution for this request? I am trying to find out wether 0365 lib supports retrieving attachment of type message

my fallback approach would be to call the corresponding graph api endpoint directly.. but as its messy to parse the result i would rather use 0365 lib

codingmercy1993 avatar Nov 14 '22 10:11 codingmercy1993

You can retrieve it as shown in the example code I posted. But for the moment the only thing supported is to save the content to disk. You can (if you know how) read that into memory and work with that data

alejcas avatar Nov 14 '22 15:11 alejcas

hello, did you find a solution to this ? with EWS it was easy to process such items, but migrating to MS Graph I'm stuck with this

yeyeric avatar Jan 08 '23 03:01 yeyeric

for what it's worth, here is my quick workaround which creates a message I can process:

# email is my original email which has a single attachment which is an item with another email
url = email.attachments.build_url(email.attachments._endpoints.get('get_mime').format(id=email.object_id, ida=attachments[0].attachment_id))
# as pdelporte suggestion and ms graph doc
response = email.con.get(f'{url.removesuffix("$value")}?$expand=microsoft.graph.itemattachment/item').json()
# 
attachment = account.mailbox(resource=my_resource).message_constructor(parent=account.mailbox(resource=my_resource), **{account.mailbox(resource=my_resource)._cloud_data_key: response.get('item', [])})

Here attachment is a Message which can be processed as a standard Message sent with get_messages()

yeyeric avatar Jan 08 '23 03:01 yeyeric