经过大量的搜索,我无法找到如何使用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()

当前回答

这是一个老问题。我发布新答案的主要原因是解释如何用Python 3.6+中的现代电子邮件库解决问题,以及它与旧版本的区别;但首先,让我们回顾一下Anony-Mousse在2012年的回答。

SMTP根本不关心头文件中有什么。您传递给sendmail方法的收件人列表实际上决定了消息将被传递到哪里。

在SMTP术语中,这称为消息的信封。在协议级别上,您连接到服务器,然后告诉它消息来自谁(MAIL from: SMTP动词)以及将消息发送给谁(RCPT to:),然后分别传输消息本身(DATA),其标题和正文作为一个斜向字符串blob。

现代smtplib通过提供send_message方法简化了Python方面的工作,该方法实际发送给消息头中指定的收件人。

现代电子邮件库提供了一个EmailMessage对象,它取代了所有不同的MIME类型,过去您必须使用这些MIME类型从较小的部分组装消息。您可以添加附件,而不需要单独构造它们,如果需要,还可以构建各种更复杂的多部分结构,但通常不必这样做。只需创建一条消息并填充您想要的部分。

注意,下面有大量注释;总的来说,新的EmailMessage API比旧的API更简洁、更通用。

from email.message import EmailMessage

msg = EmailMessage()

# This example uses explicit strings to emphasize that
# that's what these header eventually get turned into
msg["From"] = "me@example.org"
msg["To"] = "main.recipient@example.net, other.main.recipient@example.org"
msg["Cc"] = "secondary@example.com, tertiary@example.eu"
msg["Bcc"] = "invisible@example.int, undisclosed@example.org.au"
msg["Subject"] = "Hello from the other side"

msg.set_content("This is the main text/plain message.")
# You can put an HTML body instead by adding a subtype string argument "html"
# msg.set_content("<p>This is the main text/html message.</p>", "html")

# You can add attachments of various types as you see fit;
# if there are no other parts, the message will be a simple
# text/plain or text/html, but Python will change it into a
# suitable multipart/related or etc if you add more parts
with open("image.png", "rb") as picture:
    msg.add_attachment(picture.read(), maintype="image", subtype="png")

# Which port to use etc depends on the mail server.
# Traditionally, port 25 is SMTP, but modern SMTP MSA submission uses 587.
# Some servers accept encrypted SMTP_SSL on port 465.
# Here, we use SMTP instead of SMTP_SSL, but pivot to encrypted
# traffic with STARTTLS after the initial handshake.
with smtplib.SMTP("smtp.example.org", 587) as server:
    # Some servers insist on this, others are more lenient ...
    # It is technically required by ESMTP, so let's do it
    # (If you use server.login() Python will perform an EHLO first
    # if you haven't done that already, but let's cover all bases)
    server.ehlo()
    # Whether or not to use STARTTLS depends on the mail server
    server.starttls()
    # Bewilderingly, some servers require a second EHLO after STARTTLS!
    server.ehlo()
    # Login is the norm rather than the exception these days
    # but if you are connecting to a local mail server which is
    # not on the public internet, this might not be useful or even possible
    server.login("me.myself@example.org", "xyzzy")

    # Finally, send the message
    server.send_message(msg)

Bcc:标题的最终可见性取决于邮件服务器。如果你真的想确保收件人之间是不可见的,也许根本就不要使用Bcc:头,并且在信封中单独列举信封中的收件人,就像你以前在sendmail中必须做的那样(send_message也允许你这样做,但如果你只是想发送给头中指定的收件人,你就不必这样做)。

This obviously sends a single message to all recipients in one go. That is generally what you should be doing if you are sending the same message to a lot of people. However, if each message is unique, you will need to loop over the recipients and create and send a new message for each. (Merely wishing to put the recipient's name and address in the To: header is probably not enough to warrant sending many more messages than required, but of course, sometimes you have unique content for each recipient in the body, too.)

其他回答

这里有很多答案在技术上或部分上是正确的。在阅读了每个人的答案后,我想出了这个更可靠/通用的电子邮件功能。我已经确认它的工作,你可以通过HTML或纯文本的主体。注意,此代码不包括附件代码:

import smtplib
import socket

# Import the email modules we'll need
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

#
# @param [String] email_list
# @param [String] subject_line
# @param [String] error_message
def sendEmailAlert(email_list="default@email.com", subject_line="Default Subject", error_message="Default Error Message"):
    hostname = socket.gethostname()
    # Create message
    msg = MIMEMultipart()
    msg['Subject'] = subject_line
    msg['From'] = f'no-reply@{hostname}'
    msg['To'] = email_list
    msg.attach(MIMEText(error_message, 'html'))
    # Send the message via SMTP server
    s = smtplib.SMTP('localhost') # Change for remote mail server!
    # Verbose debugging
    s.set_debuglevel(2)
    try:
        s.sendmail(msg['From'], msg['To'].split(","), msg.as_string())
    except Exception as e:
        print(f'EMAIL ISSUE: {e}')
    s.quit()

这显然可以修改为使用本机Python日志记录。我只是提供了一个坚实的核心功能。我也不能强调这一点,sendmail()需要一个列表,而不是一个字符串!函数适用于Python3.6+

几个月前我发现了这一点,并在博客上发表了相关文章。总结如下:

如果您想使用smtplib向多个收件人发送电子邮件,请使用email. message。add_header('To', eachRecipientAsString)来添加它们,然后当您调用sendmail方法时,使用email.Message.get_all('To')将消息发送给所有它们。抄送和密送收件人也是如此。

下面的解决方案对我很有效。它成功地向多个收件人发送电子邮件,包括“抄送”和“密件抄送”。

toaddr = ['mailid_1','mailid_2']
cc = ['mailid_3','mailid_4']
bcc = ['mailid_5','mailid_6']
subject = 'Email from Python Code'
fromaddr = 'sender_mailid'
message = "\n  !! Hello... !!"

msg['From'] = fromaddr
msg['To'] = ', '.join(toaddr)
msg['Cc'] = ', '.join(cc)
msg['Bcc'] = ', '.join(bcc)
msg['Subject'] = subject

s.sendmail(fromaddr, (toaddr+cc+bcc) , message)

这是一个老问题。我发布新答案的主要原因是解释如何用Python 3.6+中的现代电子邮件库解决问题,以及它与旧版本的区别;但首先,让我们回顾一下Anony-Mousse在2012年的回答。

SMTP根本不关心头文件中有什么。您传递给sendmail方法的收件人列表实际上决定了消息将被传递到哪里。

在SMTP术语中,这称为消息的信封。在协议级别上,您连接到服务器,然后告诉它消息来自谁(MAIL from: SMTP动词)以及将消息发送给谁(RCPT to:),然后分别传输消息本身(DATA),其标题和正文作为一个斜向字符串blob。

现代smtplib通过提供send_message方法简化了Python方面的工作,该方法实际发送给消息头中指定的收件人。

现代电子邮件库提供了一个EmailMessage对象,它取代了所有不同的MIME类型,过去您必须使用这些MIME类型从较小的部分组装消息。您可以添加附件,而不需要单独构造它们,如果需要,还可以构建各种更复杂的多部分结构,但通常不必这样做。只需创建一条消息并填充您想要的部分。

注意,下面有大量注释;总的来说,新的EmailMessage API比旧的API更简洁、更通用。

from email.message import EmailMessage

msg = EmailMessage()

# This example uses explicit strings to emphasize that
# that's what these header eventually get turned into
msg["From"] = "me@example.org"
msg["To"] = "main.recipient@example.net, other.main.recipient@example.org"
msg["Cc"] = "secondary@example.com, tertiary@example.eu"
msg["Bcc"] = "invisible@example.int, undisclosed@example.org.au"
msg["Subject"] = "Hello from the other side"

msg.set_content("This is the main text/plain message.")
# You can put an HTML body instead by adding a subtype string argument "html"
# msg.set_content("<p>This is the main text/html message.</p>", "html")

# You can add attachments of various types as you see fit;
# if there are no other parts, the message will be a simple
# text/plain or text/html, but Python will change it into a
# suitable multipart/related or etc if you add more parts
with open("image.png", "rb") as picture:
    msg.add_attachment(picture.read(), maintype="image", subtype="png")

# Which port to use etc depends on the mail server.
# Traditionally, port 25 is SMTP, but modern SMTP MSA submission uses 587.
# Some servers accept encrypted SMTP_SSL on port 465.
# Here, we use SMTP instead of SMTP_SSL, but pivot to encrypted
# traffic with STARTTLS after the initial handshake.
with smtplib.SMTP("smtp.example.org", 587) as server:
    # Some servers insist on this, others are more lenient ...
    # It is technically required by ESMTP, so let's do it
    # (If you use server.login() Python will perform an EHLO first
    # if you haven't done that already, but let's cover all bases)
    server.ehlo()
    # Whether or not to use STARTTLS depends on the mail server
    server.starttls()
    # Bewilderingly, some servers require a second EHLO after STARTTLS!
    server.ehlo()
    # Login is the norm rather than the exception these days
    # but if you are connecting to a local mail server which is
    # not on the public internet, this might not be useful or even possible
    server.login("me.myself@example.org", "xyzzy")

    # Finally, send the message
    server.send_message(msg)

Bcc:标题的最终可见性取决于邮件服务器。如果你真的想确保收件人之间是不可见的,也许根本就不要使用Bcc:头,并且在信封中单独列举信封中的收件人,就像你以前在sendmail中必须做的那样(send_message也允许你这样做,但如果你只是想发送给头中指定的收件人,你就不必这样做)。

This obviously sends a single message to all recipients in one go. That is generally what you should be doing if you are sending the same message to a lot of people. However, if each message is unique, you will need to loop over the recipients and create and send a new message for each. (Merely wishing to put the recipient's name and address in the To: header is probably not enough to warrant sending many more messages than required, but of course, sometimes you have unique content for each recipient in the body, too.)

对于那些希望只发送一个“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"}