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

当前回答

要向多个收件人发送电子邮件,请将收件人添加为电子邮件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')

其他回答

我使用python 3.6和以下代码为我工作

email_send = 'xxxxx@xxx.xxx,xxxx@xxx.xxx'
server.sendmail(email_user,email_send.split(','),text)    

我提出了这个可导入模块函数。本例中使用的邮件服务器为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) + '\n' + 'From: ' + gmail_user + '\n' + 'Subject: ' + subject + '\n'
    msg = header + '\n' + subject + '\n\n'
    smtpserver.sendmail(gmail_user, to, msg)
    smtpserver.close()

这对我很管用。

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())

您可以在将收件人电子邮件写入文本文件时尝试此操作

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')

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

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

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

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

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

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

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