通过python通过电子邮件发送mysql select语句 [英] emailing mysql select statement by python
本文介绍了通过python通过电子邮件发送mysql select语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在电子邮件中插入选择语句。
表有10行和3列,但我无法执行。下面的代码和错误。
I am trying to insert a select statement into an email. The table has 10 rows and 3 columns but I'm not able to do it. Code and errors below.
#!/usr/bin/env python
import csv
import MySQLdb
import pysftp
import smtplib
import sys
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
conn = MySQLdb.connect(XXXX)
#Email Part1 Msg
part1 = """
To: To Person <>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test
"""
sender = ''
receivers =
x = conn.cursor()
query1= ("""select * from table""")
smtpObj=smtplib.SMTP('smtp', 25)
smtpObj.sendmail(sender, receivers, x.execute(query1))
这是错误消息:
Traceback (most recent call last):
File "test3.py", line 33, in <module>
smtpObj.sendmail(sender, receivers, x.execute(query1))
File "/usr/lib/python2.7/smtplib.py", line 729, in sendmail
esmtp_opts.append("size=%d" % len(msg))
TypeError: object of type 'long' has no len(
推荐答案
您必须将字符串作为第三个参数传递给 smtpObj.sendmail()
。在执行 x.execute(query1)
之后,必须获取结果并将它们转换为字符串。
You have to pass in a string as the 3rd argument to smtpObj.sendmail()
. After you do x.execute(query1)
, you have to fetch the results and turn them into a string.
x.execute(query1)
msg = ""
for row in x.fetchall():
msg += str(row) # or however you turn a row into a string
smtpObj.sendmail(sender, receivers, msg)
这篇关于通过python通过电子邮件发送mysql select语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文