python實現QQ郵箱群發郵件實例

一,登錄郵箱,獲取授權碼

在這裡插入圖片描述

在這裡插入圖片描述

二,替換參數

給多人發送郵箱,我隻是做瞭個循環,把每個人得授權碼循環輸入瞭。把授權碼和郵箱替換成自己得就行,內容你可以自己定義

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from sql_chi import *
import datetime

class Phpson:

    def __init__(self,msg_from,passwd,msg_to):
       
        self.sql_pool = My_mysql_connPool(max_connections=1, **info)

        self.msg_from = msg_from  # 發送方郵箱
        self.passwd = passwd  # 填入發送方郵箱的授權碼(就是剛剛你拿到的那個授權碼)
        self.msg_to = msg_to  # 收件人郵箱
        # # file_path = 'demo.xlsx' # 需要發送的附件目錄

        self.text_content = "這個參數是郵件內容"#這個特別重要

    # 寫成瞭一個通用的函數接口,想直接用的話,把參數的註釋去掉就好
    def send_email(self, file_path=None):
        finishTime = datetime.datetime.now()
        msg = MIMEMultipart()
        subject =f"{finishTime}爬取新聞數據總數"  # 主題
        text = MIMEText(self.text_content)
        msg.attach(text)
        # docFile = 'C:/Users/main.py'  如果需要添加附件,就給定路徑
        if file_path:  # 最開始的函數參數我默認設置瞭None ,想添加附件,自行更改一下就好
            docFile = file_path
            docApart = MIMEApplication(open(docFile, 'rb').read())
            docApart.add_header('Content-Disposition', 'attachment', filename=docFile)
            msg.attach(docApart)
            print('發送附件!')
        msg['Subject'] = subject
        msg['From'] = self.msg_from
        msg['To'] = self.msg_to
        try:
            s = smtplib.SMTP_SSL("smtp.qq.com", 465)
            s.login(self.msg_from, self.passwd)
            s.sendmail(self.msg_from, self.msg_to, msg.as_string())
            print("發送成功")
        except smtplib.SMTPException as e:
            print("發送失敗")
        finally:
            s.quit()


fs = ['97679',"wangqi"]  # 發送方郵箱
sq = ['widr',"wihxa"]  # 填入發送方郵箱的授權碼(就是剛剛你拿到的那個授權碼)
sj = ['97679',"wangq"]  # 收件人郵箱

for i in range(len(fs)):
    wqx = Phpson(fs[i],sq[i],sj[i])
    wqx.Dintes()
    wqx.send_email()



總結

到此這篇關於python實現QQ郵箱群發郵件實例的文章就介紹到這瞭,更多相關python QQ郵箱群發郵件內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: