How to parse winmail.dat using python
I pull emails with attachments from the server and put them in folders based on certain criteria. This is not a problem for emails that were sent using plain text encoding, but as we all know if they were sent using rich text, attachments are converted to winmail.dat format.
I tried using a module called tnefparse but I had no luck as I couldn't find any documentation or examples on the internet.
Is it possible to provide some examples on how to read and convert a winmail.dat attachment using tnefparse or any other Python module?
source to share
It's pretty easy to use tnefparse from the command line
First of all install it using pip
pip install tnefparse
extract application from winmail.dat only run
tnefparse -a winmail.dat
If you want to integrate this library into your Python code, just take the tnefparse command line implementation , which is very easy to understand. Anyway, here's some sample code that fetches all attachments from winmail.dat to the current working directory:
import sys
from tnefparse.tnef import TNEF, TNEFAttachment, TNEFObject
from tnefparse.mapi import TNEFMAPI_Attribute
t = TNEF(open("winmail.dat").read(), do_checksum=True)
for a in t.attachments:
with open(a.name, "wb") as afp:
afp.write(a.data)
sys.exit("Successfully wrote %i files" % len(t.attachments))
source to share