无法使用python将从gmail检索到的电子邮件成功转储到mbox文件

时间:2013-05-19 05:14:08

标签: python python-3.x

我在下面写了两段代码片段。第二个工作是预期的,但不是第一个。我无法找出为什么第一个代码段中第22行的打印输出列表(msg_obj.keys())不包含'主题','来自'键而msg_obj包含标题字段,如“主题”,“发件人”。当我使用此脚本将电子邮件转储到mbox文件并稍后使用某个实用程序(用于Windows的mboxview.exe)打开该文件时,实用程序无法识别任何已转储的电子邮件。请帮我解决此问题。任何建议都是最受欢迎的。

import imaplib,email,mailbox

M=imaplib.IMAP4_SSL('imap.gmail.com',993)
status,data=M.login('someone@gmail.com', 'password')
M.select()

#create new mbox file if doesn't exist
mbox_file=mailbox.mbox('gmail_mails.mbox')
mbox_file.lock()

#get all mails number   
status,data=M.search(None, 'ALL')

try:
    for mail_no in data[0].split():

        status,msg=M.fetch(mail_no,'(RFC822)')

        msg_obj=email.message_from_string(str(msg[0][1]))

        #print for debugging purpose
        print(msg_obj.keys())
        print(msg_obj["Subject"])

        mbox_msg_obj=mailbox.mboxMessage(msg_obj)

        mbox_file.add(mbox_msg_obj)

        mbox_file.flush()

finally:
    mbox_file.unlock()
    mbox_file.close()

M.close()
M.logout()

我发现以下代码的情况也是如此:

from email.parser import Parser

str="""Received: (qmail 8580 invoked from network); 15 Jun 2010 21:43:22      -0400\r\nReceived: from mail-fx0-f44.google.com (209.85.161.44) by ip-73-187-35-131.ip.secureserver.net with SMTP; 15 Jun 2010 21:43:22 -0400\r\nReceived: by fxm19 with SMTP id 19so170709fxm.3 for <username@domain.com>; Tue, 15 Jun 2010 18:47:33 -0700 (PDT)\r\nMIME-Version: 1.0\r\nReceived: by 10.103.84.1 with SMTP id m1mr2774225mul.26.1276652853684; Tue, 15 Jun 2010 18:47:33 -0700 (PDT)\r\nReceived: by 10.123.143.4 with HTTP; Tue, 15 Jun 2010 18:47:33 -0700 (PDT)\r\nDate: Tue, 15 Jun 2010 20:47:33 -0500\r\nMessage-ID: <AANLkTikFsIjJ3KYW1HJWcAqQlGXNiXE2YMzrj39I0tdB@mail.gmail.com>\r\nSubject: TEST 12\r\nFrom: Full Name <username@sender.com>\r\nTo: username@domain.com\r\nContent-Type: text/plain; charset=ISO-8859-1 ONE\nTWO\nTHREE"""

msg=Parser().parsestr(str)

print (msg['Subject'])
print (msg['From'])
print (msg['to'])

此处输出

TEST 12
Full Name <username@sender.com>
username@domain.com

使用email.parser.BytesParser()。parsebytes()而不是email.message_from_string()解决了问题。但是没有得到原因?

1 个答案:

答案 0 :(得分:0)

从您的示例代码中,我正在尝试以下操作:

import email

estr = """Received: (qmail 8580 invoked from network); 15 Jun 2010 21:43:22      -0400\r\nReceived: from mail-fx0-f44.google.com (209.85.161.44) by ip-73-187-35-131.ip.secureserver.net with SMTP; 15 Jun 2010 21:43:22 -0400\r\nReceived: by fxm19 with SMTP id 19so170709fxm.3 for <username@domain.com>; Tue, 15 Jun 2010 18:47:33 -0700 (PDT)\r\nMIME-Version: 1.0\r\nReceived: by 10.103.84.1 with SMTP id m1mr2774225mul.26.1276652853684; Tue, 15 Jun 2010 18:47:33 -0700 (PDT)\r\nReceived: by 10.123.143.4 with HTTP; Tue, 15 Jun 2010 18:47:33 -0700 (PDT)\r\nDate: Tue, 15 Jun 2010 20:47:33 -0500\r\nMessage-ID: <AANLkTikFsIjJ3KYW1HJWcAqQlGXNiXE2YMzrj39I0tdB@mail.gmail.com>\r\nSubject: TEST 12\r\nFrom: Full Name <username@sender.com>\r\nTo: username@domain.com\r\nContent-Type: text/plain; charset=ISO-8859-1 ONE\nTWO\nTHREE"""

msg = email.message_from_string(estr)

print (msg['Subject'])
print (msg['From'])
print (msg['to'])

打印结果很好。

TEST 12
Full Name <username@sender.com>
username@domain.com

因此,从您的第一个代码段开始,函数输入str(msg[0][1])必须包含一些不可解析的内容。你必须仔细看看str(msg[0][1])未能解析的内容。