python发送html邮件样式变了_python发送html邮件与文本邮件

该博客展示了如何使用Python发送HTML邮件,并保持邮件样式不变。通过导入smtplib, email等模块,创建MIMEMultipart邮件容器,设置邮件头信息,将HTML内容作为邮件正文,最后使用SMTP发送邮件。示例代码中还包括了数据库查询和数据处理,用于生成邮件内容。" 125725907,14793740,选择餐饮点餐小程序源代码的十大要点,"['小程序', 'PHP', '餐饮', '在线点餐', '用户体验', '集成']
摘要由CSDN通过智能技术生成

代码:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值