Convert Maildir to mbox [closed]
Asked Answered
C

4

12

I'm changing hosts and I need to convert my maildir accounts on my old server to mbox on my new server.

Any ideas on the best way to do this?

I found this:

for i in new/* cur/*; do
    formail <”$i” >> ../mbox
done

But I don't really understand it. I have a basic knowledge of Linux and I have root access to my server via SSH.

Most of the Maildir folders have the following contents:

cur       
new      
tmp                 
dovecot.index.log      
dovecot-uidlist    
dovecot.index         
dovecot.index.log.2    
dovecot.index.cache  
dovecot-keywords   
subscriptions

Do I need all of that or can I ignore the dovecot files?

Circinus answered 23/3, 2010 at 15:31 Comment(4)
This should be on serverfault.comGreatgrandaunt
MBOX to Maildir conversion scriptsSevenfold
The Dovecot files are useful to Dovecot but of course, your new mbox server is probably not running Dovecot.Crustaceous
See Dovecot's wiki: Converting between mailbox formatsMinisterial
M
3

If you have access to both servers via imap (or can temporarily arrange it), you might want to consider using an imapsync tool, eg:

http://freshmeat.net/projects/imapsync/

If that won't work, you should be able to ignore the dovecot files, but beware that you'll likely lose information like which messages are read and any flags set on the messages. (The imapsync method would preserve all those things.)

Marginate answered 25/3, 2010 at 6:21 Comment(0)
D
31

If one needs to convert a maildir account into a mailbox account without setting mailservers, one can use the mailbox library of python. If one has a single maildir folder to convert, one can use this small (10 lines+comments) python script found here. If one has subfolder, one needs to explore the subfolder structure, which is different between the two formats. This gives the following script :

#!/usr/bin/env python 
# -*- coding: utf-8 -*-
"""
Frédéric Grosshans, 19 January 2012
Nathan R. Yergler, 6 June 2010

This file does not contain sufficient creative expression to invoke
assertion of copyright. No warranty is expressed or implied; use at
your own risk.

---

Uses Python's included mailbox library to convert mail archives from
maildir [http://en.wikipedia.org/wiki/Maildir] to 
mbox [http://en.wikipedia.org/wiki/Mbox] format, icluding subfolder.

See http://docs.python.org/library/mailbox.html#mailbox.Mailbox for 
full documentation on this library.

---

To run, save as md2mb.py and run:

$ python md2mb.py [maildir_path] [mbox_filename]

[maildir_path] should be the the path to the actual maildir (containing new, 
cur, tmp, and the subfolders, which are hidden directories with names like 
.subfolde.subsubfolder.subsubsbfolder);

[mbox_filename] will be newly created, as well as a [mbox_filename].sbd the 
directory.
"""

import mailbox
import sys
import email
import os

def maildir2mailbox(maildirname, mboxfilename):
    """
    slightly adapted from maildir2mbox.py, 
    Nathan R. Yergler, 6 June 2010
    http://yergler.net/blog/2010/06/06/batteries-included-or-maildir-to-mbox-again/


    """
    # open the existing maildir and the target mbox file
    maildir = mailbox.Maildir(maildirname, email.message_from_file)
    mbox = mailbox.mbox(mboxfilename)

    # lock the mbox
    mbox.lock()

    # iterate over messages in the maildir and add to the mbox
    for msg in maildir:
        mbox.add(msg)

    # close and unlock
    mbox.close()
    maildir.close()

#Creates the main mailbox
dirname=sys.argv[-2]
mboxname=sys.argv[-1]
print(dirname +' -> ' +mboxname)
mboxdirname=mboxname+'.sbd'
maildir2mailbox(dirname,mboxname)
if not os.path.exists(mboxdirname): os.makedirs(mboxdirname)

listofdirs=[dn for dn in os.walk(dirname).next()[1] if dn not in ['new', 'cur', 'tmp']]
for curfold in listofdirs:
    curlist=[mboxname]+curfold.split('.')
    curpath=os.path.join(*[dn+'.sbd' for dn in curlist if dn])
    if not os.path.exists(curpath): os.makedirs(curpath)
    print('| ' +curfold +' -> '+curpath[:-4])
    maildir2mailbox(os.path.join(dirname,curfold),curpath[:-4])

print('Done')
Deedradeeds answered 19/1, 2012 at 17:1 Comment(5)
I created a Python 3 version, to deal with filesystem encoding problems that can happen when moving a maildir directory from a linux partition to a windows partition. Download it from here: github.com/bluebird75/maildir2mboxExpound
This worked perfectly for me. Importing dovecot emails into thunderbird.Dim
When working with versions above Python 2.7, the line maildir = mailbox.Maildir(maildirname, email.message_from_file) should read maildir = mailbox.Maildir(maildirname)Klemm
When I run this the imported results in Thunderbird have correct headers (subject, date, time, etc) but body of all messages is gobbledeegook like "º×½Á×ïÓ›‡Mg½ŽC&..." What am I doing wrong?Steenbok
@Steenbok I used the scrip made by Philippe F and it worked very well to import Kmail folders into Thunderbird. Maybe you should try that instead of Frédéric's script!Lumbricoid
M
3

If you have access to both servers via imap (or can temporarily arrange it), you might want to consider using an imapsync tool, eg:

http://freshmeat.net/projects/imapsync/

If that won't work, you should be able to ignore the dovecot files, but beware that you'll likely lose information like which messages are read and any flags set on the messages. (The imapsync method would preserve all those things.)

Marginate answered 25/3, 2010 at 6:21 Comment(0)
E
0

You can use mutt to do this.

The following command will convert a Maildir folder to an mbox file:

mutt -f /path/to/Maildir_folder -e 'set mbox_type=mbox; set confirmcreate=no; set delete=no; push "T.*<enter>;s/path/to/result.mbox<enter><quit>"'
Eventual answered 1/6 at 10:38 Comment(0)
O
-1

If you have Maildir file data and you want to Import your Maildir file to MBOX Account then you can take the help of any third-party tool that has the ability to Import Maildir file to an MBOX Account. but In my opinion, you should use the free trial version of this Maildir to MBOX Converter tool that Imports single and multiple Maildir files to MBOX Account in just simple and easy clicks.

Visit at : https://www.spikevare.com/maildir/

Orchardist answered 28/3, 2022 at 12:22 Comment(1)
That tool is only for Windows.Billion

© 2022 - 2024 — McMap. All rights reserved.