python怎么实现批量发送邮件

32次阅读
没有评论

共计 956 个字符,预计需要花费 3 分钟才能阅读完成。

要批量发送邮件,可以使用 Python 的 smtplib 库来实现。以下是一个简单的示例代码,演示如何使用 smtplib 库批量发送邮件:

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()

在上述示例代码中,需要配置发件人的邮箱地址和密码、收件人列表、SMTP 服务器的地址和端口。然后创建邮件对象,设置邮件主题、内容和发件人信息。接下来,通过循环将邮件发送给每个收件人,并最后断开与 SMTP 服务器的连接。

请注意,使用 smtplib 库发送邮件需要配置发件人的邮箱地址和密码,以便进行 SMTP 认证。另外,SMTP 服务器的地址和端口需要根据你使用的邮件服务提供商进行配置。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2024-02-05发表,共计956字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)