经过大量的搜索,我无法找到如何使用smtplib。发送邮件到多个收件人。问题是每次发送邮件时,邮件标题似乎包含多个地址,但实际上只有第一个收件人会收到电子邮件。

问题似乎出在邮件上。Message模块期望与smtplb .sendmail()函数不同的内容。

简而言之,要发送给多个收件人,您应该将标题设置为一串以逗号分隔的电子邮件地址。sendmail()参数to_addr应该是一个电子邮件地址列表。

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib

msg = MIMEMultipart()
msg["Subject"] = "Example"
msg["From"] = "me@example.com"
msg["To"] = "malcom@example.com,reynolds@example.com,firefly@example.com"
msg["Cc"] = "serenity@example.com,inara@example.com"
body = MIMEText("example email body")
msg.attach(body)
smtp = smtplib.SMTP("mailhost.example.com", 25)
smtp.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
smtp.quit()

当前回答

msg['To']需要是一个字符串:

msg['To'] = "a@b.com, b@b.com, c@b.com"

而sendmail中的收件人(sender,收件人,message)需要是一个列表:

sendmail("a@a.com", ["a@b.com", "b@b.com", "c@b.com"], "Howdy")

其他回答

尝试声明一个包含所有收件人和cc_收件人的列表变量为字符串,而不是循环遍历它们,如下所示:

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib

recipients = ["malcom@example.com","reynolds@example.com", "firefly@example.com"]
cc_recipients=["serenity@example.com", "inara@example.com"]
msg = MIMEMultipart()
msg["Subject"] = "Example"
msg["From"] = "me@example.com"
msg["To"] = ', '.join(recipients)
msg["Cc"] = ', '.join(cc_recipients)
body = MIMEText("example email body")
msg.attach(body)
smtp = smtplib.SMTP("mailhost.example.com", 25)
for recipient in recipients:
    smtp.sendmail(msg["From"], recipient, msg.as_string())
for cc_recipient in cc_recipients:
    smtp.sendmail(msg["From"], cc_recipient, msg.as_string())
smtp.quit()

对于那些希望只发送一个“to”报头的消息,下面的代码可以解决这个问题。确保你的接收者变量是一个字符串列表。

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = title
msg['From'] = f'support@{config("domain_base")}'
msg['To'] =  "me"
message_content += f"""
    <br /><br />
    Regards,<br />
    Company Name<br />
    The {config("domain_base")} team
"""
body = MIMEText(message_content, 'html')
msg.attach(body)

try:
    smtpObj = smtplib.SMTP('localhost')
    for r in receivers:
        del msg['To']
        msg['To'] =  r #"Customer /n" + r
        smtpObj.sendmail(f"support@{config('domain_base')}", r, msg.as_string())
    smtpObj.quit()      
    return {"message": "Successfully sent email"}
except smtplib.SMTPException:
    return {"message": "Error: unable to send email"}

msg['To']需要是一个字符串:

msg['To'] = "a@b.com, b@b.com, c@b.com"

而sendmail中的收件人(sender,收件人,message)需要是一个列表:

sendmail("a@a.com", ["a@b.com", "b@b.com", "c@b.com"], "Howdy")

要向多个收件人发送电子邮件,请将收件人添加为电子邮件id列表。

接收者= ['user1@email.com', 'user2@email.com', 'user3@email.com']

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

smtp_server = 'smtp-example.com'
port = 26 
sender = 'user@email.com'
debuglevel = 0

# add receivers as list of email id string
receivers = ['user1@email.com', 'user2@email.com', 'user3@email.com']

message = MIMEMultipart(
    "mixed", None, [MIMEImage(img_data, 'png'), MIMEText(html,'html')])
    message['Subject'] = "Token Data"
    message['From'] = sender
    message['To'] = ", ".join(receivers)
    try:

        server = smtplib.SMTP('smtp-example.com')
        server.set_debuglevel(1)
        server.sendmail(sender, receivers, message.as_string())
        server.quit()
        # print(response)

    except BaseException:
        print('Error: unable to send email')

你需要了解电子邮件的可见地址和发送地址之间的区别。

msg[“To”]本质上是打印在信件上的内容。它实际上没有任何影响。除了你的电子邮件客户端,就像普通的邮政人员一样,会假设这是你想要发送电子邮件的人。

然而,实际的交付可能会有很大的不同。所以你可以把邮件(或副本)投到完全不同的人的邮箱里。

这有很多原因。例如转发。在转发时,To:报头字段不会改变,但电子邮件会被放入不同的邮箱。

smtp。Sendmail命令现在负责实际的传递。电子邮件。信息只是信件的内容,而不是传递方式。

在低级SMTP中,您需要一个接一个地提供接收者,这就是为什么地址列表(不包括姓名!)是合理的API。

对于标头,它还可以包含例如名称,例如To: First Last <email@addr.tld>, Other User <other@mail.tld>。因此,不建议使用您的代码示例,因为它将无法传递此邮件,因为仅通过拆分它,您仍然没有获得有效地址!