Search code examples
pythonemail-attachmentssmimeexchangelib

Python Exchangelib - get attachment from email with digital sign (smime.p7m)


I need to receive information about attachments in an email that is digitally signed.

I will load the email as follows:

email = self.acc.inbox.all().order_by('-datetime_received')[-1]

In the standard case after calling email.attachment I see information about attachments, but here I have one attachment 'smime.p7m', content_type='multipart/signed'. I see a PDF attachment in Outlook.

[FileAttachment(attachment_id=AttachmentId(id='AAMkADliYWJjZDMwLTQ3ZmItNDUyZC04NzUzLTI4NzdmNmIzZTVmMQBGAAAAAADNLU08xmcgS4nSs8MSU1J4BwBpxlbu+UWAR65DdgjipD7cAAAAAAEMAABpxlbu+UWAR65DdgjipD7cAAIR/w6kAAABEgAQAEEthNdS53xLsXJcT4nI0GQ=', root_id=None, root_changekey=None), name='smime.p7m', content_type='multipart/signed', content_id=None, content_location=None, size=500729, last_modified_time=EWSDateTime(2023, 2, 20, 8, 25, 8, tzinfo=EWSTimeZone(key='Europe/Budapest')), is_inline=False, is_contact_photo=False)]

Please, how do I decrypt it so that I can read the information about the attachments in the email (content-type, content, size, etc.)

Thanks very much for any help! :)


Solution

  • There's some information on encrypted emails in this issue: https://github.com/ecederstrand/exchangelib/issues/331#issuecomment-359811342

    Basically, you can use OpenSSL to decrypt the file, given that you have the private key. This SO answer has some information on decrypting p7m files using Python: How To Decrypt a .p7m File via Python Openssl