开发者

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.

What I've done so far is to take the email from stdin, 开发者_运维百科and parse it into an email object like so:

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. 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()'?

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.

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

the re_inject method does exactly that (FUSMTPClient is a subclass of smtplib.SMTP), so basically it boils down to:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜