If you are trying to export mails from Thunderbird or some other mail client to Haiku, one way to do so is to export them as .eml-files.
They have the benefit of being almost the same as Haiku mail files - buuuuut… without the attributes. That’s not very practical. The mail attributes also disappear if moved to a filesystem without support for extended file attributes. Also not very practical.
Following script kinda sorta takes care of that partially, more or less - if it doesn’t blow up in your face… Use at your own peril.
It should be noted that it almost didn’t break down on those 28,400 emails I needed fixed, so apart from a few misconfigured mailing lists and some spam (they don’t care about being standard compliant, apparently) it worked really well. The conditional statements should mostly take care of those breakdowns.
Save the script as eml_attributefixer.py (or something similar or whatever) and remember to run it in Terminal from the directory, where your mails-to-fix are stored. And to make it easy, make the script executable.
It can be used on Haiku mails IF you change eml_files = glob.glob*(path + '*.eml')
to eml_files = glob.glob(path + '*')
- e.g. remove the .eml part of the overall path.
Good luck.
-mrjones
EDITED: changed the conditional statements to be much more reliable - also covers nonetype now - also, setting mime type works correctly now - borked that one in my ‘sanitizing’ the script.
#!/usr/bin/env python3
import subprocess
import email
from email import policy
from email.parser import BytesParser
import glob
import os
path = './' # set this to "./" if in current directory
eml_files = glob.glob(path + '*.eml') # get all .eml files in a list - remove .eml to work on all files, incl. Haiku mails
for eml_file in eml_files:
with open(eml_file, 'rb') as fp: # select a specific email file from the list
name = fp.name # Get file name
msg = BytesParser(policy=policy.default).parse(fp)
fp.close()
print (name) # Get name of eml file
# change this to your account_id or comment out
subprocess.run(["addattr", "-tint", "MAIL:account_id", '1234567890', name ])
# change this to your email account - or comment out
subprocess.run(["addattr", "-tstring", "MAIL:account", 'yourmail@domain.bleh', name ])
subprocess.run(["addattr", "-tstring", "MAIL:from", msg['from'], name ])
subprocess.run(["addattr", "-ttime", "MAIL:when", msg['date'], name ])
subprocess.run(["addattr", "-tstring", "MAIL:status", 'Read', name ])
subprocess.run(["addattr", "-tint", "MAIL:read", '2', name ])
subprocess.run(["addattr", "-tmime", "BEOS:TYPE", 'text/x-email', name ])
if msg['subject']:
subprocess.run(["addattr", "-tstring", "MAIL:subject", msg['subject'], name ])
else:
subprocess.run(["addattr", "-tstring", "MAIL:subject", 'empty subject', name ])
if msg['to']:
subprocess.run(["addattr", "-tstring", "MAIL:to", msg['to'], name ])
elif msg['Delivered-To']:
subprocess.run(["addattr", "-tstring", "MAIL:to", msg['Delivered-To'], name ])
else:
subprocess.run(["addattr", "-tstring", "MAIL:to", 'no recipient', name ])
if msg['MIME-Version']:
subprocess.run(["addattr", "-tstring", "MAIL:mime", msg['MIME-Version'], name ])