K3NN3TH K3NN3TH - 1 year ago 106
Linux Question

python - read email from postfix in python on linux

I'm very new to postfix and python. I've setup postfix on Ubuntu and have configured the

main.cf
with
mailbox_command = /home/someuser/test.py


test.py:

#!/usr/bin/python
import sys, MySQLdb

email_input = sys.stdin
db = MySQLdb.connect(host="localhost",
user="user",
passwd="password",
db="test")

cur = db.cursor()

sql = "insert into postfix (value) values (%s)"
cur.execute(sql, (email_input,))
db.commit()
db.close()


I was expecting the content of the email to be inserted into the field but instead I ended up with
<open file '<stdin>', mode 'r' at 0x7f018b3b40c0>


How do I get the raw email string from what seems to be that memory address?

Answer Source

sys.stdin is an object of type TextIOWrapper and cur.execute is expecting a string. You need to instruct sys.stdin to read input and return the string representing it. Use either readline or readlines depending on what you're trying to do.

cur.execute(sql, (sys.stdin.readlines(),))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download