-
-
Save kadin2048/c332a572a388acc22d56 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3 | |
""" Converts a directory full of .eml files to a single Unix "mbox" file. | |
This is similar to http://www.cosmicsoft.net/emlxconvert.html | |
Accepts as input either an individual .eml file or a directory containing one | |
or more .eml files. | |
Usage: | |
$ ./emlToMbox.py inputdir/ output.mbox | |
$ ./emlToMbox.py input.eml output.mbox | |
STATUS: Lightly tested using Python 3.9.1 | |
""" | |
import os | |
import sys | |
import mailbox | |
global debug | |
debug = True | |
def main( arguments ): | |
infile_name = arguments[1] | |
dest_name = arguments[2] | |
if debug: | |
print("Input is: " + infile_name) | |
print("Output is: " + dest_name) | |
dest_mbox = mailbox.mbox(dest_name, create=True) # if dest doesn't exist create it | |
dest_mbox.lock() # lock the mbox file | |
if os.path.isdir(infile_name): | |
if debug: | |
print("Detected directory as input, using directory mode") | |
count = 0 | |
for filename in os.listdir(infile_name): | |
if filename.split('.')[-1] == "eml": | |
try: | |
fi = open(os.path.join(infile_name, filename), 'r') | |
except: | |
sys.stderr.write("Error while opening " + filename + "\n") | |
dest_mbox.close() | |
raise | |
addFileToMbox( fi, dest_mbox ) | |
count += 1 | |
fi.close() | |
if debug: | |
print("Processed " + str(count) + " total files.") | |
if infile_name.split('.')[-1] == "eml": | |
if debug: | |
print("Detected .eml file as input, using single file mode") | |
try: | |
fi = open(infile_name, 'r') | |
except: | |
sys.stderr.write("Error while opening " + infile_name + "\n") | |
dest_mbox.close() | |
raise | |
addFileToMbox( fi, dest_mbox ) | |
fi.close() | |
dest_mbox.close() # close/unlock the mbox file | |
return 0 | |
def addFileToMbox( fi, dest_mbox ): | |
# Any additional preprocessing logic goes here... | |
try: | |
dest_mbox.add( fi ) | |
except: | |
dest_mbox.close() | |
raise | |
if __name__ == "__main__": | |
if len(sys.argv) != 3: | |
sys.stderr.write("Usage: ./emlToMbox.py input outbox.mbox\n") | |
sys.exit(1) | |
sys.exit( main( sys.argv ) ) |
@Fliptron: Glad this was of use to you! I haven't had to run it in a while, but next time I do, I'll likely update it for Python 3 and probably also add some sort of duplicate filtering (so that I can use it to update existing MBOX archives with directories of emlx messages that have had new messages added).
Thanks, it saved my skin yesterday. I was getting some errors related to Print in Python 3.10 but all you need to do is add ( and ) to it, like:
print "Input is: " + infile_name
to
print ("Input is: " + infile_name)
fantastic! just needed the py3 fixes mentioned above by erebar.
Excellent @erebar! I'm honestly a bit surprised it worked under Python3 with only those minor changes. Great to know.
Thank you @kadin2048, this has been very useful when migrating mail providers!
With @kadin2048's blessing, I forked this into a repository (rather than a Gist), so I could combine it with an existing mbox de-duplicator (Jachimo/mbox_dedupe):
https://github.com/Jachimo/emlToMbox
Pull requests welcome.
@kadin2048 Please add a license comment so that we can use and modify your excellent work! Without a license, this code is illegal to reproduce (copy), distribute, or create derivative works from (modify).
The MIT license is pretty good, and I put your name on it for you. Simply copy and paste the following to the top of the file:
# Copyright © 2023 Kadin2048
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Alternatively, you could choose a license for yourself.
This worked great. Thank you! 🙏
Thanks for this.
My challenge was email in Earthlink WebMail that exports as a zip of separate eml files
to my mail reader on Windows 7 Pro , Forte Agent. Agent can read all emails from Earthlink but not
selective which is what I wanted. So I selected in the Web/Browser the emails I wanted to export, and
got a zip which I unpacked into a folder. A small problem with Python version requirement.
In a command box, my successful usage is:
D:\temp1>C:\Python27\python.exe EML_multi_to_MBOX.py mail_dl merged.txt
Input is: mail_dl
Output is: merged.txt
Detected directory as input, using directory mode
Processed 21 total files.
Thank you Very Much.