简体   繁体   English

python-从linux上的python中的postfix读取电子邮件

[英]python - read email from postfix in python on linux

I'm very new to postfix and python. 我对postfix和python很陌生。 I've setup postfix on Ubuntu and have configured the main.cf with mailbox_command = /home/someuser/test.py 我已经在Ubuntu上安装Postfix和配置了main.cfmailbox_command = /home/someuser/test.py

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> 我原本希望将电子邮件的内容插入到字段中,但是最终我得到了<open file '<stdin>', mode 'r' at 0x7f018b3b40c0>

How do I get the raw email string from what seems to be that memory address? 如何从该内存地址中获取原始电子邮件字符串?

sys.stdin is an object of type TextIOWrapper and cur.execute is expecting a string. sys.stdin是类型的对象TextIOWrappercur.execute期待一个字符串。 You need to instruct sys.stdin to read input and return the string representing it. 您需要指示sys.stdin读取输入并返回代表它的字符串。 Use either readline or readlines depending on what you're trying to do. 使用readlinereadlines取决于您要执行的操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM