python发送html邮件样式变了_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 = '

\n'

thBegin = '

'

thEnd = '

'

for jj in range(thnum):

svrStr = svrStr + fillSvrTableTh(trData[jj],thBegin,thEnd)

svrStr = svrStr + '

'

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')

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页