0

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='742A502EB7681B4F8D08B03020716918@namprd10.prod.outlook.com',
   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='AF02FF7A060C5F4BA45628DE091DF5CD@namprd10.prod.outlook.com', 
   size=76875, 
   last_modified_time=EWSDateTime(2020, 7, 20, 22, 25, 2, tzinfo=<UTC>), 
   is_inline=False, 
   is_contact_photo=False)

(some content redacted)

RajSoundar
  • 341
  • 1
  • 2
  • 8

2 Answers2

1

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.

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
  • Yes, .ics file becomes FileAttachment and is easy to download. The .eml file coming as the ItemAttachment is also not a problem.But the latter simply loses the extension which came with the file originally. Kind of alters the state of the incoming file (aka its extension .eml) for no reason. Like you suggested, I decided to handle that myself by getting the content and renaming the downloaded file to include the .eml extension. I will try posting that approach as an additional answer as well in case anyone faces the same. – RajSoundar Jul 21 '20 at 22:26
1

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.

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
RajSoundar
  • 341
  • 1
  • 2
  • 8