While trying to handle couple attachments of types ItemAttachment and FileAttachment from an inbound email, I notice that the ItemAttachment (representing an email attachment "HELLO WORLD.eml") strips the extension .eml from the name. So I lose that info downstream in my flow.
The other types of attachments of type FileAttachment are all fine and keep their extensions. Not sure whether I am missing something or is a defect in the way the ItemAttachment is initialized. Thoughts?
Note 1: These attachments are right off the bat like: attachments = message_item.attachments
Note 2: exchangelib==3.2.0
** ATTACHMENT 1 NAME: HELLO WORLD, <== Supposed to have .eml extension TYPE: <class 'exchangelib.attachments.ItemAttachment'> content_type='message/rfc822', content_id='[email protected]', size=31367, last_modified_time=EWSDateTime(2020, 7, 20, 22, 25, 2, tzinfo=<UTC>), is_inline=False ** ATTACHMENT 2 NAME: Daily Sync-up call.ics TYPE: <class 'exchangelib.attachments.FileAttachment'>: content_type='text/calendar', content_id='[email protected]', size=76875, last_modified_time=EWSDateTime(2020, 7, 20, 22, 25, 2, tzinfo=<UTC>), is_inline=False, is_contact_photo=False)
(some content redacted)
Taking into account the accepted answer for my question, to counter the loss of .eml extension I was facing with ItemAttachment, I have adopted an explicit renaming scheme as follows:
if isinstance(a, ItemAttachment): attach_name = a.name regex_pat = re.compile(r'.*.eml$') # regex for explicit .eml extension if not regex_pat.match(a.name) and a.content_type == "message/rfc822": attach_name += ".eml" attachment_file = ContentFile(a.item.mime_content, name=attach_name)
An obvious gotcha is my assumption that "message/rfc822" type file has .eml as the extension and not others. But this works for my purposes in my environment as a workaround to reinstate the missing .eml extension. Leaving this approach here for compare/contrast in case anyone comes across this issue.
Answered by RajSoundar on November 18, 2021
Item attachments in EWS are different in that they are not actually files, but references to other items in the Exchange database. So the .ics extension you probably see in e.g. Outlook is a .eml file that Outlook creates from the referenced item and offers for download. But EWS does not know about it.
ItemAttachment.item is an ordinary
Item, and you can use it as such. If you need the attachment, you can create a .eml file from the information contained in the item attachment, but you'll have to do that yourself or use a library to help you out.
Answered by Erik Cederstrand on November 18, 2021
1 Asked on February 14, 2021 by andrew-vo
15 Asked on February 14, 2021 by david-essien
1 Asked on February 14, 2021 by nikhil-shrivastava
0 Asked on February 13, 2021 by elighne
1 Asked on February 13, 2021 by usef-juan
1 Asked on February 13, 2021 by janpeterka
1 Asked on February 13, 2021 by mahedi-hasan-durjoy
1 Asked on February 13, 2021 by bmalbusca
2 Asked on February 13, 2021 by yeroduk
0 Asked on February 13, 2021 by alexw-h-b
5 Asked on February 13, 2021 by alvira
2 Asked on February 13, 2021 by evgeniy-golovin
2 Asked on February 13, 2021 by lv98
1 Asked on February 13, 2021 by varun_rathinam
4 Asked on February 13, 2021
2 Asked on February 13, 2021
2 Asked on February 13, 2021 by mariaaaa
0 Asked on February 13, 2021 by addsw
2 Asked on February 12, 2021 by brokhus
Get help from others!