2011-08-30 62 views
1

用什麼方式可以從下面的字符串中使用python獲得'X-Mailer-recipient:'電子郵件ID。使用python從字符串獲取電子郵件ID的最佳方式

使用re?

Received: from localhost6.localdomain6 (unknown [59.92.85.188]) 
     by smtp.webfaction.com (Postfix) with ESMTP id 05B332078BD1 
     for <[email protected]>; Fri, 26 Aug 2011 04:59:36 -0500 (CDT) 
    Content-Type: text/html; charset="utf-8" 
    MIME-Version: 1.0 
    Content-Transfer-Encoding: 7bit 
    Subject: Test subject100 
    From: [email protected] 
    To: [email protected] 
    Date: Fri, 26 Aug 2011 10:01:39 -0000 
    Message-ID: <[email protected]> 
    X-Mailer-status: false 
    X-Mailer-recipient: [email protected] 

感謝

回答

1

您也可以使用這樣的事情:

d = """Received: from localhost6.localdomain6 (unknown [59.92.85.188]) by smtp.webfaction.com (Postfix) with ESMTP id 05B332078BD1 for <[email protected]>; Fri, 26 Aug 2011 04:59:36 -0500 (CDT) Content-Type: text/html; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: Test subject100 From: [email protected] To: [email protected] Date: Fri, 26 Aug 2011 10:01:39 -0000 Message-ID: <[email protected]> X-Mailer-status: false X-Mailer-recipient: [email protected]""" 

if 'X-Mailer-recipient:' in d: 
    d.split('X-Mailer-recipient:')[1].split()[0] 
>>> [email protected] 
+0

這還包括電子郵件地址後面的任何文字。 – Spycho

+0

@Spycho - 謝謝修復 –

+0

感謝您的回答,其實X-Mailer收件人可能在字符串中間,我應該得到正確的電子郵件 – shivg

2

使用正則表達式X-Mailer-recipient:\s*(.*)。您可以在Python中使用正則表達式,詳見here。您需要確保您不會意外地包含您正在查找的文本。例如,上面的正則表達式可以匹配所有的「X-Mailer-recipient:[email protected] BLARG BLARG BLARG」。然後您需要訪問所需的捕獲組。

4

使用email包:

from email import message_from_string 

msg = '''Received: from localhost6.localdomain6 (unknown [59.92.85.188]) 
    by smtp.webfaction.com (Postfix) with ESMTP id 05B332078BD1 
    for <[email protected]>; Fri, 26 Aug 2011 04:59:36 -0500 (CDT) 
Content-Type: text/html; charset="utf-8" 
MIME-Version: 1.0 
Content-Transfer-Encoding: 7bit 
Subject: Test subject100 
From: [email protected] 
To: [email protected] 
Date: Fri, 26 Aug 2011 10:01:39 -0000 
Message-ID: <[email protected]> 
X-Mailer-status: false 
X-Mailer-recipient: [email protected] 
''' 
mail = message_from_string(msg) 
print mail['x-mailer-recipient'] 

使用正則表達式是不是一個好主意,因爲一)頭名不區分大小寫,B)可以有多個具有相同名稱的標題,c)一個標題可以包含另一個,例如有人可能會有郵件地址「X-Mailer-recipient:@ hotmail.com」,這會混淆基於正則表達式的方法。

+0

它只返回沒有 – shivg

+0

你可以做不區分大小寫的正則表達式匹配 – steabert

+0

@steabert,是的,這是真的。我想我的觀點是,對於電子郵件標題,有人已經處理了解析電子郵件標題時所需的所有細節信息,以便您不必構建自己的解析機制。 –

相關問題