2012-09-14 44 views
1

我试图从IMAP提取未读邮件。当我尝试解析电子邮件内容时,我收到len(email_message.keys()) == 0。所以我永远不会得到FromToSubject解析邮件,来自和主题

打印电子邮件(email.message_from_string(email_str)):

From nobody Fri Sep 14 13:42:50 2012 

1 (RFC822 {1015} 
Return-Path: <[email protected]> 
X-Original-To: [email protected] 
Delivered-To: [email protected] 
Received: from ec2.....amazonaws.com (unknown [IP]) 
    (Authenticated sender: [email protected]) 
    by domain.com (Postfix) with ESMTPA id EACD436CF 
    for <[email protected]>; Fri, 14 Sep 2012 12:47:54 +0000 (UTC) 
DKIM-Signature: .... 
Content-Type: text/plain; charset="us-ascii" 
MIME-Version: 1.0 
Content-Transfer-Encoding: 7bit 
From: [email protected] 
To: [email protected] 
Subject: welcome 

Dear recipient, 

Welcome. 

Best, 
Robot 

而这里的代码:

def fetch_new_emails(host, port, user, password): 
    conn = imaplib.IMAP4(host=host, port=port) 

    try: 
    (retcode, capabilities) = conn.login(user, password) 
    conn.select(readonly=1) # Select inbox or default namespace 
    (retcode, messages) = conn.search(None, '(UNSEEN)') 
    results = [] 
    if retcode == 'OK': 
     for message in messages[0].split(' '): 
     (ret, raw_email) = conn.fetch(message, '(RFC822)') 
     if ret == 'OK': 
      print raw_email[0] 
      email_str = string.join(raw_email[0], "\n") 
      email_message = email.message_from_string(email_str) 
      email_from = email_message['From'] 
      to = email_message['To'] 
      subject = email_message['Subject'] 
      results.append({ 
      'from': email_from, 
      'to': to, 
      'subject': subject}) 
    except: 
    print sys.exc_info()[1] 
    sys.exit(1) 
    finally: 
    conn.close() 
    return results 

问题:

print email_message['From'] 
>>None  
print email_message['To'] 
>>None 
print email_message['Subject'] 
>>None 
+0

你能打印出raw_email的回应吗? – Max

回答

2

还有的From nobody...线后,一个奇怪的空行。从技术上讲,空行是标题的结尾,之后的所有内容都是正文,所以该消息实际上没有这些标题。

无论如何,IMAP消息不应该有From行(这是典型的Berkeley mbox格式,很少有IMAP服务器使用这种格式;即使您的存储实现的细节不应该对IMAP客户端可见) 。

奇怪的1 (RFC822 {1015}行也不属于;它看起来隐约像是IMAP协议响应的一部分,而不是实际消息的一部分。正确的消息以Return-Path:标题开始,在这种情况下。

IMAP服务器和/或客户端代码是否为生产版本?

+1

事实上,这是IMAP协议的一部分:''1(RFC822 {1015}''。实际上,这就是imaplib给你的FETCH响应,消息序列号1,RFC822响应,1015字节。的回应是实际的消息。 – Max