I want to make postfix send all emails to a python script that will scan the emails.
However, how do I pipe the output from postfix to python ?
What is the stdin for Python ?
Can you give a code example ?
I want to make postfix send all emails to a python script that will scan the emails.
However, how do I pipe the output from postfix to python ?
What is the stdin for Python ?
Can you give a code example ?
Rather than calling
sys.stdin.readlines()
then looping and passing the lines toemail.FeedParser.FeedParser().feed()
as suggested by Michael, you should instead pass the file object directly to the email parser.The standard library provides a conveinience function,
email.message_from_file(fp)
, for this purpose. Thus your code becomes much simpler:To push mail from postfix to a python script, add a line like this to your postfix alias file:
The python
email.FeedParser
module can construct an object representing a MIME email message from stdin, by doing something like this:From here, you need to iterate over the MIME parts of
msg
and act on them accordingly. Refer to the documentation onemail.Message
objects for the methods you'll need. For exampleemail.Message.get("Header")
returns the header value ofHeader
.