我在这里做错了什么?只有一个电子邮件地址接收电子邮件,而不是另一个。因此my@email.com
会收到它,yours@email.com
不会收到它。
#!/usr/bin/python3
import smtplib, ssl
import email.message
import email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
def sendfile(subject, body_text, f_name):
"""
Sends e-mail with attachment.
"""
msg = MIMEMultipart()
msg['From'] = 'him@email.com'
msg['To'] = 'my@email.com,yours@email.com'
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(body_text))
part = MIMEBase('application', "octet-stream")
part.set_payload(open(f_name, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename={}'.format(f_name))
msg.attach(part)
smtp_obj = smtplib.SMTP("localhost")
smtp_obj.sendmail(msg['From'], [msg['To']], msg.as_string())
smtp_obj.quit()
答案 0 :(得分:0)
如果您要发送到多个地址,msg['To']
应该是每个地址的列表:
msg['To'] = ['my@email.com', 'yours@email.com']
当您致电sendmail()
时,由于msg['To']
已经是一个列表,请不要将其括在方括号中。
答案 1 :(得分:0)
我可以使用以下代码发送电子邮件。
recipients = ['my@email.com','yours@email.com'
msg['To'] = ", ".join(recipients)
smtp_obj.sendmail(msg['From'], recipients, msg.as_string())