Skip to main content
 首页 » 编程设计

python之为什么我不能使用此脚本向多个收件人发送电子邮件

2025年05月04日29mfrbuaa

为什么我不能使用此脚本向多个收件人发送电子邮件?

我没有收到任何错误或退回邮件,第一个收件人确实收到了电子邮件。其他人都没有。

脚本:

#!/usr/bin/python 
import smtplib 
 
SMTP_SERVER = 'smtp.gmail.com' 
SMTP_PORT = 587 
 
recipient = 'email@domain.com; email2@domain.com;' 
sender = 'me@gmail.com' 
subject = 'the subject' 
body = 'the body' 
password = "password" 
username = "me@gmail.com" 
 
body = "" + body + "" 
 
headers = ["From: " + sender, 
           "Subject: " + subject, 
           "To: " + recipient, 
           "MIME-Version: 1.0", 
           "Content-Type: text/html"] 
headers = "\r\n".join(headers) 
 
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) 
 
session.ehlo() 
session.starttls() 
session.ehlo 
session.login(username, password) 
 
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body) 
session.quit() 

请您参考如下方法:

分号不是收件人标题中地址的正确分隔符。您必须使用逗号。

编辑:我现在看到您使用的库不正确。您提供的字符串将始终被解释为单个地址。您必须提供要发送给多个收件人的地址列表。