python django 测试报告 发送邮件

python django 邮件发送测试报告给指定发件人,测试报告支持自定义上传

HTML代码:

{% csrf_token %}
sendMailTest获取post的参数:
 
def sendMailTest(request):
        from_addr = request.POST['from_addr']
        smtp_addr = request.POST['smtp_addr']
        to_addrs = request.POST['to_addrs']
        subject = request.POST['subject']+time.strftime("%Y-%m-%d %H-%M-%S")
        password = request.POST['password']
        msg = request.POST['msg'] 

        testFile = request.FILES.getlist('testFile')
        testFile1=''
        for f in testFile:      
           
            for chunk in f.chunks():
                testFile1+=chunk.decode()19         mess = {}
        if from_addr !='' and smtp_addr !='' and to_addrs !='' and password !='':
            common = Common()
            status = common.SendMail(subject,msg,to_addrs,from_addr,smtp_addr,password,testFile1)
            print (status)
            if status ==1:
                mess['status'] = '发送邮件成功'
            elif status==0:
                 mess['status'] = '发送邮件失败'
        elif request.POST['from_addr'] =='' or request.POST['password'] == '' or to_addrs == '' or smtp_addr =='' or subject == '':
            mess['status'] = '请填写发件人、收件人、密码、邮箱服务器地址、主题'
        
        return render(request, "sendMail.html", mess)
SendMail发送带附件的邮件 
def SendMail(self,subject,msg,to_addrs,from_addr,smtp_addr,password,testFile):
        '''
        @subject:邮件主题
        @msg:邮件内容
        @to_addrs:收信人的邮箱地址
        @from_addr:发信人的邮箱地址
        @smtp_addr:smtp服务地址,可以在邮箱看,比如163邮箱为smtp.163.com
        @password:发信人的邮箱密码
        '''
        mail_msg = MIMEMultipart()      #创建一个带附件实例

        #构造附件test.docx
        att1 = MIMEText(testFile, 'base64', 'gb2312')
        att1["Content-Type"] = 'application/octet-stream'
        att1.add_header('Content-Disposition', 'attachment', filename=u'测试报告.html') 
        mail_msg.attach(att1)
        #构建MIMEText纯文本内容
        txt = MIMEText(msg,'plain', 'utf-8')
        mail_msg.attach(txt)

        mail_msg['Subject'] = subject
        mail_msg['From'] =from_addr
        mail_msg['To'] = ','.join(to_addrs)
        try:
            s = smtplib.SMTP()
            s.connect(smtp_addr)  #连接smtp服务器
            s.login(from_addr,password)  #登录邮箱
            s.sendmail(from_addr, to_addrs, mail_msg.as_string()) #发送邮件
            s.quit()
            print ('success')
            return 1
        except Exception as e:
            print (str(e))
            return 0

 

python 3抛出异常用 except Exception as e 而不是except Exception, e

 

python django 测试报告 发送邮件_第1张图片

 

你可能感兴趣的:(python,selenium)