【问题标题】:How to convert mbox file to .msg file format using python?如何使用 python 将 mbox 文件转换为 .msg 文件格式?
【发布时间】:2021-01-04 07:40:52
【问题描述】:

我想将 mbox 文件转换为 msg 格式。为此,我已经完成了,但我没有得到正确的格式。我能够读取 mbox 文件,但我不知道如何使用它创建 msg 文件。我已将 mbox 文件转换为 eml 文件,但我想以同样的方式创建 msg 文件,但我不知道如何做到这一点。

下面是mbox转eml的代码

 import os
import mailbox
from email import generator
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

count = 0


def emlGenerator(body, thisemail):
    global count
    msg = MIMEMultipart('alternative')
    msg['Subject'] = thisemail['subject']
    msg['From'] = thisemail['From']
    msg['To'] = thisemail['To']
    msg['Cc'] = thisemail['Cc']
    msg['Bcc'] = thisemail['Bcc']
    msg['Date'] = thisemail['Date']
    name = str(count) + '.eml'
    count += 1
    part = MIMEText(body)
    msg.attach(part)
    outfile_name = os.path.join('xxxxx/test2', name)
    with open(outfile_name, 'w') as outfile:
        gen = generator.Generator(outfile)
        gen.flatten(msg)


def getcharsets(msg):
    charsets = set({})
    for c in msg.get_charsets():
        if c is not None:
            charsets.update([c])
    return charsets


def handleerror(errmsg, emailmsg, cs):
    print()
    print(errmsg)
    print("This error occurred while decoding with ", cs, " charset.")
    print("These charsets were found in the one email.", getcharsets(emailmsg))
    print("This is the subject:", emailmsg['subject'])
    print("This is the sender:", emailmsg['From'])


def getbodyfromemail(msg):
    body = None
    # Walk through the parts of the email to find the text body.
    if msg.is_multipart():
        for part in msg.walk():
            # If part is multipart, walk through the subparts.
            if part.is_multipart():
                for subpart in part.walk():
                    if subpart.get_content_type() == 'text/plain':
                        # Get the subpart payload (i.e the message body)
                        body = subpart.get_payload(decode=True)
                        # charset = subpart.get_charset()
            # Part isn't multipart so get the email body
            elif part.get_content_type() == 'text/plain':
                body = part.get_payload(decode=True)
                # charset = part.get_charset()
    # If this isn't a multi-part message then get the payload (i.e the message body)
    elif msg.get_content_type() == 'text/plain':
        body = msg.get_payload(decode=True)
        # No checking done to match the charset with the correct part.
    charsets = set({})
    for c in msg.get_charsets():
        if c is not None:
            charsets.update([c])
    for charset in charsets:
        try:
            body = body.decode(charset)
        except:
            print("Hit a UnicodeDecodeError or AttributeError. Moving right along.")
    return body


if __name__ == "__main__":
    for thisemail in mailbox.mbox('xxxxxx/topics.mbox'):
        print (thisemail['Message-id'])
        body = getbodyfromemail(thisemail)
        emlGenerator(body, thisemail)
    print("=========== DONE ============")
    print("Total ", count, " File")

【问题讨论】:

标签: python python-2.7 outlook win32com


【解决方案1】:

要将 .mbox 转换为 .msg 文件格式,您可以使用第三方库 Aspose.Email for Python via .NET

这是一个强大的电子邮件编程 API。电子邮件 API 可用于基本的电子邮件管理功能,例如消息内容编辑和附件操作,以及其高级功能,例如管理消息存储文件,通过包括 POP3、IMAP 和 SMTP 在内的多种协议发送和接收电子邮件。

例如,你可以看到下面的代码。

reader = MboxrdStorageReader(dir + "template.mbox", False)
eml = reader.read_next_message()
# Read all messages in a loop
while (eml is not None):
    # show message subject
    print("Subject: " + eml.subject)
    # save message in EML & MSG formats
    eml.save("output.msg", aspose.email.SaveOptions.default_msg_unicode)
    # get the next message
    eml = reader.read_next_message();
reader.dispose();

对我来说,这种方法可能对你有用。

我是 Aspose 的开发人员宣传员。

【讨论】:

  • 我不知道是谁设计了这个 API,但是安装它是不可能的,当你安装它时,它就无法工作。这甚至受支持吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
  • 2017-08-22
相关资源
最近更新 更多