希望將result.to_html檔案 包在mail裡以python方式寄出,但在跑程式時error顯示name 'result' is not defined,想請問大家要怎麼定義result ,code 如下
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
from sqlalchemy import create_engine
from urllib.parse import quote
import pymssql
import pandas as pd
class Mail():
def init(self, send_addr="555555@gmail.com"):
self.server = SMTP('relay.sing.com', 25)
self.send_addr = send_addr
def generate_msg(self, recipients,cc_list, mail_title, mail_body):
self.recipients = recipients
self.cc_list = cc_list
msg = MIMEMultipart()
msg['Subject'] = mail_title
msg['From'] = self.send_addr
msg['To'] = ", ".join(self.recipients)
msg['Cc'] = ", ".join(self.cc_list)
HtmlContent = mail_body
msgText = MIMEText(Style+HtmlContent, 'html')
msg.attach(msgText)
return msg
def send(self, msg):
self.server.sendmail(msg['From'], self.recipients+self.cc_list, msg.as_string())
self.server.quit()
def send_mail(mail_title,mail_body,users):
m = Mail()
#recipients = [user + '.com' for user in user_list]
#cc_list = ['@gmail.com']
recipients = ['.com']
cc_list = ['@gmail.com']
msg = m.generate_msg(
recipients,
cc_list,
mail_title,
mail_body)
m.send(msg)
db_eng = create_engine('mssql+pymssql://mtbtestee:%s@TAWSQLDBA77\TAMSSDBA55/MTB_TEST_EE' % quote('password'))
query_string='select* from [MTB_TEST_EE].[dbo].[PM_calendar_rawdata] where [Due_Date] >= convert (date,CURRENT_TIMESTAMP) and [Due_Date] < convert (date,CURRENT_TIMESTAMP+9)'
result=pd.read_sql_query(query_string,db_eng)
print(result.to_html())
send_mail('title',result.to_html(),'1234@gmail.com')