python django 測試報告 發送郵件

記錄下python dajango 發送郵件:html

python django 郵件發送測試報告給指定發件人,測試報告支持自定義上傳文件,之後更新,可支持發送多個收件人列表python

HTML代碼:django

 

 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服務器

 

相關文章
相關標籤/搜索