栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 利用 tls验证的方式 发送邮件

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 利用 tls验证的方式 发送邮件

根据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()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/313677.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号