iT邦幫忙

0

定義 name error問題

  • 分享至 

  • xImage

希望將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')

看更多先前的討論...收起先前的討論...
引用包 跟 詳細錯誤訊息呢
print(result.to_html())
有印出訊息嗎?
result.to_html()有可能會是None
dadada iT邦新手 5 級 ‧ 2022-08-04 14:27:12 檢舉
您好 錯誤訊息是 NamEerror: name 'result' is not defined
dadada iT邦新手 5 級 ‧ 2022-08-04 14:29:34 檢舉
在最後沒還沒加上send_mail('title',result.to_html(),'1234@gmail.com')前有印出訊息,訊息就是原本的table轉成html的型態
我的意思是說 請問 print(result.to_html())
有正確執行道嗎?
也就是說有沒有PRINT出東西
dadada iT邦新手 5 級 ‧ 2022-08-04 14:33:38 檢舉
有的,print(result.to_html())有執行到,印出的是table轉html型態的資料
你在這行result=pd.read_sql_query(query_string,db_eng)
上面加個result = None
試試看
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答