InsideDarkWeb.com

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

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)

Stack Overflow Asked by RajSoundar on November 18, 2021

2 Answers

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!

Related Questions

How to revert back to python 2.7?

1  Asked on February 14, 2021 by nikhil-shrivastava

         

Using same name for attribute and getter

1  Asked on February 13, 2021 by janpeterka

     

What is the purpose to use className beside functionName using colon(:)

1  Asked on February 13, 2021 by mahedi-hasan-durjoy

 

Python: access a variable in a module from another file

2  Asked on February 13, 2021 by yeroduk

 

How to get elements of a sublist in Java

5  Asked on February 13, 2021 by alvira

     

`SyntaxError` in if-else one-liner

2  Asked on February 13, 2021 by evgeniy-golovin

         

NiFi Consume kafka sasl_plaintext issue

1  Asked on February 13, 2021 by varun_rathinam

       

Clock divider in vhdl from 100MHz to 1Hz code

2  Asked on February 13, 2021 by mariaaaa

     

Inconsistent Paging in ASP.NET CORE

0  Asked on February 13, 2021 by addsw

       

Nuxt.js: can’t generate routes

3  Asked on February 12, 2021 by drake035

     

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.