简体   繁体   English

用于后缀的Python过滤器

[英]Python filter for postfix

I am trying to make a simple Python filter for postfix, to add in a 'Reply-to' header to certain messages. 我正在尝试为postfix创建一个简单的Python过滤器,在某些消息的“Reply-to”标题中添加。

What I've done so far is to take the email from stdin, and parse it into an email object like so: 到目前为止我所做的是从stdin获取电子邮件,并将其解析为一个电子邮件对象,如下所示:

raw = sys.stdin.readlines()
msg = email.message_from_string(''.join(raw))

Then I've played with headers etc. 然后我玩了标题等。

msg.add_header('Reply-to', 'foo@bar.com')

And now want to re-inject that back into postfix. 现在想把它重新注入后缀。 Reading the filter readme associated with postfix, I should pass it back using the 'sendmail' command. 阅读与postfix相关的过滤器自述文件 ,我应该使用'sendmail'命令将其传回去。 However, I'm not sure how to pass the email object over to sendmail, for example using subprocess's 'call()' or whether I should use the smtplib's 'smtplib.SMTP()'? 但是,我不确定如何将电子邮件对象传递给sendmail,例如使用subprocess的'call()'或者是否应该使用smtplib的'smtplib.SMTP()'?

What would be the 'correct' method? 什么是'正确'的方法?

Thanks 谢谢

You should be able to use both methods, but smtplib.SMTP() is more flexible and makes the error handling easier. 您应该能够使用这两种方法,但smtplib.SMTP()更灵活,使错误处理更容易。

If you need an example, have a look at my framework for python filters : https://github.com/gryphius/fuglu/blob/master/fuglu/src/fuglu/connectors/smtpconnector.py#L67 如果您需要一个示例,请查看我的python过滤器框架https//github.com/gryphius/fuglu/blob/master/fuglu/src/fuglu/connectors/smtpconnector.py#L67

the re_inject method does exactly that (FUSMTPClient is a subclass of smtplib.SMTP), so basically it boils down to: re_inject方法就是这样(FUSMTPClient是smtplib.SMTP的子类),所以基本上归结为:

client = smtplib.SMTP('127.0.0.1',<yourportnumber for the receiving postfix instance>)
client.sendmail(<envelope from>, <envelope to>, <yourmessageobject>.as_string())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM