python - read email from postfix in python on linux
<p>I'm very new to postfix and python. I've setup postfix on Ubuntu and have configured the <code>main.cf</code> with <code>mailbox_command = /home/someuser/test.py</code></p>

<p>test.py:</p>

<pre><code>#!/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()
</code></pre>

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

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

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

<pre><code>cur.execute(sql, (sys.stdin.readlines(),))
</code></pre>

python linux postfix