python发送html邮件样式变了_python3发送自定义html样式邮件

#!/usr/bin/env python

# coding=utf-8

import os

import time

import datetime

import psutil

from email.mime.text import MIMEText

from email.header import Header

import smtplib

from smtplib import SMTP_SSL

bakdir = "/www/backup/"

daytime = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime("%Y-%m-%d")

def html(datalist, info):

df_html = ""

df_html1 = ""

title = ""

for item in datalist:

df_html = df_html + '

'

for i in item:

df_html = df_html + '

' + str(i) + ''

df_html = df_html + '

'

df_html1 = df_html1 + '

'

for item1 in info:

df_html1 = df_html1 + '

' + item1 + ''

df_html1 = df_html1 + '

'

head = \

"""

{title}

.table1{{min-width:500px;font-size:15px;margin:0 auto;}}

.table1 tr{{}}

.table1 th,.table1 td{{padding:0px 10px 0px 10px;border:1px solid #fff;}}

.table1 th{{color:#fff;line-height:40px;}}

.table1 td{{background-color:#ABCDEF;color:#333;line-height:34px;}}

.h31{{text-align:center;color:#06C;}}

""".format(title=title)

body = \

"""

{title}

xxx内容{daytime}

机器IP:xxxxx

内存:used/total磁盘/:used/total

{df_html1}

类型文件名SIZEINFO

{df_html}

""".format(title=title, df_html=df_html, df_html1=df_html1,daytime=daytime)

html_msg = "" + head + body + ""

return html_msg

def getresource():

memory = psutil.virtual_memory()

total_mem = str(round((float(memory.total) / 1024 / 1024 / 1024), 2)) + 'G'

used_mem = str(round((float(memory.used) / 1024 / 1024 / 1024), 2)) + 'G'

diskinfo = psutil.disk_usage('/')

total_disk = str(round((float(diskinfo.total)/1024/1024/1024), 2)) + 'G'

used_disk = str(round((float(diskinfo.used) / 1024 / 1024 / 1024), 2)) + 'G'

info = [used_mem + '/' + total_mem, used_disk + '/' + total_disk]

return info

def sendmail(html_msg):

sender = 'xxx@xxx.com'

receiver = 'xxx@xxx.com'

smtpserver = 'smtp.exmail.qq.com'

username = 'xxx@xxx.com'

password = 'xxxxxxxxxxxxxx'

mail_title = 'xxx标题'

message = MIMEText(html_msg, 'html', 'utf-8')

message['From'] = sender

message['To'] = receiver

message['Subject'] = Header(mail_title, 'utf-8')

try:

smtp = SMTP_SSL(smtpserver)

smtp.login(username, password)

smtp.sendmail(sender, receiver, message.as_string())

print("发送邮件成功!!!")

smtp.quit()

except smtplib.SMTPException:

print("发送邮件失败!!!")

if __name__ == '__main__':

# 数据列表

datalist = [['','','',''],['','','','']]

info = getresource()

html_msg = html(datalist,info)

sendmail(html_msg)

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