2013-08-18 45 views
-1

我的安裝程序使用fetchmail從Gmail中取出電子郵件,這些電子郵件由procmail處理並傳遞給python腳本。python email.message_from_string()解析問題

當我使用email.message_from_string()時,生成的對象不會被解析爲電子郵件對象。 get_payload()將電子郵件的標題/正文/有效內容文本作爲單個文本塊進行返回。

這是文本返回:

From [email protected] Sat Aug 17 19:20:44 2013 
>From example Sat Aug 17 19:20:44 2013 
MIME-Version: 1.0 
Received: from ie-in-f109.1e100.net [74.125.142.109] 
    by VirtualBox with IMAP (fetchmail-6.3.21) 
    for <[email protected]> (single-drop); Sat, 17 Aug 2013 19:20:44 -0700 (PDT) 
Received: by 10.70.131.110 with HTTP; Sat, 17 Aug 2013 19:20:42 -0700 (PDT) 
Date: Sat, 17 Aug 2013 19:20:42 -0700 
Delivered-To: [email protected] 
Message-ID: <[email protected]om> 
Subject: test 19:20 
From: example <[email protected]> 
To: example <[email protected]> 
Content-Type: multipart/alternative; boundary=001a1133435474449004e42f7861 

--001a1133435474449004e42f7861 
Content-Type: text/plain; charset=ISO-8859-1 

19:20 

--001a1133435474449004e42f7861 
Content-Type: text/html; charset=ISO-8859-1 

<div dir="ltr">19:20</div> 

--001a1133435474449004e42f7861-- 

我的代碼:

full_msg = sys.stdin.read() 
msg = email.message_from_string(full_msg) 
msg['to']   # returns None 
msg.get_payload() # returns the text above 

我缺少的是讓Python正確解讀電子郵件?

我看到從thesequestions我可能沒有得到正確的電子郵件標題沿線,但我不能確認。第2行的「>」不是拼寫錯誤:它在文本中。

回答

1

無論你說的「>」在文本中「,無論如何 - 這是錯誤的。除去這個人物後:

>python test.py <input.txt
example <[email protected]>
[<email.message.Message instance at 0x02810288>, <email.message.Message instance at 0x02810058>]

因此錯誤是不是在解析消息,但在 「>」 字符某種程度上破壞你的電子郵件文本。

+0

不知道什麼是添加「<」,但是一旦我在腳本中刪除它,解析器就可以正常工作。整個系統現在可以正常工作。感謝這個想法。 – schroeder