基本上我是尝试在电子邮件正文中发送查询结果而无法弄清楚。我测试了SQL查询的代码,它独立工作。我也可以发一封电子邮件没问题。无法将它们放在一起。然后我尝试将查询结果转换为字符串,但这不起作用。这是我的代码:
import smtplib
import pyodbc
import pandas as pd
cnxn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};"
r"DBQ=\\Location.accdb;")
query = """SELECT *;
"""
df = pd.read_sql(query, cnxn)
remail = "sendto@gmail.com"
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('from@gmail.com','PASSWORD')
smtpObj.sendmail('from@gmail.com', remail, 'Subject: Latest Query \n' + string(df))
smtpObj.quit()'
有什么想法吗?
答案 0 :(得分:0)
改变这个 -
smtpObj.sendmail('from@gmail.com', remail, 'Subject: Latest Query \n' + string(df))
到 -
smtpObj.sendmail('from@gmail.com', remail, 'Subject: Latest Query \n' + df.to_string())