python3:利用SMTP協議發送QQ郵件+附件

轉載請代表出處:http://www.javashuo.com/article/p-rmamgiew-ec.htmlhtml

1.發送QQ郵件,首先必須知道QQ郵箱的SMTP服務器安全

http://service.mail.qq.com/cgi-bin/help?id=28&no=167&subtype=1服務器

2.發送郵件以前,必須開啓qq郵箱的smtp服務app

設置路徑:郵箱設置--帳戶--開啓截圖服務--保存更改測試

3.代碼拋出異常分析編碼

(1)郵箱密碼傳入值爲平常登陸密碼,報錯加密

global send_user global email_host global password password = 'xxx92' email_host = "smtp.qq.com" send_user = "11xxx@qq.com"

拋出異常:spa

smtplib.SMTPAuthenticationError:(535, b'Error: \xc7\xeb\xca\xb9\xd3\xc3\xca\xda\xc8\xa8\xc2\xeb\xb5\xc7\xc2\xbc\xa1\xa3\xcf\xea\xc7\xe9\xc7\xeb\xbf\xb4: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256').net

打開拋出異常中的連接:是關於受權碼的介紹,根據介紹,登陸時應該使用受權碼做爲登陸密碼,該處的受權碼是開啓服務時收到的16位受權碼debug

http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

修改代碼:

password = "lunkbrgwqxhfjgxx"(對應的16位受權碼)

(2)安全郵件,須要經過SSL發送

server = smtplib.SMTP() server.connect(email_host,25)

拋出異常:

smtplib.SMTPServerDisconnected: Connection unexpectedly closed

QQ郵箱是支持安全郵件的,須要經過SSL發送的郵件:使用標準的25端口鏈接SMTP服務器時,使用的是明文傳輸,發送郵件的整個過程可能會被竊聽。要更安全地發送郵件,能夠加密SMTP會話,實際上就是先建立SSL安全鏈接,而後再使用SMTP協議發送郵件

修改代碼:

server = smtplib.SMTP_SSL() server.connect(email_host,465)# 啓用SSL發信, 端口通常是465

4.附上完整代碼

#coding:utf-8
import smtplib from email.mime.text import MIMEText class SendEmail: global send_user global email_host global password password = "lunkbrgwqxhfjgxx" email_host = "smtp.qq.com" send_user = "11xx@qq.com"

    def send_mail(self,user_list,sub,content): user = "shape" + "<" + send_user + ">" message = MIMEText(content,_subtype='plain',_charset='utf-8') message['Subject'] = sub message['From'] = user message['To'] = ";".join(user_list) server = smtplib.SMTP_SSL() server.connect(email_host,465) server.login(send_user,password) server.sendmail(user,user_list,message.as_string()) server.close() if __name__ == '__main__': send = SendEmail() user_list = ['11xx@qq.com'] sub = "測試郵件" content = "ceshi看看" send.send_mail(user_list,sub,content)

(1)Python對SMTP支持有smtplib和email兩個模塊,email負責構造郵件,smtplib負責發送郵件

(2)構造MIMEText對象時,第一個參數是郵件正文;第二個參數是MIME的subtype,傳入'plain'表示純文本,最終的MIME就是'text/plain';最後必定要用utf-8編碼保證多語言兼容性

(3)發送的郵件須要添加頭部信息,頭部信息中包含發送者、接收者、郵件主題等信息:message['From']、message['To']、message['Subject']

(4)構造完要發送的郵件信息後,經過SMTP發出去:login()方法用來登陸SMTP服務器;sendmail()方法就是發郵件,因爲能夠一次發給多我的,因此傳入一個list;郵件正文是一個str,as_string()把MIMEText對象變成str

(5)SMTP.close() :關閉SMTP服務器鏈接

 

五、發送郵件帶附件

參考代碼:

 

#coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

class SendEmail:
    global send_user
    global email_host
    global password
    password = "lunkbrgwqxhfjgxx"
    email_host = "smtp.qq.com"
    send_user = "xx@qq.com"

    def send_mail(self,user_list,sub,content):
        user = "shape" + "<" + send_user + ">"

        # 建立一個帶附件的實例
        message = MIMEMultipart()
        message['Subject'] = sub
        message['From'] = user
        message['To'] = ";".join(user_list)

        # 郵件正文內容
        message.attach(MIMEText(content, 'plain', 'utf-8'))

        # 構造附件(附件爲txt格式的文本)
        att = MIMEText(open('../log/log.txt', 'rb').read(), 'base64', 'utf-8')
        att["Content-Type"] = 'application/octet-stream'
        att["Content-Disposition"] = 'attachment; filename="Log.txt"'
        message.attach(att)

        server = smtplib.SMTP_SSL()
        server.connect(email_host,465)# 啓用SSL發信, 端口通常是465
        # server.set_debuglevel(1)# 打印出和SMTP服務器交互的全部信息
        server.login(send_user,password)
        server.sendmail(user,user_list,message.as_string())
        server.close()

    def send_main(self,pass_list,fail_list,no_run_list):
        pass_num = len(pass_list)
        fail_num = len(fail_list)
        #未執行的用例
        no_run_num = len(no_run_list)
        count_num = pass_num + fail_num + no_run_num

        #成功率、失敗率
        '''
        用%對字符串進行格式化
        %d 格式化整數
        %f 格式化小數;想保留兩位小數,須要在f前面加上條件:%.2f;用%%來表示一個%
        若是你不太肯定應該用什麼,%s永遠起做用,它會把任何數據類型轉換爲字符串 
       '''
        pass_result = "%.2f%%" % (pass_num/count_num*100)
        fail_result = "%.2f%%" % (fail_num/count_num*100)
        no_run_result = "%.2f%%" % (no_run_num/count_num*100)

        user_list = ['xx@qq.com']
        sub = "接口自動化測試報告"
        content = "總共執行接口個數%s個,經過個數%s個,失敗個數%s個,未執行個數%s個:經過率爲%s,失敗率爲%s,未執行率爲%s" % (count_num,pass_num,fail_num,no_run_num,pass_result,fail_result,no_run_result)
        self.send_mail(user_list,sub,content)

 

舉例說明附件爲TXT類型,其餘類型的可參考:https://blog.csdn.net/u013250071/article/details/79037843

相關文章
相關標籤/搜索