Python : Postfix stdin
Asked Answered
M

2

8

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 ?

Meda answered 29/11, 2011 at 14:1 Comment(0)
O
7

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

# send to [email protected]
emailname: "|/path/to/script.py"

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()

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.

Othelia answered 29/11, 2011 at 14:6 Comment(4)
problem now is, I want this to happen for every email, not only the emaildomains that are present. Is there a wirldcard for all domains ?Meda
@LucasKauffman That's a postfix configuration question. Probably best to ask on serverfault.com, as I don't have an answer.Othelia
Also, you may need to invoke the python interpreter like this: emailname: "|python path/to/script.py". At least that's what we had to do on othello.Capriola
@Capriola lulz.. Hopefully you aren't troubleshooting my old codeOthelia
N
12

Rather than calling sys.stdin.readlines() then looping and passing the lines to email.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:

import email
msg = email.message_from_file(sys.stdin)
Nucleotidase answered 17/12, 2013 at 19:48 Comment(0)
O
7

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

# send to [email protected]
emailname: "|/path/to/script.py"

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()

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.

Othelia answered 29/11, 2011 at 14:6 Comment(4)
problem now is, I want this to happen for every email, not only the emaildomains that are present. Is there a wirldcard for all domains ?Meda
@LucasKauffman That's a postfix configuration question. Probably best to ask on serverfault.com, as I don't have an answer.Othelia
Also, you may need to invoke the python interpreter like this: emailname: "|python path/to/script.py". At least that's what we had to do on othello.Capriola
@Capriola lulz.. Hopefully you aren't troubleshooting my old codeOthelia

© 2022 - 2024 — McMap. All rights reserved.