要批量发送邮件,可以使用Python的smtplib库来实现。以下是一个简单的示例代码,演示如何使用smtplib库批量发送邮件:
import smtplibfrom email.mime.text import MIMEText# 配置发件人信息sender = 'sender@example.com'password = 'password'# 配置收件人列表recipients = ['recipient1@example.com', 'recipient2@example.com']# 配置邮件内容subject = 'Test Email'body = 'This is a test email.'# 创建邮件对象message = MIMEText(body, 'plain')message['Subject'] = subjectmessage['From'] = sender# 连接到SMTP服务器smtp_server = 'smtp.example.com'smtp_port = 587smtp = smtplib.SMTP(smtp_server, smtp_port)smtp.starttls()smtp.login(sender, password)# 发送邮件给每个收件人for recipient in recipients: message['To'] = recipient smtp.sendmail(sender, recipient, message.as_string())# 断开与SMTP服务器的连接smtp.quit()
在上述示例代码中,需要配置发件人的邮箱地址和密码、收件人列表、SMTP服务器的地址和端口。然后创建邮件对象,设置邮件主题、内容和发件人信息。接下来,通过循环将邮件发送给每个收件人,并最后断开与SMTP服务器的连接。
请注意,使用smtplib库发送邮件需要配置发件人的邮箱地址和密码,以便进行SMTP认证。另外,SMTP服务器的地址和端口需要根据你使用的邮件服务提供商进行配置。