【Python】利用python自动发送邮件

简介:

【Python】利用python自动发送邮件

前言
在训练网络的过程中,需要大量的时间,虽然可以预估网络训练完成时间,但蹲点看结果着实有点不太聪明的亚子。

因此,参照师兄之前发的python利用smtp自动发邮件的代码,我作了些调整,并参照网上的开源代码,整理出了加强版(可以传文件),这样训练的log还有model,或者是远程电脑上的文件都可以通过邮件即时到达接收邮件的邮箱很方便吧~~

Top
正文
废话不多说,直接上代码。

一、普通文本邮件(作通知训练结束用 😃 )

-- coding: UTF-8 --

import smtplib
from email.mime.text import MIMEText

第三方 SMTP 服务

mail_host = "smtp.163.com" # SMTP服务器
mail_user = "yourname" # 用户名
mail_pass = "xxx" # 密码(这里的密码不是登录邮箱密码,而是授权码)

sender = 'yourname@163.com' # 发件人邮箱
receivers = 'othername@163.com'] # 接收人邮箱

content = 'Python Send Mail ! 训练结束!'
title = 'Python SMTP Mail 训练结束' # 邮件主题

message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = title

try:

smtpObj = smtplib.SMTP_SSL(mail_host, 465)  # 启用SSL发信, 端口一般是465
smtpObj.login(mail_user, mail_pass)  # 登录验证
smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
print("mail has been send to {0} successfully.".format(receivers))

except smtplib.SMTPException as e:

print(e)

二、加强版附件传输邮件

-- coding: UTF-8 --

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

Files' Paths:

file1 = 'mail.py'
file2 = 'maill.py'

收邮件的地址,可以多个。

Receivers = ['receiver1@163.com','receiver2@163.com']

邮件主题:

title = 'Python SMTP 邮件(文件传输)'

模拟服务器

SMTP服务器

SMTPServer="smtp.163.com"

发邮件的地址

Sender="yourname@163.com"

发送者邮件的授权密码,去163邮箱设置里获取。并非是密码。

passwd="xxx"

创建一个带附件的实例

message = MIMEMultipart()
message['From'] = Sender
message['To'] = ",".join(Receivers)
message['Subject'] = title

邮件正文内容

message.attach(MIMEText('附件中是要传输的文件。n ', 'plain', 'utf-8'))
message.attach(MIMEText('The files you need are as followed. n ', 'plain', 'utf-8'))

构造附件1

att1 = MIMEText(open(file1, 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename={0}'.format(file1)
message.attach(att1)

构造附件2

att2 = MIMEText(open(file2, 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename={0}'.format(file2)
message.attach(att2)

try:

mailServer = smtplib.SMTP(SMTPServer, 25)  # 25为端口号(邮件),0-1024都被系统占用了
# 登录邮箱
mailServer.login(Sender, passwd)  # 需要的是,邮箱的地址和授权密码
# 发送文件
mailServer.sendmail(Sender, Receivers, message.as_string())
print("邮件发送成功")
print("Mail with {0} & {1} has been send to {2} successfully.".format(file1,file2,Receivers))

except smtplib.SMTPException as e:

print("Error: 无法发送邮件")
print(e)

Top
后话
可以把代码加到网络train.py的最后,别忘了在train.py的开头加上:

-- coding: UTF-8 --

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
然后你就可以专心忙自己的事情,网络训练结束就自动发邮件啦~

果然——Codes change the world. 😃

原文地址https://www.cnblogs.com/hatimwen/p/pythonmail.html

相关文章
|
3月前
|
存储 搜索推荐 数据安全/隐私保护
python实战讲解之使用Python批量发送个性化邮件
python实战讲解之使用Python批量发送个性化邮件
|
5月前
|
Python
python实现发送邮件demo
python实现发送邮件demo
38 1
|
5月前
|
Unix 数据安全/隐私保护 Python
python自动生成Excel表格数据并发送邮件案例
python自动生成Excel表格数据并发送邮件案例
|
6月前
|
数据安全/隐私保护 Python
python 发送邮件demo
python 发送邮件demo
|
1天前
|
监控 Python
Python监控主机是否存活,并发报警邮件
Python监控主机是否存活,并发报警邮件
|
5天前
|
数据安全/隐私保护 Python
Python Flask-Mail实现邮件发送
Python Flask-Mail实现邮件发送
|
2月前
|
存储 安全 计算机视觉
用 Python 脚本实现电脑唤醒后自动拍照 截屏并发邮件通知
用 Python 脚本实现电脑唤醒后自动拍照 截屏并发邮件通知
|
3月前
|
存储 Shell API
Python 自动化指南(繁琐工作自动化)第二版:十八、发送电子邮件和短信
Python 自动化指南(繁琐工作自动化)第二版:十八、发送电子邮件和短信
57 0
|
3月前
|
移动开发 Python HTML5
Python办公自动化【发送普通邮件、发送HTML邮件、发送附件邮件-smtplib、批量发送邮件-smtplib、发送邮件-zmail】(八)-全面详解(学习总结---从入门到深化)
Python办公自动化【发送普通邮件、发送HTML邮件、发送附件邮件-smtplib、批量发送邮件-smtplib、发送邮件-zmail】(八)-全面详解(学习总结---从入门到深化)
47 0
|
8月前
|
Python
Python发送邮件脚本
Python发送邮件脚本
37 0

热门文章

最新文章