一、smtplib库
是SMTP 简单邮件传输协议的操作模块,发送邮件时起到服务器之间的通信作用。
发送一封邮件分为:设置服务器信息-编写邮件主体信息-进行登录发送
发送一个文本邮件
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 设置服务器信息
mail_host = "smtp.qq.com"
mail_user = "372109913"
mail_pass = "输入授权码"
sender = '372109913@qq.com'
receivers = ['smilepassed@163.com']
# 编写邮件主体
message = MIMEText('这是一个测试内容','plain','UTF-8')
message['Subject'] = '一封来自测试的信' # 邮件标题
message['From'] = Header("Jie的QQ邮箱","utf-8") # 邮件主体中发送者名称
message['To'] = Header("Jie的163邮箱","utf-8") # 邮件主体中接收者名称
# 进行登录发送
try:
smtpobj = smtplib.SMTP()
smtpobj.connect(mail_host,25)
smtpobj.login(mail_user,mail_pass)
smtpobj.sendmail(sender,receivers,message.as_string())
print('success')
except Exception as e:
print(f'error: {e}')
finally:
smtpobj.quit()
1、设置服务器信息
mail_host : SMTP 服务器主机;
mail_user:邮箱登录账号;
mail_pass:邮箱登录授权码;
sender:发送邮件的邮箱地址;
receivers:要接收邮件的地址,是一个list,即可实现群发邮件;
2、编写邮件主体
MIMEText('这是一个测试内容','html','UTF-8') :邮件主体内容三个参数('文本内容','文本格式','文本编码');
message['Subject'] :邮件标题;
message['From'] :邮件主体中发送者名称;
message['To']:邮件主体中接收者名称;
3、进行登录发送
smtplib.SMTP( ):创建SMTP对象;
通过connect( )连接SMTP服务器,参数(服务器地址,端口号);
通过login( )进行登录,参数(用户名,登录授权码);
sendmail 进行发送,参数(发送者邮箱,接收邮箱列表,邮件主体)
在发送方邮箱设置中需要,打开POP3,也是在这里获取登录的“授权码”
发送一个带附件的邮件
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 设置服务器信息
mail_host = "smtp.qq.com"
mail_user = "372109913"
mail_pass = "输入授权码"
sender = '372109913@qq.com'
receivers = ['smilepassed@163.com']
# 编写邮件主体
message = MIMEMultipart() # 生成包含多个部分的邮件主体对象
message['Subject'] = '一封来自测试的信' # 邮件主题
message['From'] = Header("Jie的QQ邮箱","utf-8") # 邮件主体中发送者名称
message['To'] = Header("Jie的163邮箱","utf-8") # 邮件主体中接收者名称
body = MIMEText('正文文本内容','plain','utf-8')
message.attach(body)
att_body = open(file_path, 'rb') # 以二进制的格式打开附件
att = MIMEApplication(att_body.read()) # 导入附件
att_body.close()
att.add_header('Content-Disposition','attachment',filename='allure测试报告.zip')
message.attach(att)
# 进行登录发送
try:
smtpobj = smtplib.SMTP()
smtpobj.connect(mail_host,25)
smtpobj.login(mail_user,mail_pass)
smtpobj.sendmail(sender,receivers,message.as_string())
smtpobj.quit()
print('success')
except Exception as e:
print(f'error: {e}')
1、创建 message = MIMEMultipart() 对象用于构造带附件的邮件主体;
2、通过 body = MIMEText('正文文本内容','plain','utf-8') 定义邮件主体的文本内容;
3、message.attach(body) 把文本内容添加到邮件主体中;
4、att =MIMEApplication(open(file_path, 'rb').read()) 把要上传的附件以二进制打开读取后作为作为附件导入
5、att.add_header('Content-Disposition','attachment',filename='allure测试报告.zip') 通过filename定义可下载的附件名;
6、message.attach(att) 把定义好的附件添加到message主体中;
邮件发送模块的封装
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
class SendEmail:
# 初始化服务器信息
def __init__(self,mail_host, mail_user, mail_pass, sender, receives):
self.mail_host = mail_host
self.mail_user = mail_user
self.mail_pass = mail_pass
self.sender = sender
self.receivers = receives
# 以文本的形式发送邮件
def make_email_by_text(self,context, subject, from_address, to_address):
message = MIMEText(context, 'plain', 'UTF-8')
message['Subject'] = subject # 邮件标题
message['From'] = Header(from_address, "utf-8") # 邮件主体中发送者名称
message['To'] = Header(to_address, "utf-8") # 邮件主体中接收者名称
self.send_email(message)
# 以文本和附件的形式发送邮件
def make_email_by_att(self,file_path,subject, from_address, to_address):
message = MIMEMultipart()
message['Subject'] = subject # 邮件标题
message['From'] = Header(from_address, "utf-8") # 邮件主体中发送者名称
message['To'] = Header(to_address, "utf-8") # 邮件主体中接收者名称
body = MIMEText('dear all: 测试报告详情请查看附件','plain','utf-8')
message.attach(body)
att_body = open(file_path, 'rb') # 以二进制的格式打开附件
att = MIMEApplication(att_body.read()) # 导入附件
att_body.close()
att.add_header('Content-Disposition','attachment',filename='allure测试报告.pdf') # 添加附件名称
message.attach(att)
self.send_email(message)
# 登录并进行发送
def send_email(self,message):
# 进行登录发送
try:
smtpobj = smtplib.SMTP()
smtpobj.connect(self.mail_host,25)
smtpobj.login(self.mail_user,self.mail_pass)
smtpobj.sendmail(self.sender,self.receivers,message.as_string())
smtpobj.quit()
print('success')
except Exception as e:
print(f'error: {e}')
raise e
if __name__ == '__main__':
pass