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)