python 測試報告發送郵件


 

使用過程成出現的如下錯誤

smtplib.SMTPDataError: (554, 'DT:SPM 126 smtp5錯誤解決辦法

 

1.自動化測試中,調用郵件模塊自動發送郵件時,運行腳本報錯:

smtplib.SMTPDataError: (554, 'DT:SPM 126 smtp5,jtKowAD3MJz2c1JXLcK2AA--.52114S2 1465021431,please see http://mail.163.com/help/help_spam_16.htm?ip=123.114.121.110&hostid=smtp5&time=1465021431')

2.解決方法(這里已python為例):

#定義發送郵件
def send_mail(file_new):
f = open(file_new, 'rb')
mail_body = f.read()
f.close()

msg = MIMEText(mail_body, 'html', 'utf-8')
msg = MIMEText('請查看附件內容!','plain','utf-8')
msg['Subject'] = Header("自動化測試報告", 'utf-8')

#報錯原因是因為“發件人和收件人參數沒有進行定義
msg['from'] = 'test_bug@126.com'
msg['to'] = 'testyao@163.com'

smtp = smtplib.SMTP()
smtp.connect("smtp.126.com")
smtp.login("test_bug@126.com", "登錄密碼")
smtp.sendmail("test_bug@126.com","testyao@163.com", msg.as_string())
smtp.quit()
print('郵件發送成功email has send out !')

 3.利用此種方法(綠色代碼部分)即可解決相關郵箱的554, 'DT:SPM的錯誤。

本文為"測試那點事_TestYao"博客整理收集,轉載請注明出處,謝謝!

 以下代碼是通過python3實現了 郵件的發送

 

# coding:utf-8

import os, sys
import smtplib
import time
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

reportPath = os.path.join(os.getcwd(), 'report')  # 測試報告的路徑

print("打印路徑:")

print(reportPath)


# reportPath = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])),'report')

class SendMail(object):
    def __init__(self, recver=None):
        """接收郵件的人:list or tuple"""
        if recver is None:
            self.sendTo = ['chinayyj2010@163.com']  # 收件人這個參數,可以是list,或者tulp,以便發送給多人
        else:
            self.sendTo = recver

    def get_report(self):  # 該函數的作用是為了在測試報告的路徑下找到最新的測試報告
        dirs = os.listdir(reportPath)
        dirs.sort()
        newreportname = dirs[-1]
        print('The new report name: {0}'.format(newreportname))
        return newreportname  # 返回的是測試報告的名字

    def take_messages(self):  # 該函數的目的是為了 准備發送郵件的的消息內容
        newreport = self.get_report()
        self.msg = MIMEMultipart()
        self.msg['Subject'] = '測試報告主題'  # 郵件的標題
        self.msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')

        with open(os.path.join(reportPath, newreport), 'rb') as f:
            mailbody = f.read()  # 讀取測試報告的內容
        html = MIMEText(mailbody, _subtype='html', _charset='utf-8')  # 將測試報告的內容放在 郵件的正文當中
        self.msg.attach(html)  # 將html附加在msg里

        # html附件    下面是將測試報告放在附件中發送
        att1 = MIMEText(mailbody, 'base64', 'gb2312')
        att1["Content-Type"] = 'application/octet-stream'
        att1["Content-Disposition"] = 'attachment; filename="TestReport.html"'  # 這里的filename可以任意寫,寫什么名字,附件的名字就是什么
        self.msg.attach(att1)

    def send(self):
        self.take_messages()
        self.msg['from'] = 'wmXXXXXt@163.com'  # 發送郵件的人
        self.msg['to'] = 'cXXXXXXX@163.com'     # 收件人和發送人必須這里定義一下,執行才不會報錯。
        #smtp = smtplib.SMTP('smtp.163.com', 25)  # 連接服務器
        smtp = smtplib.SMTP()
        smtp.connect('smtp.163.com')
        smtp.login('wmqyyj_test@163.com', 'XXXXXXX')  # 登錄的用戶名和密碼(注意密碼是設置客戶端授權碼,因為使用用戶密碼不穩聽,有時無法認證成功,導致登錄不上,故無法發送郵件。)
        smtp.sendmail(self.msg['from'], self.msg['to'], self.msg.as_string())  # 發送郵件
        smtp.close()
        print('sendmail success')


if __name__ == '__main__':
    sendMail = SendMail()
    sendMail.send()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM