python3发送自定义html样式邮件

可以调整更好看点的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 + '<tr>'
        for i in item:
            df_html = df_html + '<td>' + str(i) + '</td>'
        df_html = df_html + '</tr>'
    df_html1 = df_html1 + '<tr>'
    for item1 in info:
        df_html1 = df_html1 + '<td>' + item1 + '</td>'
    df_html1 = df_html1 + '</tr>'
    head = \
        """
        <head>
            <meta charset="utf-8">
            <title>{title}</title>
            <style>
                .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;}}
            </style>
        </head>
        """.format(title=title)

    body = \
        """
        <body>
            <h2 class="h31">{title}</h2>
            <p align="center">xxx内容{daytime}</p>
            <p align="center">机器IP:xxxxx</p>
            <table class="table1">
            <!---表头-->
            <tr>
            <th>内存:used/total</th>
            <th>磁盘/:used/total</th>
            </tr>
            <!---表内容-->
            {df_html1}
            </table>
            <table class="table1">
            <!---表头-->
            <tr>
            <th>类型</th>
            <th>文件名</th>
            <th>SIZE</th>
            <th>INFO</th>
            </tr>
            <!---表内容-->
            {df_html}
            </table>
        </body>
        """.format(title=title, df_html=df_html, df_html1=df_html1,daytime=daytime)
    html_msg = "<html>" + head + body + "</html>"

    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)

 

posted on 2020-09-12 12:00  一个梦想自由的人  阅读(752)  评论(0编辑  收藏  举报