python解析text / plain email

时间:2016-11-12 20:02:31

标签: python email parsing

假设我有一封如下所示的电子邮件:

From hostname Tue Jun 15 21:43:30 2010
Received: (qmail 8580 invoked from network); 15 Jun 2010 21:43:22 -0400
Received: 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
Received: by fxm19 with SMTP id 19so170709fxm.3
for <username@domain.com>; Tue, 15 Jun 2010 18:47:33 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.103.84.1 with SMTP id m1mr2774225mul.26.1276652853684; Tue, 15
Jun 2010 18:47:33 -0700 (PDT)
Received: by 10.123.143.4 with HTTP; Tue, 15 Jun 2010 18:47:33 -0700 (PDT)
Date: Tue, 15 Jun 2010 20:47:33 -0500
Message-ID: <AANLkTikFsIjJ3KYW1HJWcAqQlGXNiXE2YMzrj39I0tdB@mail.gmail.com>
Subject: TEST 12
From: Full Name <username@sender.com>
To: username@domain.com
Content-Type: text/plain; charset=ISO-8859-1

ONE
TWO
THREE

我知道我可以使用msg = email.message_from_string(str)msg['To']来获取username@domain.com,但如何识别&#34; ONE TWO THREE&#34;在底部?奖金!如何识别所有&#34;已收到:&#34; k / v对在顶部?当我尝试:

msg['Received'] 

我只得到第一个。我如何得到第二个?

2 个答案:

答案 0 :(得分:1)

这只能解决“奖励”问题:您可以使用msg.get_all('Received')

>>> msg.get_all('Received')
['(qmail 8580 invoked from network); 15 Jun 2010 21:43:22 -0400', 'from mail-fx0-f44.google.com (209.85.161.44)']
>>> msg.get_all('Received')[1]
'from mail-fx0-f44.google.com (209.85.161.44)'

旁注,您可以在解释器中探索dir(msg)help(msg)

答案 1 :(得分:0)

我可以使用以下代码从电子邮件正文中识别“一两个”:

msg.get_payload()

是的我!