要将最新的测试报告以附件的形式发到指定邮箱,可以使用Python的smtplib和email模块来实现。下面是实现的完整攻略:
1. 准备工作
首先需要准备以下内容:
- SMTP邮箱服务器的地址和端口号(比如,使用腾讯企业邮箱SMTP服务器地址为smtp.exmail.qq.com,端口号为465或587)
- 发件人的邮箱地址和登录密码
- 收件人的邮箱地址
- 最新的测试报告文件的路径和文件名(比如,report.html文件)
2. 导入必要的模块
在Python中,发送邮件需要导入两个模块:smtplib和email。可以使用以下代码导入:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
3. 构造邮件内容
定义邮件主题、发件人、收件人和附件:
subject = "最新测试报告" # 邮件主题
from_email = "发件人邮箱地址" # 发件人邮箱地址
to_email = "收件人邮箱地址" # 收件人邮箱地址
attachment_path = "最新测试报告文件路径" # 最新测试报告文件路径
构造邮件主体部分:
msg = MIMEMultipart()
msg["From"] = from_email
msg["To"] = to_email
msg["Subject"] = subject
添加邮件正文(可选):
body = "最新的测试报告请查收!"
text = MIMEText(body)
msg.attach(text)
添加附件:
with open(attachment_path, 'rb') as f:
attachment_data = f.read()
attachment = MIMEApplication(attachment_data)
attachment.add_header("Content-Disposition", "attachment", filename="最新测试报告.html")
msg.attach(attachment)
4. 发送邮件
连接SMTP服务器并登录:
smtp_server = "SMTP邮箱服务器地址"
smtp_port = 465 # 或587
smtp_user = from_email
smtp_password = "发件人邮箱登录密码"
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
server.login(smtp_user, smtp_password)
发送邮件:
server.sendmail(from_email, to_email, msg.as_string())
关闭连接:
server.quit()
示例
下面是两个示例,分别演示了从发送Gmail和腾讯企业邮箱的情况。
示例一:使用Gmail发送邮件
假设要从Gmail发邮件,SMTP服务器地址为smtp.gmail.com,端口号为465。以下是代码示例:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
subject = "最新测试报告"
from_email = "发件人Gmail地址"
smtp_server = "smtp.gmail.com"
smtp_port = 465
smtp_user = from_email
smtp_password = "Gmail账户密码"
to_email = "收件人邮箱地址"
attachment_path = "最新测试报告文件路径"
body = "最新的测试报告请查收!"
msg = MIMEMultipart()
msg["From"] = from_email
msg["To"] = to_email
msg["Subject"] = subject
text = MIMEText(body)
msg.attach(text)
with open(attachment_path, 'rb') as f:
attachment_data = f.read()
attachment = MIMEApplication(attachment_data)
attachment.add_header("Content-Disposition", "attachment", filename="最新测试报告.html")
msg.attach(attachment)
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
server.login(smtp_user, smtp_password)
server.sendmail(from_email, to_email, msg.as_string())
server.quit()
示例二:使用腾讯企业邮箱发送邮件
假设要从腾讯企业邮箱发邮件,SMTP服务器地址为smtp.exmail.qq.com,端口号可选465或587。以下是代码示例:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
subject = "最新测试报告"
from_email = "发件人腾讯企业邮箱地址"
smtp_server = "smtp.exmail.qq.com"
smtp_port = 465 # 或587
smtp_user = from_email
smtp_password = "腾讯企业邮箱登录密码"
to_email = "收件人邮箱地址"
attachment_path = "最新测试报告文件路径"
body = "最新的测试报告请查收!"
msg = MIMEMultipart()
msg["From"] = from_email
msg["To"] = to_email
msg["Subject"] = subject
text = MIMEText(body)
msg.attach(text)
with open(attachment_path, 'rb') as f:
attachment_data = f.read()
attachment = MIMEApplication(attachment_data)
attachment.add_header("Content-Disposition", "attachment", filename="最新测试报告.html")
msg.attach(attachment)
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
server.login(smtp_user, smtp_password)
server.sendmail(from_email, to_email, msg.as_string())
server.quit()
以上就是使用Python将最新的测试报告以附件的形式发到指定邮箱的完整攻略,可以根据需要进行调整和扩展。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:使用python将最新的测试报告以附件的形式发到指定邮箱 - Python技术站