python 发送html格式邮件到qq

# -*- coding: utf-8 -*-
__author__ = 'Alon'
__date__ = '2017/8/14 18:38'
import sys

reload(sys)
sys.setdefaultencoding('utf8')
import smtplib
import time
from datetime import datetime
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

mail_host = 'smtp.qq.com'
mail_user = 'XXX@qq.com'
mail_pwd = 'XXXXX'
ssl_pwd = "XXXXXX"
mail_to = ['XXXX5@qq.com']
subject = "【重要】在研问题单统计,收到邮件请及时处理"
send_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

def get_html_msg():
    head = """<head><meta charset="utf-8">
    <style type="text/css" MEDIA=screen>
    table.gridtable {
        font-family: verdana, arial, sans-serif;
        font-size: 11px;
        color: #333333;
        border-width: 1px;
        border-color: #666666;
        border-collapse: collapse;
        width: 60%;
        table-layout: fixed;
        word-break: break-all;
    }

    table.gridtable th {
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #666666;
        background-color: #dedede;
    }

    table.gridtable td {
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #666666;
        background-color: #ffffff;
    }
</style>
    </head>"""

    p = """<p>大家好:<br>截止到 """ + send_time + """,问题单报表如下,请责任人及时处理。<br></p>"""

    body = """<body>""" + p + """
     <table class="gridtable">
    <tr>
        <th>单号</th>
        <th>描述</th>
        <th>创建人</th>
        <th>创建时间</th>
        <th>当前责任人</th>
        <th>状态</th>
    </tr>
    <tr>
        <td>50px
        </td>
        <td>50% aaaaaaa1111111111111111bbbbbbbccccccc
        </td>
        <td>50% aaaaaaabbbbbbbccccccc
        </td>
        <td>50px
        </td>
        <td>100px
        </td>
    </tr>
    <tr>
        <td>50px
        </td>
        <td>50% aaaaaaa1111111111111111bbbbbbbccccccc
        </td>
        <td>50% aaaaaaabbbbbbbccccccc
        </td>
        <td>50px
        </td>
        <td>100px
        </td>
    </tr>
</table>
     </body>"""
    html = """<html>""" + head + body + """</html>"""
    return html


def send_mail(html_msg):
    msg = MIMEMultipart()
    content = MIMEText(html_msg, 'html')
    msg.attach(content)
    msg['To'] = ";".join(mail_to)
    msg['From'] = mail_user
    msg['Subject'] = subject
    s = smtplib.SMTP_SSL(mail_host, 465)
    s.login(mail_user, ssl_pwd)
    s.sendmail(mail_user, mail_to, msg.as_string())
    s.quit()
    print "ok"


if __name__ == "__main__":
    now = datetime.now()
    html = get_html_msg()
    send_mail(html)

 

转载于:https://www.cnblogs.com/aloneblog/p/7362115.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值