如何使用 python smtplib 向多个收件人发送电子邮件?
- 2025-01-20 09:07:00
- admin 原创
- 139
问题描述:
经过一番搜索,我还是找不到如何使用 smtplib.sendmail 向多个收件人发送邮件。问题是每次发送邮件时,邮件标题似乎包含多个地址,但实际上只有第一个收件人会收到电子邮件。
问题似乎是email.Message
模块期望的东西与功能不同smtplib.sendmail()
。
简而言之,要发送给多个收件人,您应该将标题设置为以逗号分隔的电子邮件地址字符串。但是,sendmail()
参数to_addrs
应该是电子邮件地址列表。
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()
解决方案 1:
这确实有效,我花了很多时间尝试多种变体。
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = ['john.doe@example.com', 'john.smith@example.co.uk']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(sender, recipients, msg.as_string())
解决方案 2:
需要msg['To']
是一个字符串:
msg['To'] = "a@b.com, b@b.com, c@b.com"
虽然recipients
insendmail(sender, recipients, message)
需要是一个列表:
sendmail("a@a.com", ["a@b.com", "b@b.com", "c@b.com"], "Howdy")
解决方案 3:
您需要了解电子邮件的可见地址和递送地址之间的区别。
msg["To"]
基本上就是信件上印刷的内容。它实际上没有任何作用。只不过你的电子邮件客户端,就像普通邮递员一样,会假设这就是你想要发送电子邮件的对象。
但实际投递方式可能大不相同。因此,您可以将电子邮件(或副本)投递到完全不同的人的邮箱中。
造成这种情况的原因有很多。例如转发。To:
转发时标头字段不会改变,但电子邮件会被放入不同的邮箱。
命令smtp.sendmail
现在负责实际交付。email.Message
仅是信件的内容,而不是交付。
在低级别中SMTP
,您需要逐一提供收件人,这就是为什么地址列表(不包括姓名!)是合理的 API。
对于标题,它还可以包含例如名称,例如To: First Last <email@addr.tld>, Other User <other@mail.tld>
。因此,不推荐使用您的代码示例,因为它将无法传递此邮件,因为仅通过拆分它,
您仍然没有有效的地址!
解决方案 4:
它对我有用。
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = 'john.doe@example.com,john.smith@example.co.uk'
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = recipients
s.sendmail(sender, recipients.split(','), msg.as_string())
解决方案 5:
以下解决方案对我有用。它成功地将电子邮件发送给多个收件人,包括“抄送”和“密送”。
toaddr = ['mailid_1','mailid_2']
cc = ['mailid_3','mailid_4']
bcc = ['mailid_5','mailid_6']
subject = 'Email from Python Code'
fromaddr = 'sender_mailid'
message = "
!! 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)
解决方案 6:
所以实际上的问题是 SMTP.sendmail 和 email.MIMEText 需要两个不同的东西。
email.MIMEText 设置电子邮件正文的“收件人:”标头。它仅用于向另一端的人显示结果,并且像所有电子邮件标头一样,必须是单个字符串。(请注意,它实际上与实际接收消息的人没有任何关系。)
另一方面,SMTP.sendmail 为 SMTP 协议设置消息的“信封”。它需要一个 Python 字符串列表,每个字符串都有一个地址。
因此,您需要做的是合并收到的两个回复。将 msg['To'] 设置为单个字符串,但将原始列表传递给 sendmail:
emails = ['a.com','b.com', 'c.com']
msg['To'] = ', '.join( emails )
....
s.sendmail( msg['From'], emails, msg.as_string())
解决方案 7:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def sender(recipients):
body = 'Your email content here'
msg = MIMEMultipart()
msg['Subject'] = 'Email Subject'
msg['From'] = 'your.email@gmail.com'
msg['To'] = (', ').join(recipients.split(','))
msg.attach(MIMEText(body,'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('your.email@gmail.com', 'yourpassword')
server.send_message(msg)
server.quit()
if __name__ == '__main__':
sender('email_1@domain.com,email_2@domain.com')
它只对我有用,使用 send_message 函数并使用收件人列表中的 join 函数,python 3.6。
解决方案 8:
我尝试了下面的方法,效果很好:)
rec_list = ['first@example.com', 'second@example.com']
rec = ', '.join(rec_list)
msg['To'] = rec
send_out = smtplib.SMTP('localhost')
send_out.sendmail(me, rec_list, msg.as_string())
解决方案 9:
我想出了这个可导入模块函数。它在本例中使用了 gmail 电子邮件服务器。它分为标题和消息,因此您可以清楚地看到发生了什么:
import smtplib
def send_alert(subject=""):
to = ['email@one.com', 'email2@another_email.com', 'a3rd@email.com']
gmail_user = 'me@gmail.com'
gmail_pwd = 'my_pass'
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + ", ".join(to) + '
' + 'From: ' + gmail_user + '
' + 'Subject: ' + subject + '
'
msg = header + '
' + subject + '
'
smtpserver.sendmail(gmail_user, to, msg)
smtpserver.close()
解决方案 10:
我使用 python 3.6,以下代码对我有用
email_send = 'xxxxx@xxx.xxx,xxxx@xxx.xxx'
server.sendmail(email_user,email_send.split(','),text)
解决方案 11:
几个月前我弄清楚了这一点,并在博客中发表了这篇文章。摘要如下:
如果您想使用 smtplib 向多个收件人发送电子邮件,请使用email.Message.add_header('To', eachRecipientAsString)
添加他们,然后在调用 sendmail 方法时use email.Message.get_all('To')
将邮件发送给所有收件人。抄送和密送收件人也一样。
解决方案 12:
好吧,这个 asnwer方法中的方法对我来说不起作用。我不知道,也许这是一个 Python3(我使用的是 3.4 版本)或 gmail 相关的问题,但经过几次尝试,对我有用的解决方案是
s.send_message(msg)
而不是
s.sendmail(sender, recipients, msg.as_string())
解决方案 13:
这是一个老问题。我发布新答案的主要原因是解释如何使用email
Python 3.6+ 中的现代库解决问题以及它与旧版本有何不同;但首先,让我们回顾一下 Anony-Mousse 在2012 年的答案中所写的内容。
sendmail
SMTP 根本不关心标头中的内容。您传递给该方法的收件人列表实际上决定了邮件的投递位置。
在 SMTP 术语中,这称为邮件的信封。在协议级别,您连接到服务器,然后告诉它邮件来自谁(MAIL FROM:
SMTP 动词)以及要发送给谁(RCPT TO:
),然后单独传输邮件本身(DATA
),邮件头和正文作为一个不透明的字符串 blob。
现代通过提供一种实际上发送给消息头中指定的收件人的方法,smtplib
简化了 Python 方面的事情。send_message
现代email
库提供了一个EmailMessage
对象,它取代了过去您必须使用的各种单独的 MIME 类型来从较小的部分组装消息。您可以添加附件而无需单独构建它们,并且可以根据需要构建各种更复杂的多部分结构,但通常您不必这样做。只需创建一条消息并填充所需的部分即可。
请注意,以下内容有大量注释;总体而言,新API比旧 APIEmailMessage
更简洁、更通用。
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
也允许您这样做,但如果您只想发送给标头中指定的收件人,则不必这样做)。
这显然会一次性向所有收件人发送一条消息。如果您要向很多人发送同一条消息,通常应该这样做。但是,如果每条消息都是唯一的,则需要遍历收件人并为每个收件人创建和发送一条新消息。(仅仅希望在标题中写上收件人的姓名和地址To:
可能不足以保证发送比所需更多的消息,但当然,有时您在正文中也会为每个收件人提供独特的内容。)
解决方案 14:
尝试声明一个列表变量,其中包含所有收件人和 cc_recipients 作为字符串,然后循环遍历它们,如下所示:
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()
解决方案 15:
你可以在文本文件中写收件人电子邮件时尝试此操作
from email.mime.text import MIMEText
from email.header import Header
import smtplib
f = open('emails.txt', 'r').readlines()
for n in f:
emails = n.rstrip()
server = smtplib.SMTP('smtp.uk.xensource.com')
server.ehlo()
server.starttls()
body = "Test Email"
subject = "Test"
from = "me@example.com"
to = emails
msg = MIMEText(body,'plain','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = Header(from, 'utf-8')
msg['To'] = Header(to, 'utf-8')
text = msg.as_string()
try:
server.send(from, emails, text)
print('Message Sent Succesfully')
except:
print('There Was An Error While Sending The Message')
解决方案 16:
这里有很多答案在技术上或部分正确。在阅读了所有人的答案后,我想出了一个更可靠/通用的电子邮件功能。我已确认它可以正常工作,您可以传递 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()
想要一个List
而不是一个String
!函数适用于 Python3.6+
解决方案 17:
对于那些希望只发送一个“收件人”标题的消息的人来说,下面的代码可以解决这个问题。确保您的接收者变量是一个字符串列表。
# 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"}
解决方案 18:
要向多个收件人发送电子邮件,请将收件人添加为电子邮件 ID 列表。
receivers = ['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')
扫码咨询,免费领取项目管理大礼包!