写了个批量投递邮件脚本 WIKI一下
轮询投递邮件
# 从CSV文件读取邮箱、密码和服务器地址
with open('123.csv', 'r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
next(reader) # 跳过标题行
accounts = [(row[0], row[1], row[2], row[3]) for row in reader]
# 从文本文件读取邮件地址列表
with open('mailaddress.txt', 'r') as txtfile:
recipients = [line.strip() for line in txtfile]
# 读取html文件内容
with open('html.html', 'r', encoding='utf-8') as htmlfile:
html_content = htmlfile.read()
# 设置每个邮箱的发送次数限制和IP发送次数限制
max_emails_per_account = 1
max_emails_per_ip =3
emails_sent_per_account = {account[0]: 0 for account in accounts}
emails_sent_per_ip = {}
# 循环遍历收件人列表,逐个发送邮件
for recipient in recipients:
# 随机选择一个可用的邮箱账号
available_accounts = [a for a in accounts if emails_sent_per_account[a[0]] < max_emails_per_account]
D:\delete\pythonproject\venv\Scripts\python.exe D:\delete\pythonproject\main.py
lairui2@hwmail.vip 已发送邮件给 focusj@qq.com。
zhoutianzheng@szu-edu.cn 已发送邮件给 focus.chen@qq.com。
xiangwei5@szu-edu.cn 已发送邮件给 anzhiyong@hwmail.vip。
IP地址 1.116.113.85 已经发送了最大数量的邮件,无法继续发送给 bianbao1@hwmail.vip。
IP地址 1.116.113.85 已经发送了最大数量的邮件,无法继续发送给 chenghongbin3@hwmail.vip。
zhouyouguang@hwmail.vip 已发送邮件给 chensongling3@hwmail.vip。
IP地址 1.116.113.85 已经发送了最大数量的邮件,无法继续发送给 dadaxiao1@hwmail.vip。
IP地址 101.33.61.199 已经发送了最大数量的邮件,无法继续发送给 dadaxiao2@hwmail.vip。
IP地址 1.116.113.85 已经发送了最大数量的邮件,无法继续发送给 dadaxiao3@hwmail.vip。
IP地址 1.116.113.85 已经发送了最大数量的邮件,无法继续发送给 dadaxiao4@hwmail.vip。
import csv
import smtplib
from email.message import EmailMessage
# 从CSV文件读取邮箱、密码和服务器地址
with open('123.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
next(reader) # 跳过标题行
accounts = [(row[0], row[1], row[2], row[3]) for row in reader]
# 从文本文件读取邮件地址列表
with open('mailaddress.txt', 'r') as txtfile:
recipients = [line.strip() for line in txtfile]
# 读取html文件内容
with open('html.html', 'r', encoding='utf-8') as htmlfile:
html_content = htmlfile.read()
# 设置每个邮箱的发送次数限制
max_emails_per_account = 2
emails_sent_per_account = {account[0]: 0 for account in accounts}
# 循环遍历收件人列表,逐个发送邮件
for recipient in recipients:
# 选择下一个可用的邮箱账号
account = None
for a in accounts:
if emails_sent_per_account[a[0]] < max_emails_per_account:
account = a
break
if account is None:
print('所有邮箱账号的发送次数已经达到限制,无法发送更多邮件。')
break
# 使用SMTP协议登录邮箱服务器
smtp_server = account[3]
smtp_port = 25 # 默认SMTP端口
smtp_username = account[0]
smtp_password = account[2]
smtp_name = account[1]
smtp_conn = smtplib.SMTP(smtp_server, smtp_port)
smtp_conn.ehlo()
smtp_conn.starttls()
smtp_conn.login(smtp_username, smtp_password)
# 设置邮件内容
message = EmailMessage()
message.set_content(html_content.replace("{{from_address}}", smtp_name), subtype='html')
# 设置邮件头部信息
message['From'] = smtp_username
message['To'] = recipient
message['Subject'] = '邮件主题'
# 发送邮件
from_address = smtp_username
to_address = recipient
smtp_conn.send_message(message)
print(f' {from_address} 已发送邮件给 {to_address}。')
# 更新发送次数计数器
emails_sent_per_account[account[0]] += 1
# 断开SMTP连接
smtp_conn.quit()
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 xieaoao@qq.com QQ:1296454177