将Maildir转换为mbox

11

我正在更换主机,需要将旧服务器上的maildir帐户转换为新服务器上的mbox格式。请问有什么好的方法吗?

我找到了这个:

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

但我不是很理解它。我对Linux有基本的了解,并且可以通过ssh访问我的服务器。

大多数Maildir文件夹都有以下内容:


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


我需要全部复制还是可以忽略dovecot文件?

非常感谢您的建议。

C


这应该放在http://serverfault.com上。 - Rickard von Essen
3个回答

30

如果需要将maildir账户转换为不设置邮件服务器的邮箱账户,可以使用python的mailbox库。如果有一个单独的maildir文件夹要转换,可以使用这个小巧的(10行+注释)Python脚本,网址在这里。如果有子文件夹,则需要探索两种格式之间不同的子文件夹结构。以下是相应的脚本:

#!/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')

7
我创建了一个Python 3版本,用于处理在将maildir目录从Linux分区移动到Windows分区时可能发生的文件系统编码问题。从这里下载:https://github.com/bluebird75/maildir2mbox - Philippe F
这对我非常完美地运行。将 Dovecot 邮件导入到 Thunderbird 中。 - Gambo
当使用Python 2.7以上的版本时,该行代码maildir = mailbox.Maildir(maildirname, email.message_from_file)应更改为maildir = mailbox.Maildir(maildirname)。 - Juanjo
当我运行此程序时,Thunderbird 中导入的结果具有正确的标题(主题,日期,时间等),但所有消息的正文都像“º×½Á×ïÓ›‡Mg½ŽC&...”一样是乱码。我做错了什么? - posix
@posix 我使用了Philippe F制作的脚本,它很好地将Kmail文件夹导入到Thunderbird中。也许你应该尝试一下这个,而不是Frédéric的脚本! - Simon C.

3

如果您可以通过imap访问两个服务器(或者可以暂时安排访问),您可能需要考虑使用imapsync工具,例如:

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

如果那样行不通,你应该可以忽略dovecot文件,但要注意你可能会失去一些信息,比如哪些消息已读以及在消息上设置的任何标志。(使用imapsync方法将保留所有这些内容。)

-1

如果您有Maildir文件数据,并且想将其导入到MBOX帐户中,则可以使用任何具有将Maildir文件导入到MBOX帐户的能力的第三方工具来获得帮助。但是在我看来,您应该使用此Maildir到MBOX转换器工具的免费试用版,它可以通过简单易行的点击将单个和多个Maildir文件导入到MBOX帐户中。

访问网站:https://www.spikevare.com/maildir/


那个工具只适用于Windows操作系统。 - BYTE RIDER

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接