python - 在 linux 上从 python 中的后缀读取电子邮件

python - read email from postfix in python on linux

我对 postfix 和 python 很陌生。我已经在 Ubuntu 上设置了 postfix,并用 mailbox_command = /home/someuser/test.py

配置了 main.cf

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

我原本希望将电子邮件的内容插入到该字段中,但结果却是 <open file '<stdin>', mode 'r' at 0x7f018b3b40c0>

如何从似乎是该内存地址的位置获取原始电子邮件字符串?

sys.stdin 是类型为 TextIOWrapper 的对象,而 cur.execute 需要一个字符串。您需要指示 sys.stdin 读取输入和 return 表示它的字符串。根据您要执行的操作,使用 readlinereadlines

cur.execute(sql, (sys.stdin.readlines(),))