亲宝软件园·资讯

展开

python QQ邮箱群发邮件

风启新尘 人气:0

一,登录邮箱,获取授权码

在这里插入图片描述

在这里插入图片描述

二,替换参数

给多人发送邮箱,我只是做了个循环,把每个人得授权码循环输入了。把授权码和邮箱替换成自己得就行,内容你可以自己定义

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()



总结

加载全部内容

相关教程
猜你喜欢
用户评论