我写了下面的代码,用于向多个地址发送电子邮件....但我能够只发送邮件列表中的第一个地址...可以给我确切的理由和解决方案。在此先感谢!!
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
COMMASPACE = ', '
msg = MIMEMultipart()
msg['Subject'] = 'Test attaching mail'
msg['From'] = 'x@x.com'
msg['Reply-to'] = ''
msg['To'] = COMMASPACE.join(['x1@x.com','x2@x.com','x3@x.com'])
# That is what u see if dont have an email reader:
msg.preamble = 'Multipart massage.\n'
# This is the textual part:
part = MIMEText("Hello im sending an email from a python program")
msg.attach(part)
# This is the binary part(The Attachment):
file="../logs_usecase/TestUsecase.log"
part = MIMEApplication(open(file,"rb").read())
part.add_header('Content-Disposition', 'attachment', filename=file)
msg.attach(part)
# Create an instance in SMTP server
smtp = SMTP("smtp.gmail.com:587")
# Start the server:
smtp.starttls()
smtp.ehlo()
smtp.login('x@x.com', "xxxxx")
# Send the email
smtp.sendmail(msg['From'], msg['To'], msg.as_string())
答案 0 :(得分:0)
SMTP.sendmail
预计a list of recipients,但您传递的是带有连接电子邮件地址的字符串:
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
发送邮件。所需的参数是RFC 822来自地址字符串, 列表 的RFC 822到地址字符串(裸字符串将被视为具有1个地址的列表),和消息字符串。
(强调补充)
您应该将与COMMASPACE
加入的相同列表直接传递给SMTP.sendmail
,而不是传递连接的扁平字符串。