我在下面写了 2 个代码片段。第二个按预期工作,但不是第一个。我无法找出为什么第一个片段中第 22 行的 print(msg_obj.keys()) 的输出列表不包含“主题”、“来自”键,而 msg_obj包含标题字段,如“主题”、“发件人”。当我使用此脚本将电子邮件转储到 mbox 文件并稍后使用某些实用程序(mboxview.exe for windows)打开该文件时,实用程序无法识别任何转储的电子邮件。请帮我解决这个问题。任何建议都非常受欢迎。
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() 解决了问题。但不明白为什么?