将 Maildir 转换为 mbox

发布于 2024-08-26 09:42:27 字数 1827 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

-小熊_ 2024-09-02 09:42:27

如果需要将maildir账户转换为邮箱账户而不需要设置mailservers,可以使用python的邮箱库。如果要转换单个 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')

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')
最冷一天 2024-09-02 09:42:27

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

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

如果这不起作用,您应该能够忽略 dovecot 文件,但要注意,您可能会丢失信息,例如哪些消息是读取消息上设置的任何标志。 (imapsync 方法将保留所有这些内容。)

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

七禾 2024-09-02 09:42:27

您可以使用 mutt 来执行此操作。

以下命令会将 Maildir 文件夹转换为 mbox 文件:

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>"'

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>"'
清泪尽 2024-09-02 09:42:27

如果您有 Maildir 文件数据并且想要将 Maildir 文件导入到 MBOX 帐户,那么您可以借助任何能够将 Maildir 文件导入到 MBOX 帐户的第三方工具的帮助。但在我看来,您应该使用此 Maildir 到 MBOX 转换器工具的免费试用版,只需简单的点击即可将单个和多个 Maildir 文件导入到 MBOX 帐户。

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文