点击查看
持续@2018
欢迎技术交流@2018

python django 测试报告 发送邮件

记录下python dajango 发送邮件:

python django 邮件发送测试报告给指定发件人,测试报告支持自定义上传文件,以后更新,可支持发送多个收件人列表

HTML代码:

 

 1 <form class="form-horizontal" action="/MailTest/" method="post" enctype="multipart/form-data">
 2     {% csrf_token %}
 3     <div class="form-group">
 4       <label for="inputEmail3" class="col-sm-2 control-label">发件人邮箱:</label>
 5       <div class="col-sm-5">
 6         <input type="text" class="form-control"  placeholder="请输入发件人邮箱" name="from_addr">
 7       </div>
 8     </div>
 9     <div class="form-group">
10         <label for="inputPassword3" class="col-sm-2 control-label">发件人邮箱密码:</label>
11         <div class="col-sm-5">
12           <input type="text" class="form-control"  placeholder="发件人邮箱密码" name="password">
13         </div>
14        </div>
15        <div class="form-group">
16       <label for="inputEmail3" class="col-sm-2 control-label">SMTP服务器地址:</label>
17       <div class="col-sm-5">
18         <input type="text" class="form-control"  placeholder="SMTP服务器地址" name="smtp_addr">
19       </div>
20     </div>
21     <div class="form-group">
22       <label for="inputEmail3" class="col-sm-2 control-label">收件人邮箱:</label>
23       <div class="col-sm-5">
24         <input type="text" class="form-control"  placeholder="请输入收件人邮箱" name="to_addrs">
25       </div>
26     </div>
27     <div class="form-group">
28       <label for="inputEmail3" class="col-sm-2 control-label">邮件主题:</label>
29       <div class="col-sm-5">
30         <input type="text" class="form-control"  placeholder="邮件主题" name="subject">
31       </div>
32     </div>
33     <div class="form-group">
34       <label for="inputEmail3" class="col-sm-2 control-label">邮件内容:</label>
35       <div class="col-sm-5">
36         <input type="text" class="form-control"  placeholder="邮件内容" name="msg">
37       </div>
38        </div>
39 
40     <div class="form-group">
41       <label for="inputEmail3" class="col-sm-2 control-label">测试报告:</label>
42       <div class="col-sm-5">
43         <input type="file" class="form-control"  name="testFile">
44       </div>
45     </div>
46     
47     
48     
49     <div class="form-group">
50       <div class="col-sm-offset-2 col-sm-10">
51         <button type="submit" class="btn btn-info">发送邮件</button>
52       </div>
53     </div>
54 </form>

sendMailTest获取post的参数:

 1  def sendMailTest(request):
 5         from_addr = request.POST['from_addr']
 6         smtp_addr = request.POST['smtp_addr']
 7         to_addrs = request.POST['to_addrs']
 8         subject = request.POST['subject']+time.strftime("%Y-%m-%d %H-%M-%S")
 9         password = request.POST['password']
10         msg = request.POST['msg'] 
11 
12         testFile = request.FILES.getlist('testFile')
13         testFile1=''
14         for f in testFile:      
15            
16             for chunk in f.chunks():
17                 testFile1+=chunk.decode()19         mess = {}
20         if from_addr !='' and smtp_addr !='' and to_addrs !='' and password !='':
21             common = Common()
22             status = common.SendMail(subject,msg,to_addrs,from_addr,smtp_addr,password,testFile1)
23             print (status)
24             if status ==1:
25                 mess['status'] = '发送邮件成功'
26             elif status==0:
27                  mess['status'] = '发送邮件失败'
28         elif request.POST['from_addr'] =='' or request.POST['password'] == '' or to_addrs == '' or smtp_addr =='' or subject == '':
29             mess['status'] = '请填写发件人、收件人、密码、邮箱服务器地址、主题'
30         
31         return render(request, "sendMail.html", mess)
SendMail发送带附件的邮件
 1  def SendMail(self,subject,msg,to_addrs,from_addr,smtp_addr,password,testFile):
 2         '''
 3         @subject:邮件主题
 4         @msg:邮件内容
 5         @to_addrs:收信人的邮箱地址
 6         @from_addr:发信人的邮箱地址
 7         @smtp_addr:smtp服务地址,可以在邮箱看,比如163邮箱为smtp.163.com
 8         @password:发信人的邮箱密码
 9         '''
10         mail_msg = MIMEMultipart()      #创建一个带附件实例
11 
12         #构造附件test.docx
13         att1 = MIMEText(testFile, 'base64', 'gb2312')
14         att1["Content-Type"] = 'application/octet-stream'
15         att1.add_header('Content-Disposition', 'attachment', filename=u'测试报告.html') 
16         mail_msg.attach(att1)
17         #构建MIMEText纯文本内容
18         txt = MIMEText(msg,'plain', 'utf-8')
19         mail_msg.attach(txt)
20 
21         mail_msg['Subject'] = subject
22         mail_msg['From'] =from_addr
23         mail_msg['To'] = ','.join(to_addrs)
24         try:
25             s = smtplib.SMTP()
26             s.connect(smtp_addr)  #连接smtp服务器
27             s.login(from_addr,password)  #登录邮箱
28             s.sendmail(from_addr, to_addrs, mail_msg.as_string()) #发送邮件
29             s.quit()
30             print ('success')
31             return 1
32         except Exception as e:
33             print (str(e))
34             return 0

 

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

 

 

posted @ 2018-09-04 12:32  小姐姐1024  阅读(852)  评论(2编辑  收藏  举报