Python script for mail attribute fixing (.eml conversion)

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 (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.

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 = # Get file name
        msg = BytesParser(policy=policy.default).parse(fp)
    print (name) # Get name of eml file
	# change this to your account_id or comment out["addattr", "-tint", "MAIL:account_id", '1234567890', name ])
    # change this to your email account - or comment out["addattr", "-tstring", "MAIL:account", 'yourmail@domain.bleh', name ])["addattr", "-tstring", "MAIL:from", msg['from'], name ])["addattr", "-ttime", "MAIL:when", msg['date'], name ])["addattr", "-tstring", "MAIL:status", 'Read', name ])["addattr", "-tint", "MAIL:read", '2', name ])["addattr", "-tmime", "BEOS:TYPE", 'text/x-email', name ])
    if msg['subject']:["addattr", "-tstring", "MAIL:subject", msg['subject'], name ]) 
    else:["addattr", "-tstring", "MAIL:subject", 'empty subject', name ]) 
    if msg['to']:["addattr", "-tstring", "MAIL:to", msg['to'], name ]) 
    elif msg['Delivered-To']:["addattr", "-tstring", "MAIL:to", msg['Delivered-To'], name ]) 
    else:["addattr", "-tstring", "MAIL:to", 'no recipient', name ]) 
    if msg['MIME-Version']: ["addattr", "-tstring", "MAIL:mime", msg['MIME-Version'], name ])

Oh, and “thank you very much” to OscarL for kindly forcing me in the right direction in finding inspiration for a solution to my attribute fixing needs.


Thanks very much for that. It works!
Bookmarked! :slight_smile:

1 Like

Glad you made it work! (OscarL in bad disguise here).

Now, @Mr.Jones… the real question is how the heck your account is from 2004? did you just never re-registered when that was supposedly required back in 2006 (I’m still “salty” about losing my early account registration date :smiley:. At least I have 3 post from 2004 (example) to prove I was here back then :stuck_out_tongue:).


Uuhhh… I had to dig reeeeaaaallly deeeeeeeeeeeeeeep in my memory there.

That’s right. There was that thing with rëregistration back in the days of Svend Tveskæg or there about. What was not widely broadcast, but merely written in a single comment in one thread was following: You did not really have to be rëregistered - you could also send a message to whomever was responsible for the forum back then; and your profile would be marked as whatever it had to be marked as or migrated or sumthen’ - while you kept your account.

I cannot remember if I sent a pm or merely wrote a ‘me too’ post to keep my old account. It is all very vague in my admittedly holey memory.

1 Like