开发者

Best way to get email id from a string using python

开发者 https://www.devze.com 2023-03-31 13:17 出处:网络
In what are the ways we can get \'X-Mailer-recipient:\' email id from this below string using python.

In what are the ways we can get 'X-Mailer-recipient:' email id from this below string using python.

using re?

    Received: from localhost6.localdomain6 (unknown [59.92.85.188])
        by smtp.webfaction.com (Postfix) with ESMTP id 05B332078BD1
        for <rshivaganesh@gmail.com>; 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: shivaganesh@gmail.com
    To: rshivaganesh@gmail.com
    Date: Fri, 26 Aug 2011 10:01:39 -0000
    Message-ID: <20110826100139.4763.43开发者_如何转开发322@localhost6.localdomain6>
    X-Mailer-status: false
    X-Mailer-recipient: rshivaganesh@gmail.com

Thanks


Using the email package:

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 <rshivaganesh@gmail.com>; 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: shivaganesh@gmail.com
To: rshivaganesh@gmail.com
Date: Fri, 26 Aug 2011 10:01:39 -0000
Message-ID: <20110826100139.4763.43322@localhost6.localdomain6>
X-Mailer-status: false
X-Mailer-recipient: rshivaganesh@gmail.com
'''
mail = message_from_string(msg)
print mail['x-mailer-recipient']

Using regular expressions is not a good idea because a) header names are case insensitive, b) there can be multiple headers with the same name, c) one header can encompass another one, e.g. someone could have the mail adress "X-Mailer-recipient:@hotmail.com" which will confuse regex based approaches.


Use the regex X-Mailer-recipient:\s*(.*). You can use regexes in Python as detailed here. You will need to make sure you don't accidentally include text past that which you are looking for. For example, the above regex would match all of "X-Mailer-recipient: a@b.c BLARG BLARG BLARG". You then need to access the desired capture group.


You can also use something like this:

d = """Received: from localhost6.localdomain6 (unknown [59.92.85.188]) by smtp.webfaction.com (Postfix) with ESMTP id 05B332078BD1 for <rshivaganesh@gmail.com>; 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: shivaganesh@gmail.com To: rshivaganesh@gmail.com Date: Fri, 26 Aug 2011 10:01:39 -0000 Message-ID: <20110826100139.4763.43322@localhost6.localdomain6> X-Mailer-status: false X-Mailer-recipient: rshivaganesh@gmail.com"""

if 'X-Mailer-recipient:' in d:
    d.split('X-Mailer-recipient:')[1].split()[0]
>>> rshivaganesh@gmail.com
0

精彩评论

暂无评论...
验证码 换一张
取 消