当我们在深度学习训练时,可以将训练的结果通过邮箱发送给自己,以便及时查看训练结果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| 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 import base64
def send(path): from_addr = '@qq.com' password = '' to_addr = '@qq.com' smtp_server = 'smtp.qq.com'
html_msg = """ <p>Python 邮件发送HTML格式文件测试...</p> """
msg = MIMEMultipart()
msg['To'] = Header('李四') msg['From'] = Header(f'=?utf-8?B?{base64.b64encode("python自动发送".encode()).decode()}=?= <@qq.com>') subject = 'Python SMTP 邮件测试' msg['Subject'] = Header(subject, 'utf-8') msg.attach(MIMEText(html_msg, 'html', 'utf-8'))
imageFile = path imageApart = MIMEImage(open(imageFile, 'rb').read(), imageFile.split('.')[-1]) imageApart.add_header('Content-Disposition', 'attachment', filename=imageFile) msg.attach(imageApart)
try: smtpobj = smtplib.SMTP_SSL(smtp_server) smtpobj.connect(smtp_server, 465) smtpobj.login(from_addr, password) smtpobj.sendmail(from_addr, to_addr, msg.as_string()) print("邮件发送成功") except smtplib.SMTPException: print("无法发送邮件") finally: smtpobj.quit()
|
在训练的代码中加入
1 2 3 4 5 6
| from sendemail import send
path = r'xxx' send(path)
|