send_email.py 1.39 KB
Newer Older
王志伟's avatar
王志伟 committed
1 2 3 4 5 6 7 8 9 10
##发送邮件

#coding=utf-8

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.application import MIMEApplication
import datetime

王志伟's avatar
王志伟 committed
11 12
from email.mime.multipart import MIMEMultipart

王志伟's avatar
王志伟 committed
13 14
my_sender='wangzhiwei@igengmei.com'
my_pass = 'RiKEcsHAgesCZ7yd'
王志伟's avatar
王志伟 committed
15
my_user1='wangzhiwei@igengmei.com'
王志伟's avatar
王志伟 committed
16 17
my_user2='gaoyazhe@igengmei.com'
my_user3='huangkai@igengmei.com'
王志伟's avatar
王志伟 committed
18 19
def mail():
    ret = True
王志伟's avatar
王志伟 committed
20
    pdfFile = 'hypothesis.txt'
王志伟's avatar
王志伟 committed
21 22
    pdfApart = MIMEApplication(open(pdfFile, 'rb').read())
    pdfApart.add_header('Content-Disposition', 'attachment', filename=pdfFile)
王志伟's avatar
王志伟 committed
23 24
    m = MIMEMultipart()
    m.attach(pdfApart)
王志伟's avatar
王志伟 committed
25
    m['Subject'] = '数据指标监控数据(假设检验)'
王志伟's avatar
王志伟 committed
26
    m['From'] = '王志伟<wangzhiwei@igengmei.com>'
王志伟's avatar
王志伟 committed
27

王志伟's avatar
王志伟 committed
28 29

    try:
王志伟's avatar
王志伟 committed
30 31 32 33 34
        # text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.baidu.com"
        # msg = MIMEText(text, 'plain', 'utf-8')
        # msg['From'] = formataddr(["王志伟", my_sender])
        # msg['To'] = my_user1
        # msg['Subject'] = str(datetime.date.today()) + "-esmm多目标模型训练指标统计"
王志伟's avatar
王志伟 committed
35 36
        server = smtplib.SMTP_SSL("smtp.exmail.qq.com", 465)
        server.login(my_sender, my_pass)
王志伟's avatar
王志伟 committed
37
        server.sendmail(my_sender, [my_user1], m.as_string())
王志伟's avatar
王志伟 committed
38 39 40 41 42 43 44 45 46 47
        server.quit()
    except Exception:
        ret=False
    return ret

ret=mail()
if ret:
    print("邮件发送成功")
else:
    print("邮件发送失败")