python

解析python发送邮件

2019-08-11  本文已影响0人  gaby666

获取邮件服务器

email_host = "smtp.163.com"
send_user = "xxxxxx@163.com"
password = "xxxx123456"

定义发送邮件主体

def send_mail(self,user_list,sub,content):
    user = "xxxxxx"+"<"+send_user+">"
    message = MIMEText(content,_subtype='plain',_charset='utf-8')
   message['Subject'] = sub
    message['From'] = user
   message['To'] = ";".join(user_list)
    server = smtplib.SMTP()
     server.connect(email_host)
    server.login(send_user,password)
    server.sendmail(user,user_list,message.as_string())
   server.close()

定义发送邮件的主体方法,统计发送邮件失败的个数,和发送邮件成功个数

def send_main(self,pass_list,fail_list):
 pass_num = float(len(pass_list))
     fail_num = float(len(fail_list))
      count_num = pass_num+fail_num
       pass_result = "%.2f%%" %(pass_num/count_num*100)
        fail_result = "%.2f%%" %(fail_num/count_num*100)
          user_list = ['123456789@qq.com']
          sub = "接口自动化测试报告"
           content = "此次一共运行接口个数为%s个,通过个数为%s个,失败个数为%s,通过率为%s,失败率为%s" % 
           (count_num,pass_num,fail_num,pass_result,fail_result )
            self.send_mail(user_list,sub,content)

               if __name__ == '__main__':
               sen = SendEmail()
                 sen.send_main([1,2,3,4],[2,3,4,5,6,7])
上一篇下一篇

猜你喜欢

热点阅读