打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python发送html邮件与文本邮件
代码:
#-*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header 
import email.utils
import os,sys,datetime
import reportStr
import pymysql
import codecs
def sendMail(server, fro,to,subject,content,contentType='txt',files=[]):
    assert type(to) == list
    assert type(files) == list
   
    main_msg = MIMEMultipart()
    main_msg['From'] = fro
    main_msg['To'] = ";".join(to)
    main_msg['Subject'] = "%s"%(subject)
    main_msg['Date'] = email.utils.formatdate()
  
    if contentType=="html":
        text_msg = MIMEText("%s"%content,_subtype='html',_charset='utf-8')
        main_msg.attach(text_msg)
    elif contentType == 'plain':
        text_msg = MIMEText("%s"%content,_subtype='plain',_charset='utf-8')
        main_msg.attach(text_msg)
    elif contentType == 'image':
        msgRoot.attach(content)
   
    smtp = smtplib.SMTP()
    smtp.connect(server) 
    smtp.login('deepmao', 'am0443AMM') 
    smtp.sendmail(From, To, main_msg.as_string())
    smtp.quit()
def fillSvrTableTh(conStr,thBegin,thEnd):
    thStr = ''
    thStr = thStr + thBegin
    thStr = thStr + conStr + thEnd + '\r\n'
    return thStr
def fillSvrTableTR(trData,thnum):
    svrStr = '<tr style="height: 26px; overflow: hidden;">\n'
    thBegin = '<td style="overflow: hidden; padding-left: 25px; text-overflow: ellipsis; border-bottom: 1px solid #efefef;">'
    thEnd = '</td>'
    for jj in range(thnum):
        svrStr = svrStr + fillSvrTableTh(trData[jj],thBegin,thEnd)
    svrStr = svrStr + '</tr>'
    return svrStr
   
db_user = 'root'
db_pwd = '123457'
db_host = '10.1.163.33'
db_database = 'shareMM'
   
if __name__ == '__main__':
   
    #version
    vDate = datetime.datetime.now() + datetime.timedelta(days=-1)
    vDateStr = vDate.strftime('%y-%m-%d')
    MhoVersion = 'MHFC_DevMain_%s'%(vDateStr)   #1
    dReport = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')  #2
    trDatas = []
   
    conn=pymysql.connect(host=db_host,user=db_user,passwd=db_pwd,db=db_database,port=3309,charset='utf8')
    cur = conn.cursor()
    cur.execute('select * from townsvr_mem where info_time="2014-10-21"')
    dTownMem=cur.fetchall() 
    cur.execute('select * from townsvr_cpu where info_time="2014-10-21"')
    dTownCpu=cur.fetchall() 
    cur.execute('select * from townsvr_fps where info_time="2014-10-21"')
    dTownFps=cur.fetchall()
   
    cur.execute('select * from worldsvr_mem where info_time="2014-10-21"')
    dWorldMem=cur.fetchall()
    cur.execute('select * from worldsvr_cpu where info_time="2014-10-21"')
    dWorldCpu=cur.fetchall()
    cur.execute('select * from worldsvr_fps where info_time="2014-10-21"')
    dWorldFps=cur.fetchall()
   
    tmpList = []
    tmpList.append('350人城镇随机行走')
    tmpList.append('townsvr')
    fmem = int(dTownMem[0][5]) / 1000
    strmem = '%.2f M'%(fmem)
    tmpList.append(strmem)
    fmem = int(dTownMem[0][3]) / 1000
    strmem = '%.2f M'%(fmem)
    tmpList.append(strmem)
    tmpList.append(dTownFps[0][5])
    tmpList.append(dTownFps[0][3])
    tmpList.append(dTownCpu[0][5])
    tmpList.append(dTownCpu[0][3])
    trDatas.append(tmpList)
   
    tmpList = []
    tmpList.append('350人城镇随机行走')
    tmpList.append('worldsvr')
    fmem = int(dWorldMem[0][5]) / 1000
    strmem = '%.2f M'%(fmem)
    tmpList.append(strmem)
    fmem = int(dWorldMem[0][3]) / 1000
    strmem = '%.2f M'%(fmem)
    tmpList.append(strmem)
    tmpList.append(dWorldFps[0][5])
    tmpList.append(dWorldFps[0][3])
    tmpList.append(dWorldCpu[0][5])
    tmpList.append(dWorldCpu[0][3])
    trDatas.append(tmpList)
   
    tblStr = ''
    for ii in range(len(trDatas)):
        tblStr = tblStr + fillSvrTableTR(trDatas[ii],8)          #3
   
    dtime = datetime.datetime.now().strftime('%y-%m-%d')
    #456
    svrMemSrc = 'http://10.1.163.33:8180/MHOProject/mhoperformance/chart/%s/townworldmem.png'%(dtime)
    svrFpsSrc = 'http://10.1.163.33:8180/MHOProject/mhoperformance/chart/%s/townworldfps.png'%(dtime)
    svrCpuSrc = 'http://10.1.163.33:8180/MHOProject/mhoperformance/chart/%s/townworldcpu.png'%(dtime)
   
    print(svrMemSrc)
   
    html = reportStr.html_content%(MhoVersion,dReport,tblStr,svrMemSrc,svrFpsSrc,svrFpsSrc)
    # print(html)
    # bb = html.decode('gb2312').encode('utf-8')
    f = codecs.open('%s.html'%(dtime),'w','utf-8')
    f.write(html)
    f.close()
       
   
    # mail info
    smtpsvr = ('tsmtp.tencent.com')#('172.25.7.178')
    From = "deepmao@tencent.com"
    To = ["deepmao@tencent.com"]
    Subject = "怪物猎人服务器常态化性能测试"
   
    main_msg = MIMEMultipart()
    main_msg['From'] = From
    main_msg['To'] = ";".join(To)
    main_msg['Subject'] = "%s"%(Subject)
    main_msg['Date'] = email.utils.formatdate()
   
    # 发送文本邮件
    # htmlText = 'just python text mail test'
    # text_msg = MIMEText("%s"%htmlText,_subtype='html',_charset='utf-8')
    # main_msg.attach(text_msg)
   
    # 发送html邮件
    # msgText = MIMEText(html,'html','utf-8')
    # main_msg.attach(msgText)
   
  
   
    # smtp = smtplib.SMTP()
    # smtp.connect(smtpsvr) 
    # smtp.login('deepmao', 'am0443AMM') 
    # smtp.sendmail(From, To, main_msg.as_string(unixfrom=True))
    # smtp.quit()
 
 # sendMail(smtpsvr,From,To,Subject,htmlText,'html')
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Python 发送邮件V2.0---学习笔记
python 发送邮件实例
用Python看盘,出现交易信号邮件提醒
[Python运维]使用Python发送邮件
Python3 SMTP发送邮件
Zmail-简单易用的python邮件模块
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服