Python 发送html格式邮件
技术积累 Python

Pyhon脚本发送"磁盘使用"html格式邮件;我这里使用的企业邮箱用的25端口

#!/usr/bin/python
# -*- coding: utf-8 -*-
import smtplib, time, os
from email.mime.text import MIMEText
from email.header import Header

def send_mail_html(file):
    sender = 'admin@jinchuang.org' #发件人
    receiver = 'gaojing@jinchuang.org' #收件人
    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  #获取当前时间
    subject = '博客磁盘使用报警_' + t  #邮件主题
    smtpserver = 'smtp.exmail.qq.com' #发送服务器地址
    username = 'admin@jinchuang.org' #用户名
    password = 'passwd' #密码

    f = open(file, 'rb')
    mail_body = f.read()
    f.close()


    msg = MIMEText(mail_body, _subtype='html', _charset='utf-8')
    msg['Subject'] = Header(subject, 'utf-8')
    msg['From'] = sender
    msg['To'] = receiver

    try:
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver)
        smtp.login(username, password)
        smtp.sendmail(sender, receiver, msg.as_string())
    except:
        print("邮件发送失败!")
    else:
        print("邮件发送成功!")
    finally:
        smtp.quit()

file = '/tmp/df.html' #html文件
send_mail_html(file)

html表格模板

#!/bin/bash
ip=`ifconfig |grep -v 127 |grep inet|awk '{print $2}'`
a=`df -hT|grep -w "/"|awk '{print $1}'`
b=`df -hT|grep -w "/"|awk '{print $2}'`
c=`df -hT|grep -w "/"|awk '{print $3}'`
d=`df -hT|grep -w "/"|awk '{print $4}'`
e=`df -hT|grep -w "/"|awk '{print $5}'`
f=`df -hT|grep -w "/"|awk '{print $6}'`
g=`df -hT|grep -w "/"|awk '{print $7}'`

html="<html>
<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
<head>
<style type=\"text/css\">
table{margin-top:5%;width:500px}
table.gridtable {
    font-family: verdana,arial,sans-serif;
    font-size:14px;
    color:#333333;
    border-width: 1px;
    border-color: #666666;
    border-collapse: collapse;
}
table.gridtable th {
    border-width: 1px;
    padding: 8px;
    background-color: #008eff;
    color:#fff;
}
table.gridtable td {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #afafaf;
    background-color: #ffffff;
}
table tr:first-child td:first-child, table tr:first-child th:first-child{
  border-top-left-radius: 5px;
}
table tr:first-child td:last-child, table tr:first-child th:last-child{
  border-top-right-radius: 5px;
}
</style>
</head>
<body>
<table class=\"gridtable\" align=center>
<tr>
    <th colspan="7">告警主机:192.168.11.1</th>
</tr>
<tr>
    <td>文件系统</td><td>类型</td><td>总共</td><td>已用</td><td>可用</td><td>使用率</td><td>挂载点</td>
</tr>
<tr>
    <td>$a</td><td>$b</td><td>$c</td><td>$d</td><td>$e</td><td style=\"color:red;font-weight:bold\">$f</td><td>$g</td>
</tr>
</table>

</body>
</html>"
echo -e "$html" >/tmp/df.html

如果需要使用ssl协议,修改2个地方

smtpserver = 'smtp.exmail.qq.com:465' #加上465端口
smtp = smtplib.SMTP_SSL() #加上ssl

收到的邮件表格内容:

Python 发送html格式邮件


本文最后记录时间 2024-04-10
文章链接地址:
https://me.jinchuang.org/archives/272.html
本站文章除注明[转载|引用|来源],均为本站原创内容,转载前请注明出处
使用腾讯云CDN的SDK 用脚本去刷新URL缓存
pip 安装错误 Command "python setup.py egg_info" failed with error code
Linux命令行界面使用Python+Selenium+Chrome对网页截图
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless

wkhtmltopdf - html转pdf/图片命令行工具

Mysql8.0.12安装

留言

顶部