使用 Python 解析电子邮件 [英] Parsing email with Python

查看:46
本文介绍了使用 Python 解析电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Python 脚本来处理从 Procmail 返回的电子邮件.正如在这个问题中所建议的,我正在使用以下 Procmail 配置:

:0:|$HOME/process_mail.py

我的 process_mail.py 脚本正在通过标准输入接收电子邮件,如下所示:

来自主机名 Tue Jun 15 21:43:30 2010收到:(网络调用的qmail 8580);2010 年 6 月 15 日 21:43:22 -0400收到:来自mail-fx0-f44.google.com (209.85.161.44)通过 ip-73-187-35-131.ip.secureserver.net 和 SMTP;2010 年 6 月 15 日 21:43:22 -0400收到:由 fxm19 和 SMTP id 19so170709fxm.3对于 ;2010 年 6 月 15 日,星期二 18:47:33 -0700 (PDT)MIME 版本:1.0收到:由 10.103.84.1 使用 SMTP id m1mr2774225mul.26.1276652853684;周二,152010 年 6 月 18:47:33 -0700 (PDT)收到:由 10.123.143.4 使用 HTTP;2010 年 6 月 15 日,星期二 18:47:33 -0700 (PDT)日期:2010 年 6 月 15 日,星期二 20:47:33 -0500消息 ID:<AANLkTikFsIjJ3KYW1HJWcAqQlGXNiXE2YMzrj39I0tdB@mail.gmail.com>主题:测试 12发件人:全名 至:username@domain.com内容类型:文本/纯文本;字符集=ISO-8859-1一二三

我正在尝试以这种方式解析消息:

<预><代码>>>>导入电子邮件>>>msg = email.message_from_string(full_message)

我想获取诸如发件人"、收件人"和主题"之类的消息字段.但是,消息对象不包含任何这些字段.

我做错了什么?

解决方案

您必须确保这些行不会被意外损坏(如上所示,尽管很难说这是否是复制粘贴问题)--完整的消息,例如:

Received:(网络调用的qmail 8580);2010 年 6 月 15 日 21:43:22 -0400收到:来自 mail-fx0-f44.google.com (209.85.161.44) 由 ip-73-187-35-131.ip.secureserver.net 和 SMTP;2010 年 6 月 15 日 21:43:22 -0400收到:fxm19 使用 SMTP id 19so170709fxm.3 for <username@domain.com>;2010 年 6 月 15 日,星期二 18:47:33 -0700 (PDT)MIME 版本:1.0收到:由 10.103.84.1 使用 SMTP id m1mr2774225mul.26.1276652853684;2010 年 6 月 15 日,星期二 18:47:33 -0700 (PDT)收到:由 10.123.143.4 使用 HTTP;2010 年 6 月 15 日,星期二 18:47:33 -0700 (PDT)日期:2010 年 6 月 15 日,星期二 20:47:33 -0500消息 ID:<AANLkTikFsIjJ3KYW1HJWcAqQlGXNiXE2YMzrj39I0tdB@mail.gmail.com>主题:测试 12发件人:全名 至:username@domain.com内容类型:文本/纯文本;字符集=ISO-8859-1一二三

然后

msg = email.message_from_string(msgtxt)打印味精['主题']

根据需要打印TEST 12.

I'm writing a Python script to process emails returned from Procmail. As suggested in this question, I'm using the following Procmail config:

:0:
|$HOME/process_mail.py

My process_mail.py script is receiving an email via stdin like this:

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

I'm trying to parse the message in this way:

>>> import email
>>> msg = email.message_from_string(full_message)

I want to get message fields like 'From', 'To' and 'Subject'. However, the message object does not contain any of these fields.

What am I doing wrong?

解决方案

You must ensure that the lines are not accidentally broken (as they are above, though it's hard to say if that was a copy-paste problem) -- with an intact message such as:

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

then

msg = email.message_from_string(msgtxt)
print msg['Subject']

prints TEST 12 as desired.

这篇关于使用 Python 解析电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆