文章目录
- Python使用smtplib模块发送电子邮件
- 一、配置邮件正文模块
- 1. 使用MIMEText对象(构造普通文本、超文本、文本附件)
- 2. 使用MIMEImage对象(构造图片附件)
- 3. 使用MIMEMultipart对象(一个可包含多个对象的容器)
- 二、发送邮件模块
- smtplib模块(使用QQ邮箱例子)
Python使用smtplib模块发送电子邮件
一、配置邮件正文模块
1. 使用MIMEText对象(构造普通文本、超文本、文本附件)
构造普通文本内容
from email.mime.text import MIMEText
text_info = 'Hello Evan'
text_obj = MIMEText(text_info, 'plain', 'utf-8') # 第一个参数文本信息,第二个参数文本格式,第三个参数编码格式
构造超文本内容
from email.mime.text import MIMEText
html_info = '<p>Hello Evan</p>'
html_obj = MIMEText(html_info, 'html', 'utf-8') # 第一个参数超文本信息,第二个参数超文本格式,第三个参数编码格式
构造base64数据流附件
import os
from email.header import Header
from email.mime.text import MIMEText
file_path = 'example.txt'
txt_file = open(file_path, 'rb')
file_obj = MIMEText(txt_file.read(), 'base64', 'utf-8')
txt_file.close()
file_obj["Content-Type"] = 'application/octet-stream'
file_obj["Content-Disposition"] = 'attachment;filename="{}"'.format(Header(os.path.basename(file_path).encode('utf-8'), 'utf-8'))
2. 使用MIMEImage对象(构造图片附件)
构造图片附件
import os
from email.header import Header
from email.mime.image import MIMEImage
file_path = 'example.txt'
image_file = open(file_path, 'rb')
image_obj = MIMEImage(image_file.read())
image_file.close()
image_obj["Content-Type"] = 'application/octet-stream'
image_obj["Content-Disposition"] = 'attachment;filename="{}"'.format(Header(os.path.basename(file_path).encode('utf-8'), 'utf-8'))
3. 使用MIMEMultipart对象(一个可包含多个对象的容器)
MIMEMultipart实例化有三个可选类型,分别为:
- mixed: 邮件包含附件,图片,文本等(
默认值
,选择mixed类型什么都可以发) - alternative: 邮件正文中包含纯文本正文(text/plain)和超文本正文(text/html)
- related: 邮件正文中包含图片,声音等内嵌资源
使用 mixed 例子:
import os
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
multipart_obj = MIMEMultipart('mixed') # 使用容器对象
multipart_obj['subject'] = Header('Hello boys', 'utf-8') # 配置邮件主题
multipart_obj['From'] = '666@qq.com' # 配置发件方邮箱
multipart_obj['To'] = '777@qq.com' # 配置收件方邮箱
html_info = '<p>Hello Evan</p>'
html_obj = MIMEText(html_info, 'html', 'utf-8')
multipart_obj.attach(html_obj) # 添加构造好的对象
file_path = 'example.txt'
txt_file = open(file_path, 'rb')
file_obj = MIMEText(txt_file.read(), 'base64', 'utf-8')
txt_file.close()
file_obj["Content-Type"] = 'application/octet-stream'
file_obj["Content-Disposition"] = 'attachment;filename="{}"'.format(Header(os.path.basename(file_path).encode('utf-8'), 'utf-8'))
multipart_obj.attach(file_obj) # 添加构造好的对象
二、发送邮件模块
smtplib模块(使用QQ邮箱例子)
完整例子:
import os
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
__author__ = 'Evan'
sender_mail = '666@qq.com' # 发件方邮箱
receiver_mail = '777@qq.com' # 收件方邮箱
# 发件方邮箱提供的第三方登录授权码(如QQ邮箱:设置-账户-开启服务-开启POP3/SMTP服务,然后点击生成授权码)
auth_code = 'abc123'
# 构造邮件正文
multipart_obj = MIMEMultipart('mixed') # 使用容器对象
multipart_obj['subject'] = Header('Hello boys', 'utf-8') # 配置邮件主题
multipart_obj['From'] = sender_mail # 配置发件方邮箱
multipart_obj['To'] = receiver_mail # 配置收件方邮箱
html_info = '<p>Hello Evan</p>'
html_obj = MIMEText(html_info, 'html', 'utf-8')
multipart_obj.attach(html_obj) # 添加构造好的对象
file_path = 'example.txt'
txt_file = open(file_path, 'rb')
file_obj = MIMEText(txt_file.read(), 'base64', 'utf-8')
txt_file.close()
file_obj["Content-Type"] = 'application/octet-stream'
file_obj["Content-Disposition"] = 'attachment;filename="{}"'.format(Header(os.path.basename(file_path).encode('utf-8'), 'utf-8'))
multipart_obj.attach(file_obj) # 添加构造好的对象
# 使用smtplib模块
sftp_obj = smtplib.SMTP('smtp.qq.com', 25) # 实例化SMTP对象,端口号为25
sftp_obj.login(sender_mail, auth_code) # 使用发件方邮箱账号和第三方登录授权码模拟登录
sftp_obj.sendmail(sender_mail, receiver_mail, multipart_obj.as_string()) # 发送邮件
sftp_obj.quit() # 退出登陆