日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:pythonemail模塊的使用

說明

1、email模塊支持發(fā)送的郵件內(nèi)容包括純文本、HTML內(nèi)容、圖片和附件。

2、email模塊有幾種類型,用于不同的郵件內(nèi)容形式。

有MIMEText、MIMEImage和MIMEMultupart。

MIMEText:內(nèi)容為純文本和HTML頁面。

MIMEImage:內(nèi)容是圖片。

MIMEMultupart:可以包含文本和附件。

實例

#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@author:freesigefei
Created on 2016年3月20日
Updated on 2016年5月4日
'''
#------------------------------------------------------------------------------------------------
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import os,time,re
 
def send_Test_email(mail_to):
    '''本模塊實現(xiàn)獲取最新的測試報告html文件,讀取部分報告內(nèi)容作為郵件正文,將報告作為附件,并發(fā)送到指定的郵箱,
        參數(shù)mail_to代表的是接收郵箱,例如:'xxx@126.com' '''
    
    #發(fā)送郵箱
    mail_from = 'yyy@sina.com'
    #發(fā)送郵件主題
    mail_subject = 'Automation Test Report'
    #發(fā)送郵箱服務(wù)器
    mail_smtpserver = 'smtp.sina.com'
    #發(fā)送郵箱用戶/密碼
    mail_username = 'yyy@sina.com'
    mail_password = 'yyyyyy'
 
    #定義郵件內(nèi)容,中文需參數(shù)‘utf-8’,單字節(jié)字符不需要
    '''
    #發(fā)送文件形式的郵件
    msg = MIMEText('你好!','text','utf-8')
    '''
    '''
    #發(fā)送html形式以正常文本顯示在郵件內(nèi)容中的郵件
    msg = MIMEText('

你好!

','html','utf-8')     '''     '''     #讀取html文件內(nèi)容并發(fā)送     f=open(file_new,'rb')     mail_body=f.read()     f.close()     print mail_body     msg=MIMEText(mail_body,_subtype='html',_charset='utf-8')     '''          #創(chuàng)建一個帶附件的郵件實例(內(nèi)容)     msg = MIMEMultipart()     #找到report目錄下最新生成的報告文件供后續(xù)使用     result_dir = 'D:\\report'     lists=os.listdir(result_dir)     lists.sort(key=lambda fn: os.path.getmtime(result_dir+"\\"+fn) if not                os.path.isdir(result_dir+"\\"+fn) else 0)     print (u'The Latest Test Report is: '+lists[-1])     file_new = os.path.join(result_dir,lists[-1])     #讀取最新的測試報告文件獲取部分信息來定義郵件的內(nèi)容     Regex_Theme=re.compile(r'Automation Test Report')     Regex_Content=re.compile(r'(.*:)(.*)<')     Report_File=open(file_new,'r')     Mail_Content=[]     for line in Report_File.readlines():         if 'Automation Test Report' in line or "

" in line:             i=Regex_Theme.findall(line)             j=Regex_Content.findall(line)             if i != []:                 Mail_Content.append(i)             if j != []:                 Mail_Content.append(j)     Report_File.close()     #將讀取到的測試報告的數(shù)據(jù)以html形式顯示為郵件的中文     msgTest=MIMEText('''

Test completed,Test results are as follows:

'''                      ''''''                      '''

'''+Mail_Content[0][0]+'''

'''                      '''

'''+Mail_Content[1][0][0]+''''''+Mail_Content[1][0][1]+'''

'''                      '''

'''+Mail_Content[2][0][0]+''''''+Mail_Content[2][0][1]+'''

'''                      '''

'''+Mail_Content[3][0][0]+''''''+Mail_Content[3][0][1]+'''

'''                      ''''''                      '''

PS: Detailed test results please refer to the attachment

'''                      ,'html','utf-8')     msg.attach(msgTest)     #定義郵件的附件     att1 = MIMEText(open(file_new, 'rb').read(), 'base64', 'utf-8')     att1["Content-Type"] = 'application/octet-stream'     att1["Content-Disposition"] ='attachment; filename="Automation test report.html"'#這里的filename指的是附件的名稱及類型     msg.attach(att1)     #將郵件的主題等相關(guān)信息添加到郵件實例     msg['Subject'] = Header(mail_subject)     msg['From'] = mail_from     msg['To'] = mail_to     msg['date']=time.strftime('%a, %d %b %Y %H:%M:%S %z')     #創(chuàng)建發(fā)送服務(wù)器實例并將發(fā)送服務(wù)器添加到實例中     smtp = smtplib.SMTP()     smtp.connect(mail_smtpserver)     '''     #采用ssl加密傳輸     smtp.ehlo()     smtp.starttls()     smtp.ehlo()     '''     '''     #打印交互的日志信息     #smtp.set_debuglevel(1)     '''     #登錄發(fā)送郵件服務(wù)器并進行郵件的發(fā)送     smtp.login(mail_username, mail_password)     smtp.sendmail(mail_from, mail_to, msg.as_string())     print u'Test report sent successfully,Please go to the following email to check the test report :%s' %mail_to     smtp.quit()      #---------------------------------------------------------------------------------------------------- if __name__ == "__main__":     send_Test_email('xxx@126.com')

以上就是python email模塊的使用,希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程

本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。


網(wǎng)站標題:創(chuàng)新互聯(lián)Python教程:pythonemail模塊的使用
文章網(wǎng)址:http://www.dlmjj.cn/article/djcdjch.html