根据MSSQL数据库查询结果,判断发送邮件内容
import os,pymssql
import smtplib
from email.mime.text import MIMEText
import email.mime.multipart
import email.mime.text
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
import os,time
def sendmail(conent):
day= time.strftime('%Y-%m-%d',time.localtime())
form_adder='xxx'
password='xxx'
to_adder=['xxxxx']
#smtp_server=('zxxx.com')
smtp_server=('XXX ip')
msg=email.mime.multipart.MIMEMultipart()
msg['From']=form_adder
msg['To']=','.join(to_adder)
print(msg['To'])
msg['subject']='new zabbix problems notice'
content = conent
txt = email.mime.text.MIMEText(content, 'plain', 'utf-8')
msg.attach(txt)
ret=True
try:
server=smtplib.SMTP(smtp_server,25)
server.starttls()##tls 方式验证
server.login(form_adder,password)
server.sendmail(form_adder,to_adder,msg.as_string())
server.quit()
except Exception as e:
print(e)
ret=False
return ret
server="xxx"
user="xxx"
password="xxx"
conn=pymssql.connect(server,user,password,database="soft")
cursor=conn.cursor()
cursor.execute("""select flag from flag""")
row=cursor.fetchone()
if row is None:
print("数据整理未执行完或已失败")
sendmail("数据整理未执行完成或已失败")
elif row[0].strip() == '1':
print("已成功执行完")
sendmail("已成功执行完")
else:
print("数据执行异常,值为:",row[0])
sendmail("数据整理未执行完成或已失败")
conn.close()



