Python发邮件(练习)

#/usr/bin/python
# coding=utf-8
import numpy as np
import pandas as pd
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.header import Header

sender = '10@139.com'
sender_pswd = 'elyan6'
receivers = ['aozhu@163.com', '693@qq.com']
subject = '邮件主题' # 邮件主题
# attachment = ['C:\\Users\MIC\Desktop\附件\\test.txt'] # 附件
attachment = [] # 无附件
goal = '2亿(年底到达)'
mail_body = '''
<!DOCTYPE html>
<html>
<style>
.td{width:200px;overflow:hidden}
</style>
<body>
	<p><h3>您好,请查收日报。</h3></p>
	<p>
    <table border="1px" cellspacing="0px" style="border-collapse:collapse">
        <tr align="center">
            <td class="td">产品</td>
            <td class="td">指标</td>
            <td class="td">数据来源</td>
            <td class="td">年度目标</td>
            <td class="td">本月累计月活</td>
            <td class="td">昨日数据</td>
            <td class="td">昨日数据较前日增量</td>
        </tr>
        <tr align="center">
            <td>和</td>
            <td>使用用户数</td>
            <td>IOP</td>
            <td>
'''+goal+'''
            </td>
            <td>-</td>
            <td>1.92亿(当日到达)</td>
            <td>-3.11万</td>
        </tr>
    </table>
	</p>
</body>
</html>
'''

mail_object = MIMEMultipart() # 构造邮件体
mail_object['From'] = sender
mail_object['To'] = ';'.join(receivers)
mail_object['Subject'] = Header(subject, 'utf-8')
mail_object.attach(MIMEText(mail_body, 'html', 'utf-8')) # 邮件正文
for j in attachment:
    print(open(j, mode='r', encoding='utf-8').read())
    file = MIMEText(open(file=j, mode='r', encoding='utf-8').read())
    file['Content-Type'] = 'application/octet-stream'
    file['Content-Disposition'] = 'attachment; filename="%s"'%j
    mail_object.attach(file)

try:
    smtp_obj = smtplib.SMTP('smtp.139.com') # 139邮箱SMTP服务器
    smtp_obj.set_debuglevel(0)
    smtp_obj.login(sender, sender_pswd)
    smtp_obj.sendmail(sender, receivers, mail_object.as_string())
    smtp_obj.quit()
except Exception:
    print('邮件发送失败!')
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页