How to send emails in bulk using Python?
To send emails in bulk, you can use the smtplib library in Python. Here is a simple example code demonstrating how to use the smtplib library to send emails in bulk.
import smtplib
from 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'] = subject
message['From'] = sender
# 连接到SMTP服务器
smtp_server = 'smtp.example.com'
smtp_port = 587
smtp = 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()
In the above example code, you need to configure the sender’s email address and password, the recipient’s list, the SMTP server’s address and port. Then create a mail object, set the mail subject, content, and sender information. Next, send the email to each recipient through a loop, and finally disconnect from the SMTP server.
Please note that using the smtplib library to send emails requires configuring the sender’s email address and password for SMTP authentication. Additionally, the address and port of the SMTP server need to be configured according to your email service provider.