这里用到使用第三方 SMTP 服务发送,需要自己配置好163邮箱和qq邮箱的STMP服务,具体参考这里:163邮箱QQ邮箱STMP配置步骤
import smtplib
from email import (header)
from email.mime import (text, multipart)
import time
def sender_mail():
smtp_Obj = smtplib.SMTP() # 第三方 SMTP 服务
smtp_Obj.connect(host='smtp.qq.com', port=25) # 连接qq邮箱SMTP服务器,端口是25
sender_addrs = '[email protected]' # 发件人邮箱账号
password = "xxxxxxxxxxxxx" # 发件人邮箱密码 即配置生成的授权码
smtp_Obj.login(sender_addrs, password)
receiver_addrs = ['[email protected]', '[email protected]'] #群发的收件人
for email_addrs in receiver_addrs:
try:
msg = multipart.MIMEMultipart()
msg['From'] = "lizi"
msg['To'] = email_addrs
msg['subject'] = header.Header('邮件主题xxxx', 'utf-8')
msg.attach(text.MIMEText('我是李子,这是一封群发测试邮件,无需回复~', 'plain', 'utf-8')) #邮件内容
smtp_Obj.sendmail(sender_addrs, email_addrs, msg.as_string()) # 发件人邮箱账号、收件人邮箱账号、发送邮件
time.sleep(10) #休眠
print('成功发送给%s' % ( email_addrs))
except Exception as e:
continue
smtp_Obj.quit() #退出
sender_mail()