利用python自動發郵件

工做中有時長時間運行代碼時須要監控進度,或者須要按期發送固定格式郵件時,能夠使用下面定義的郵件函數。html

該函數調用了outlook和qqmail的接口,只須要放置到python的環境目錄中便可 import 並使用。這裏建議全部本身定義的函數放置在python3的文件下 而非site-package文件下。python

也能夠防止在任意默認環境路徑下。默認路徑查詢:app

import site;
site.getsitepackages()

本身定義的函數將py文件命名爲send_mail.py而後放置在上面顯示的其中一個路徑下便可。ide

代碼:函數

def send_outlook(title,body,receivers,attachments):
    '''
    ==========================================================
    
    title='this is a title!'
    body=
    '
        Mates!
        Bon Apetite
    '
    receivers=['123211855@qq.com','ext.wenzhe.tian@uaes.com']
    attachments=['a.py','b.py'];
    
    send_outlook(title,body,receivers,attachments)
    
    ==========================================================
    '''  
    import win32com.client as win32
    import warnings
    import pythoncom
    warnings.filterwarnings('ignore')
    pythoncom.CoInitialize()    
    sub=title;
    body = body
    outlook = win32.Dispatch('outlook.application')
    receivers = receivers
    for i in receivers:
        mail = outlook.CreateItem(0)
        mail.To = i
        mail.Subject = sub
        mail.Body = body
        for j in attachments:
            mail.Attachments.Add(j)
        mail.Send()

def send_qqmail(user,password,title,body,image,receivers,attachments):
    '''
    ==========================================================
    
    user='123211855@qq.com';
    password='zpkfkgxvdrnvbgcj'
    title='this is a title!'
    body=
    '
        Mates!
        Bon Apetite
    '
    receivers=['123211855@qq.com','ext.wenzhe.tian@uaes.com']
    attachments=[];
    
    send_qqmail(user,password,title,body,image,receivers,attachments)
    
    ==========================================================
    '''  
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.image import MIMEImage
    from email.mime.multipart import MIMEMultipart
    # 設置郵箱的域名
    HOST = 'smtp.qq.com'
    # 設置郵件標題
    SUBJECT = title
    # 設置發件人郵箱
    FROM = user
    password=password #'zpkfkgxvdrnvbgcj'
    # 設置收件人郵箱
    TO = receivers
    message = MIMEMultipart('related')
    
    #--------------------------------------發送文本-----------------
    # 發送郵件主體到對方的郵箱中
#    message_html = MIMEText('<h2 style="color:red;font-size:100px">CSDN博客超級好</h2><img src="cid:big">','html','utf-8')
    message_text=MIMEText(body,'plain','utf-8')
    message.attach(message_text)
    
    #-------------------------------------發送圖片--------------------
    # rb  讀取二進制文件
    # 要肯定當前目錄有1.jpg這個文件
    for i in image:
        image_data = open(i,'rb')
        # 設置讀取獲取的二進制數據
        message_image = MIMEImage(image_data.read())
        # 關閉剛纔打開的文件
        image_data.close()
        message_image.add_header('Content-ID','big')
        # 添加圖片文件到郵件信息當中去
        message.attach(message_image)
    
    #-------------------------------------添加文件---------------------
    # 要肯定當前目錄有table.xls這個文件
#    message_xlsx = MIMEText(open(r'C:\Users\ext.wenzhe.tian\Desktop\Dynamic_Data_Analysis_v3\Static_data_read_v2.py','rb').read(),'base64','utf-8')
    for i in attachments:
        message_xlsx = MIMEText(open(i,'rb').read(),'base64','utf-8')
        # 設置文件在附件當中的名字
        message_xlsx['Content-Disposition'] = 'attachment;filename='+i
        message.attach(message_xlsx)
    
    # 設置郵件發件人
    message['From'] = FROM
    # 設置郵件收件人
    message['To'] = TO
    # 設置郵件標題
    message['Subject'] = SUBJECT
    
    # 獲取簡單郵件傳輸協議的證書
    email_client = smtplib.SMTP_SSL()
    # 設置發件人郵箱的域名和端口,端口爲465
    email_client.connect(HOST,'465')
    # ---------------------------郵箱受權碼------------------------------
    result = email_client.login(FROM,password) 
    print('登陸結果',result)
    email_client.sendmail(from_addr=FROM,to_addrs=TO.split(','),msg=message.as_string())
    # 關閉郵件發送客戶端
    email_client.close()
View Code

 

以上代碼分outlook郵件和qq郵件發送,qq郵箱發送需開啓smtp的服務。this

調用方法以下:spa

from send_mail import send_outlook
from send_mail import send_qqmail
help(send_outlook)
help(send_qqmail)

'''
    ==========================================================
    
    title='this is a title!'
    body='
        Mates!
        Bon Apetite
    '
    receivers=['xxx@qq.com','xxx@xx.com']
    attachments=['a.py','b.py'];
    
    send_outlook(title,body,receivers,attachments)
    
    ====================================================================

    ==========================================================
    
    user='xxxxx@qq.com';
    password='xxxxx'
    title='this is a title!'
    body='
        Mates!
        Bon Apetite
    '
    receivers=['xx@qq.com','xx@xx.com']
    attachments=[];
    
    send_qqmail(user,password,title,body,image,receivers,attachments)
    
    ==========================================================
'''
相關文章
相關標籤/搜索