Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : Postfix stdin

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 ?

like image 509
Lucas Kauffman Avatar asked Nov 29 '11 14:11

Lucas Kauffman


1 Answers

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)
like image 60
lfaraone Avatar answered Oct 20 '22 02:10

lfaraone