python附件发送到邮箱里_使用python将最新的测试报告以附件的形式发到指定邮箱...

具体代码如下所示:

import smtplib, email, os, time

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.header import Header

#设置smtplib所需的参数

smtpserver = 'smtp.qq.com' #SMTP服务器地址

username = 'xxx@qq.com' # 发件人地址,通过控制台创建的发件人地址

password = '******' # 发件人密码,通过控制台创建的发件人密码

receiver = ['xxx@dadaodata.com'] #单个收件人

# receivers = ['xxx@dadaodata.com', 'xxx@qq.com'] # 收件人地址或是地址列表,支持多个收件人,最多30个

# 构造邮件MIMEMultipart对象

msg = MIMEMultipart('mixed')

msg['Subject'] = Header('自动化测试报告' + time.strftime("%Y-%m-%d"), 'utf-8').encode()#自定义邮件主题

msg['From'] = '%s ' % (username, username)#邮件发送者

msg['To'] = ";".join(receiver)#邮件接受者

msg['Message-id'] = email.utils.make_msgid()

msg['Date'] = email.utils.formatdate()

# 构造文字内容

text_plain = MIMEText('附件为接口自动化测试报告,请查收!', 'plain', 'utf-8')#邮件内容

msg.attach(text_plain)

#构造附件

test_report = r'F:\PythonAutomation\Python_PyCharm\TestReport' #存放文件的目录

lists = os.listdir(test_report) #列出目录的下所有文件保存到lists

lists.sort(key=lambda fn:os.path.getmtime(test_report + "\\" + fn)) #按时间排序

file_new = os.path.join(test_report,lists[-1]) #获取最新的文件保存到file_new

sendfile = open(file_new,'rb').read()

text_att = MIMEText(sendfile, 'base64', 'utf-8')

text_att["Content-Type"] = 'application/octet-stream'

text_att["Content-Disposition"] = 'attachment; filename="report.html"'#重新命名附件

msg.attach(text_att)

# 发送邮件

try:

# client = smtplib.SMTP()

# client.connect(smtpserver, 25) #SMTP普通端口为25

client = smtplib.SMTP_SSL() #python 2.7以上版本,若需要可使用SSL

client.connect(smtpserver, 465) #SSL端口465

# client.set_debuglevel(1) #用set_debuglevel(1)可以打印出和SMTP服务器交互的所有信息

client.login(username, password)

client.sendmail(username, receiver, msg.as_string())

client.quit()

print('邮件发送成功')

except smtplib.SMTPConnectError as e:

print('邮件发送失败,连接失败:', e.smtp_code, e.smtp_error)

except smtplib.SMTPAuthenticationError as e:

print('邮件发送失败,认证错误:', e.smtp_code, e.smtp_error)

except smtplib.SMTPSenderRefused as e:

print('邮件发送失败,发件人被拒绝:', e.smtp_code, e.smtp_error)

except smtplib.SMTPRecipientsRefused as e:

print('邮件发送失败,收件人被拒绝:', e.args, e.recipients)

except smtplib.SMTPDataError as e:

print('邮件发送失败,数据接收拒绝:', e.smtp_code, e.smtp_error)

except smtplib.SMTPException as e:

print('邮件发送失败: ', str(e))

except Exception as e:

print('邮件发送失败: ', str(e))

执行结果如下:

总结

以上所述是小编给大家介绍的使用python将最新的测试报告以附件的形式发到指定邮箱,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Python的SMTP库来发送邮件,具体步骤如下: 1. 导入SMTP库 ```python import smtplib ``` 2. 设置SMTP服务器和端口号 ```python smtp_server = 'smtp.邮箱服务商.com' # 邮箱服务商的SMTP服务器地址 smtp_port = 587 # SMTP服务器的端口号 ``` 3. 登录邮箱账号 ```python sender = '发送邮箱地址' password = '发送邮箱密码' smtp_obj = smtplib.SMTP(smtp_server, smtp_port) smtp_obj.starttls() # 开启TLS加密 smtp_obj.login(sender, password) ``` 4. 设置邮件内容 ```python from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication # 创建MIMEMultipart对象 msg = MIMEMultipart() # 构建邮件正文 body = MIMEText('邮件正文') msg.attach(body) # 构建邮件附件 with open('文件路径', 'rb') as f: attachment = MIMEApplication(f.read(), _subtype='txt') attachment.add_header('Content-Disposition', 'attachment', filename='文件名.txt') msg.attach(attachment) # 设置邮件主题、发件人、收件人 msg['Subject'] = '邮件主题' msg['From'] = sender msg['To'] = '接收者邮箱地址' ``` 5. 发送邮件 ```python smtp_obj.sendmail(sender, '接收者邮箱地址', msg.as_string()) smtp_obj.quit() ``` 完整代码示例: ```python import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication smtp_server = 'smtp.邮箱服务商.com' # 邮箱服务商的SMTP服务器地址 smtp_port = 587 # SMTP服务器的端口号 sender = '发送邮箱地址' password = '发送邮箱密码' smtp_obj = smtplib.SMTP(smtp_server, smtp_port) smtp_obj.starttls() # 开启TLS加密 smtp_obj.login(sender, password) msg = MIMEMultipart() body = MIMEText('邮件正文') msg.attach(body) with open('文件路径', 'rb') as f: attachment = MIMEApplication(f.read(), _subtype='txt') attachment.add_header('Content-Disposition', 'attachment', filename='文件名.txt') msg.attach(attachment) msg['Subject'] = '邮件主题' msg['From'] = sender msg['To'] = '接收者邮箱地址' smtp_obj.sendmail(sender, '接收者邮箱地址', msg.as_string()) smtp_obj.quit() ``` 注意替换代码中的`smtp_server`、`smtp_port`、`sender`、`password`、`文件路径`、`文件名`、`邮件主题`、`邮件正文`、`接收者邮箱地址`等参数为你自己的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值