Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Combine a directory of .eml files into a single Unix "mbox" file.
#!/usr/bin/env python
""" Converts a directory full of .eml files to a single Unix "mbox" file.
Accepts as input either an individual .eml file or a directory containing one
or more .eml files.
The output mbox will be created if it doesn't already exist. If it exists,
it will be appended to. There is no checking for duplicates, so use caution.
If duplicate filtering is desired, it could be added to addFileToMbox().
Inspired by
$ ./ inputdir/ output.mbox
$ ./ input.eml output.mbox
Requires Python 2.5 or later
STATUS: Tested and appears to work.
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":
fi = open(os.path.join(infile_name, filename), 'r')
sys.stderr.write("Error while opening " + filename + "\n")
addFileToMbox( fi, dest_mbox )
count += 1
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"
fi = open(infile_name, 'r')
sys.stderr.write("Error while opening " + infile_name + "\n")
addFileToMbox( fi, dest_mbox )
dest_mbox.close() # close/unlock the mbox file
return 0
def addFileToMbox( fi, dest_mbox ):
# Any additional preprocessing logic goes here, e.g. duplicate filter
dest_mbox.add( fi )
if __name__ == "__main__":
if len(sys.argv) != 3:
sys.stderr.write("Usage: ./ input outbox.mbox\n")
sys.exit( main( sys.argv ) )

This comment has been minimized.

Copy link

@Fliptron Fliptron commented Feb 7, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment