Python:Postfix标准输入 [英] Python : Postfix stdin

查看:191
本文介绍了Python:Postfix标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让后缀将所有电子邮件发送到将扫描电子邮件的python脚本中.

I want to make postfix send all emails to a python script that will scan the emails.

但是,如何将后缀的输出传递给python?

However, how do I pipe the output from postfix to python ?

什么是Python的stdin?

What is the stdin for Python ?

您可以举一个代码示例吗?

Can you give a code example ?

推荐答案

要将邮件从后缀推送到python脚本,请在后缀别名文件中添加以下行:

To push mail from postfix to a python script, add a line like this to your postfix alias file:

# send to emailname@example.com
emailname: "|/path/to/script.py"

python email.FeedParser模块可以构造一个代表MIME电子邮件的对象来自stdin的消息,方法是这样:

The python email.FeedParser module can construct an object representing a MIME email message from stdin, by doing something like this:

# Read from STDIN into array of lines.
email_input = sys.stdin.readlines()

# email.FeedParser.feed() expects to receive lines one at a time
# msg holds the complete email Message object
parser = email.FeedParser.FeedParser()
msg = None
for msg_line in email_input:
   parser.feed(msg_line)
msg = parser.close()

从这里开始,您需要遍历msg的MIME部分并相应地对其进行操作.有关所需的方法,请参考有关email.Message对象的文档.例如,email.Message.get("Header")返回Header的标头值.

From here, you need to iterate over the MIME parts of msg and act on them accordingly. Refer to the documentation on email.Message objects for the methods you'll need. For example email.Message.get("Header") returns the header value of Header.

这篇关于Python:Postfix标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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