一,登录邮箱,获取授权码
二,替换参数
给多人发送邮箱,我只是做了个循环,把每个人得授权码循环输入了。把授权码和邮箱替换成自己得就行,内容你可以自己定义
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邮箱群发邮件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!