If you have python3 installed you can use the extract_msg
module which after installation will make available the extract_msg
utility in your path.
Install the extract_msg
utility:
pip3 install extract-msg
To get plain text and the attachments from an Outlook MSG file, run it as follows:
extract_msg mymessage.msg
The output will be created in a subfolder generated from the email subject line.
Customizing the output and getting RTF content
If you need more control over the output, below is a minimal python script based on the module above that takes the path to the outlook MSG file as the first parameter and saves the email content as plain text, and also as a RTF file (if RTF content is present, usually it is).
Create the script and make it executable (e.g. chmod +x msg2text.py
):
#!/usr/bin/env python3
import os
import sys
import extract_msg
ATTACHMENTS_DIR = 'attachments'
file_name = sys.argv[1]
msg = extract_msg.Message(file_name)
with open(file_name.replace('.msg', '.txt'), 'w', encoding='utf-8') as fout:
fout.write(f'Sender: {msg.sender}')
fout.write(f'Sent On: {msg.date}')
fout.write(f'To: {msg.to}')
fout.write(f'Subject: {msg.subject}')
fout.write(f'Body: {msg.body}')
fout.write(f'Attachments: {[a.longFilename for a in msg.attachments]}')
if msg.rtfBody:
with open(file_name.replace('.msg', '.rtf'), 'wb') as fout_rtf:
fout_rtf.write(msg.rtfBody)
os.makedirs(ATTACHMENTS_DIR, exist_ok=True)
for attachment in msg.attachments:
attachment.save(customPath=ATTACHMENTS_DIR, customFilename=attachment.longFilename)
Run the script:
./msg2text.py message.msg
A file message.txt
and another file named message.rtf
will be generated and the attachments will be extracted to the attachments
folder.