Python 发送测试报告邮件
时间: 2023-07-02 12:10:51 浏览: 49
发送测试报告邮件可以使用 Python 的 `smtplib` 模块和 `email` 模块。以下是一个示例代码,可以将测试报告以附件的形式发送到指定邮件地址:
```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
# 邮件配置
smtp_server = 'smtp.163.com'
smtp_port = 465
smtp_user = 'your_email@163.com'
smtp_password = 'your_password'
# 收件人和发件人信息
to_address = 'to_email@qq.com'
from_address = 'your_email@163.com'
# 邮件主题和正文
subject = '测试报告'
content = '请查收附件中的测试报告'
# 测试报告文件路径
report_file = 'test_report.html'
# 构造邮件内容
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = from_address
msg['To'] = to_address
# 添加正文
text = MIMEText(content)
msg.attach(text)
# 添加附件
with open(report_file, 'rb') as f:
att = MIMEApplication(f.read())
att.add_header('Content-Disposition', 'attachment', filename=report_file)
msg.attach(att)
# 发送邮件
try:
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
server.login(smtp_user, smtp_password)
server.sendmail(from_address, to_address, msg.as_string())
server.quit()
print('邮件发送成功')
except Exception as e:
print('邮件发送失败:', e)
```
在代码中,需要根据实际情况修改邮件配置、收件人和发件人信息、邮件主题、正文以及测试报告文件路径。将代码中的 `your_email@163.com` 和 `your_password` 修改为实际的发件人邮箱和密码。