python3.5+selenium 自动发邮件功能

"""发送邮件相关方法"""
def new_file(self,test_dir):
    """获取测试报告的最新路径"""
    lists=os.listdir(test_dir)
    lists.sort(key=lambda fn:os.path.getmtime(test_dir+'\\'+fn))
    file_path=os.path.join(test_dir,lists[-1])
    return file_path

def send_email(self,newfile):
    """发邮件"""
    f=open(newfile,'rb')
    mail_body=f.read()
    f.close()
    #发送邮箱相关信息
    smtpserver = PublicConfig.email_smtpserver
    user = PublicConfig.email_username
    password = PublicConfig.email_password
    sender=PublicConfig.email_sender
    receiver=PublicConfig.email_receiver
    mailToCc=PublicConfig.email_toco
    now = time.strftime("%Y-%m-%d")
    subject = PublicConfig.email_subject+now

    msg=MIMEMultipart()

    text = u"Dear all!\n\n    附件是RMP(收入管理平台)最新的自动化测试报告,此邮件是脚本执行后定时自动发送(每周二、周五),请查收指正!\n\n谢谢!"
    msg_plain = MIMEText(text,'plain', 'utf-8')
    msg.attach(msg_plain)

    # msg_html1 = MIMEText(mail_body,'html','utf-8')
    # msg.attach(msg_html1)

    msg_html = MIMEText(mail_body,'html','utf-8')
    msg_html["Content-Disposition"] = 'attachment; filename="RMP_AutoTestReport.html"'
    msg.attach(msg_html)
    # msg_html = MIMEText(mail_body,'html','utf-8')
    # msg_html["Content-Type"] = 'application/octet-stream'
    # msg_html.add_header('Content-Disposition', 'attachment', filename='testreport.html')
    # msg.attach(msg_html)
    msg['From'] = sender
    msg['To'] = ";".join(receiver)
    msg['Cc'] = ';'.join(mailToCc)
    msg['Subject']=Header(subject,'utf-8')
    #连接发送邮件

    smtp=smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(user, password)
    smtp.sendmail(sender, receiver+mailToCc, msg.as_string())
    smtp.quit()
#python启动类执行完用例后运行
test_report_dir='D:/python/RMPAutoTest/rmp/report'
u=Util()
new_report=u.new_file(test_report_dir)
u.send_email(new_report)
print('=====AutoTest Over======')
#邮箱服务器、账号、密码;发送人、接收人、抄送人;邮件主题
email_smtpserver = 'mail.com'
email_username = 'username'
email_password = 'password'
email_sender = 'aa.com'
email_receiver = ['bb.com', 'cc.com', 'dd.com']
email_toco = ['ee.com', 'ff.com']
email_subject = 'RMP_自动化测试报告_'

持续集成:

1、安装jenkins,

猜你喜欢

转载自blog.csdn.net/weixin_42519759/article/details/81185321