TransWikia.com

Exchangelib: ItemAttachment appears to strip file extension from the attachment name

Stack Overflow Asked by RajSoundar on November 18, 2021

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)

2 Answers

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.

In exchangelib, 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

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP