代码实现发邮件
Python的smtplib
提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。
这里以QQ邮箱为例,首先要拿到授权码,这里登录你的QQ邮箱,在设置中,选择账号选项,下拉到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务项,获取授权码。
以QQ邮箱为例,先把需要开启的任务弄好!!!
需要发其他邮箱请看博客https://www.cnblogs.com/Neeo/articles/11199127.html
发送普通文本邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 第三方 SMTP 服务
mail_host = "smtp.qq.com" # 设置服务器
mail_user = "1206180814@qq.com" # 用户名
mail_pass = "dfpcglacrjbybafa" # 获取授权码
sender = '1206180814@qq.com' # 发件人账号
receivers = ['1206180814@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
send_content = 'Python 邮件发送测试...'
message = MIMEText(send_content, 'plain', 'utf-8') # 第一个参数为邮件内容,第二个设置文本格式,第三个设置编码
message['From'] = Header("我是发件人", 'utf-8') # 发件人
message['To'] = Header("我是收件人", 'utf-8') # 收件人
subject = '邮件大标题'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
运行结果如下:
发送HTML格式邮件
授权码都不变,只需将MIMEText
的第二个参数的文本类型改一下即可:
改成html格式,相当于发的邮件内容是html页面的样子,send_contet= html前端的代码
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 第三方 SMTP 服务
mail_host = "smtp.qq.com" # 设置服务器
mail_user = "1206180814@qq.com" # 用户名
mail_pass = "dfpcglacrjbybafa" # 口令
sender = '1206180814@qq.com'
receivers = ['1206180814@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
send_content = """
<h1>天不生我李淳罡</h1>
<h1>剑道万古如长夜</h1>
<p>小二上酒</p>
<img src="https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=858168512,2130327819&fm=85&s=2E4020DF1CD035FBDC9D940A0300F0F3">
<div>阅读请 <a href="https://www.37zw.net/0/761/">点我,点我</a></div>
"""
message = MIMEText(send_content, 'html', 'utf-8') # 第一个参数为邮件内容
message['From'] = Header("我是发件人", 'utf-8') # 发件人
message['To'] = Header("我是收件人", 'utf-8') # 收件人
subject = '雪中悍刀行'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
还有能发本地图片的邮件,我就不列举了,看博客https://www.cnblogs.com/Neeo/articles/11199127.html
发送带各式类型附件的邮件
发送带附件的邮件,首先要创建MIMEMultipart
实例,然后在构建附件,如果有多个附件的话,可依次构建,最后利用smtplib.smtp
发送:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
# 第三方 SMTP 服务
mail_host = "smtp.qq.com" # 设置服务器
mail_user = "1206180814@qq.com" # 用户名
mail_pass = "dfpcglacrjbybafa" # 口令
sender = '1206180814@qq.com'
receivers = ['1206180814@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
# 创建一个带附件的实例
message = MIMEMultipart()
message['From'] = Header("我是发件人", 'utf-8') # 发件人
message['To'] = Header("我是收件人", 'utf-8') # 收件人
subject = 'Python发送带附件的邮件示例'
message['Subject'] = Header(subject, 'utf-8')
# 邮件正文内容
send_content = 'hi man,你收到附件了吗?'
content_obj = MIMEText(send_content, 'plain', 'utf-8') # 第一个参数为邮件内容
message.attach(content_obj)
# 构造附件1,发送当前目录下的 t1.txt 文件
att1 = MIMEText(open('t1.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件附件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="t1.txt"'
message.attach(att1)
# 构造附件2,发送当前目录下的 t2.py 文件
att2 = MIMEText(open('t2.py', 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="t2.py"'
message.attach(att2)
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
如果要发送其他类型的,如果PDF、doc、xls、MP3格式的,我们都可以通过`MIMEApplication`来完成,`MIMEApplication`默认子类型是`application/octet-stream`,而`application/octet-stream`表明**这是个二进制文件,但愿接收方知道怎么处理!!!**,然后客户端收到这个声明后会根据文件扩展名来猜测。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
# 第三方 SMTP 服务
mail_host = "smtp.qq.com" # 设置服务器
mail_user = "1206180814@qq.com" # 用户名
mail_pass = "dfpcglacrjbybafa" # 口令
sender = '1206180814@qq.com'
receivers = ['1206180814@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
# 创建一个带附件的实例
message = MIMEMultipart()
message['From'] = Header("我是发件人", 'utf-8') # 发件人
message['To'] = Header("我是收件人", 'utf-8') # 收件人
subject = 'Python发送带附件的邮件示例'
message['Subject'] = Header(subject, 'utf-8')
# 邮件正文内容
send_content = 'hi man,你收到附件了吗?'
content_obj = MIMEText(send_content, 'plain', 'utf-8') # 第一个参数为邮件内容
message.attach(content_obj)
# 构造附件1,发送当前目录下的 t1.txt 文件
part1 = MIMEApplication(open('t1.txt', 'rb').read())
part1.add_header('Content-Disposition', 'attachment', filename='t1.txt')
message.attach(part1)
# 构造附件2,发送当前目录下的 bg.mp3 文件
part2 = MIMEApplication(open('bg.mp3', 'rb').read())
part2.add_header('Content-Disposition', 'attachment', filename='bg.mp3')
message.attach(part2)
# 构造附件3,发送当前目录下的 t3.xls 文件
part3 = MIMEApplication(open('t3.xls', 'rb').read())
part3.add_header('Content-Disposition', 'attachment', filename='t3.xls')
message.attach(part3)
# 构造附件4,发送当前目录下的 t4.doc 文件
part4 = MIMEApplication(open('t4.doc', 'rb').read())
part4.add_header('Content-Disposition', 'attachment', filename='t4.doc')
message.attach(part4)
# 构造附件5,发送当前目录下的 t5.pdf 文件
part5 = MIMEApplication(open('t5.pdf', 'rb').read())
part5.add_header('Content-Disposition', 'attachment', filename='t5.pdf')
message.attach(part5)
# 构造附件6,发送当前目录下的 img.jpg 文件
part6 = MIMEApplication(open('img.jpg', 'rb').read())
part6.add_header('Content-Disposition', 'attachment', filename='img.jpg')
message.attach(part6)
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
接收方改下扩展名就可以