邮件发送测试报告

前置条件:开通QQ邮箱第三方登录,并拿到密码;


步骤1:编写测试代码,先发送一个文本的邮件
在sample文件中编写线性代码:

步骤2:编写一个带附件的邮件


代码示例:
import os
import smtplib
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders
# 先准备附件
current_path = os.path.dirname(__file__)
smtp_file_path = os.path.join(current_path,'..','reports/禅道UI自动化测试V1.0.zip')
# 登录账号密码,发件人和收件人,邮件标题,邮件内容,邮件服务器域名
# 开通QQ邮箱第三方登录,并拿到密码
smtp_server = 'smtp.qq.com'
smtp_sender = '2798413941@qq.com'
smtp_senderpassword = 'gmyiulsymlzvdcdd'
smtp_receiver = 'zz1196999489@163.com'
smtp_cc = '1196999489@qq.com'
smtp_subject = '禅道自动化测试报告'
smtp_body = '来自禅道自动化测试的结果'
smtp_file = smtp_file_path
# 发压缩包的邮件
msg = MIMEMultipart()
with open(smtp_file,'rb') as f:
mime = MIMEBase('zip','zip',filname=smtp_file.split('/')[-1])
mime.add_header('Content-Disposition', 'attachment',
filename=('gb2312', '',smtp_file.split('/')[-1]))
mime.add_header('Content-ID', '<0>')
mime.add_header('X-Attachment-Id', '0')
mime.set_payload(f.read())
encoders.encode_base64(mime)
msg.attach(mime)
msg.attach(MIMEText(smtp_body,'html','utf-8'))
msg['from'] = smtp_sender
msg['to'] = smtp_receiver
msg['Cc'] = smtp_cc
msg['subject'] = smtp_subject
# # 邮件文本消息
# msg = MIMEText(smtp_body,'html','utf-8') # 邮件消息对象
# msg['from'] = smtp_sender
# msg['to'] = smtp_receiver
# msg['Cc'] = smtp_cc
# msg['subject'] = smtp_subject
# 发送邮件
smtp = smtplib.SMTP()
smtp.connect(smtp_server)
smtp.login(user=smtp_sender,password=smtp_senderpassword)
smtp.sendm

最低0.47元/天 解锁文章
302

被折叠的 条评论
为什么被折叠?



