我尝试使用此方法向多个收件人发送电子邮件,但是它不起作用,只有列表中的第一个收件人会收到该电子邮件。可能是什么问题?
预先感谢, 劳力
import smtplib
import email
import datetime as dt
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
recipients = ['x@gmail.com', 'y@gmail.com']
fromaddr = "a@gmail.com"
pwd='xxx'
file= "asdf.pdf"
now = dt.datetime.now()
now = dt.date.strftime(now, '%Y-%m-%d')
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = ', '.join(recipients)
msg['Subject'] = "Global Market Overview"
body = "Daily Report of: " + now
msg.attach(MIMEText(body, 'plain'))
filename = "Global Market Overview.pdf"
attachment = open("Global Market Overview.pdf", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, pwd)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
print('Email sent')