3

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

jvc26
  • 6,363
  • 6
  • 46
  • 75

1 Answers1

4

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())
Gryphius
  • 75,626
  • 6
  • 48
  • 54
  • if you re-inject to port 25 you cause a loop (the message would be sent to your filter again). so you need an additional instance on a different port like 10026. example at : http://sourceforge.net/apps/trac/fuglu/wiki/FugluInstall (the block that starts with localhost:10026 inet n ... ) – Gryphius Aug 23 '11 at 06:47
  • Ah thanks for the reply - I've had a read through fuglu now, that makes sense. Can I re-inject to 10026 even though I'm taking from stdin? The Postfix docs appear to talk about the 10026 re-injection only in the context of a listening service (like Fuglu)? – jvc26 Aug 23 '11 at 07:08
  • 1
    AFAIK it doesn't matter how the message gets into your filter (stdin/smtp/....). The additional postfix instance on 10026 doesn't know anything about the original message, it just looks like a new message to it (it gets a new queue-id etc). – Gryphius Aug 23 '11 at 07:32