1. 程式人生 > >python 發送html格式郵件到qq

python 發送html格式郵件到qq

attach spa end wid %d width 問題 get 發送html

# -*- 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 
= [email protected] mail_pwd = XXXXX ssl_pwd = "XXXXXX" mail_to = [[email protected]] 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)

python 發送html格式郵件到qq