我正在尝试通过Python获取电子邮件的收件人。使用电子邮件模块在加载邮件时效果很好,并且可以使用“收件人”字段来了解:
import email
msg = email.message_from_string(some_message)
print "recipient: {}".format(msg['To'])
但是在许多情况下这不起作用,例如当收件人是密件抄送字段时。我一直在搜索地址,它似乎在“已接收”字段中编码。
如例:
In [25]: for l in msg.get_all('Received'):
....: print l
....:
[...]
from xxxx.xxxxx.com (xxxx.xxxxx.com [999.999.999.999]
by xxxx.xxxxx.com (Postfix) with ESMTP id FFFFFFFFFF
for <RECIPIENT@ADDR>; Sat, 4 May 2013 17:29:10 +0000 (UTC)
by xxxx.xxxxx.com with SMTP id 9999999999.9
for <RECIPIENT@ADDR>; Sat, 04 May 2013 10:29:10 -0700 (PDT)
by 10.70.31.33 with HTTP; Sat, 4 May 2013 10:28:50 -0700 (PDT)
解析这是了解收件人的正确方法吗?有没有办法解析它而不必自己实现解析器?
编辑:
示例中的some_message
字符串是(出于隐私原因,某些数据已被更改):
From from@addr.com Sat May 4 17:29:21 2013
Return-Path: <from@addr.com>
X-Original-To: script-99999999999999999999999999999999999@mx.xxxxxxxxx.com
Delivered-To: script-99999999999999999999999999999999999@mx.xxxxxxxxx.com
Received: from localhost (localhost.localdomain [127.0.0.1])
by mx.xxxxxxxxx.com (Postfix) with ESMTP id FFFFFFF
for <script-99999999999999999999999999999999999@mx.xxxxxxxxx.com>; Sat, 4 May 2013 17:29:21 +0000 (UTC)
X-Spam-Flag: NO
X-Spam-Score: -2.098
X-Spam-Level:
X-Spam-Status: No, score=-2.098 tagged_above=-999 required=3
tests=[BAYES_00=-1.9, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001,
RCVD_IN_DNSWL_LOW=-0.7, T_DKIM_INVALID=0.5]
Received: from mx.xxxxxxxxx.com ([127.0.0.1])
by localhost (yyy.xxxxxxxxx.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id EEEEEEEE
for <script-99999999999999999999999999999999999@mx.xxxxxxxxx.com>;
Sat, 4 May 2013 17:29:10 +0000 (UTC)
Received: from mail.zzzzzz.com (mail.zzzzzz.com [99.99.99.99])
by mx.xxxxxxxxx.com (Postfix) with ESMTP id AAAAAAAA
for <RECIPIENT@ADDR>; Sat, 4 May 2013 17:29:10 +0000 (UTC)
Received: by mail.zzzzzz.com with SMTP id wz19so1388292pbc.3
for <RECIPIENT@ADDR>; Sat, 04 May 2013 10:29:10 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=zzzzzz.com; s=20120113;
h=x-received:mime-version:from:date:message-id:subject:to
:content-type;
bh=2eA0+yYWPhD/5+hE3gbsF0Q7Y4kT0ZEPjnLAHCkmsqc=;
b=ioHV7VqfAbClFrgdsafzlSOh/eoa3G0FEhMNTFlHcOMU+tq2a/9GMASbnsXYgH8dVr
ZeHGV2Cpa0IPIt0sfr7mnJqsmzUSND1tYpWTnhcXn0b9II5FTScVRHTSigcDY6EvWZs2
Cye/HCZtqnfjzVC/Vc5SrmDzap2yVNxUsMBzUrjRaoqrCH4/4FavilOJUaNYhI1XQela
b1YLeh7GpmU1/CpnkAw4Dy6Hx4l7BDHYfvwA6pSuOwQb45KVdMzMOFCIKpIKRpwr5SVF
TFrB+hZqULgEtyqluirFEw1HqPG7nn3Nq00ViX1P5o48tUlqx0M7hWbHOd7xxKfhQS/F
3UxQ==
X-Received: by 99.99.99.999 with SMTP id x3mr19111157pbl.92.1367688550046;
Sat, 04 May 2013 10:29:10 -0700 (PDT)
MIME-Version: 1.0
Received: by 99.99.99.99 with HTTP; Sat, 4 May 2013 10:28:50 -0700 (PDT)
From: "from@addr.com" <from@addr.com>
Date: Sat, 4 May 2013 19:28:50 +0200
Message-ID: <CBLwV1i-491fOX8KVhv3tr69mhqVCkZ=y44V0MwVJQpMPtfxN-Q@mail.zzzzzz.com>
Subject: prova2
To: undisclosed-recipients:;
Content-Type: multipart/alternative; boundary=bcaec52e5c2d30634004dbe7cee5
--bcaec52e5c2d30634004dbe7cee5
Content-Type: text/plain; charset=ISO-8859-1
undisclosed
Carles
--bcaec52e5c2d30634004dbe7cee5
Content-Type: text/html; charset=ISO-8859-1
undisclosed<br clear="all"><div>Carles</div>
--bcaec52e5c2d30634004dbe7cee5--