簡體   English   中英

如何使用 python smtplib 將 email 發送給多個收件人?

[英]How to send email to multiple recipients using python smtplib?

經過大量搜索,我找不到如何使用 smtplib.sendmail 發送給多個收件人。 問題是每次發送郵件時,郵件標頭似乎都包含多個地址,但實際上只有第一個收件人會收到 email。

問題似乎是email.Message模塊期望與smtplib.sendmail() function 不同的東西。

簡而言之,要發送給多個收件人,您應該將 header 設置為逗號分隔的 email 地址字符串。 但是, sendmail()參數to_addrs應該是 email 地址的列表。

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

確實有效,我花了很多時間嘗試多種變體。

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

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

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

盡管recipientssendmail(sender, recipients, message)需要是一個列表:

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

您需要了解電子郵件的可見地址和遞送地址之間的區別。

msg["To"]本質上是打印在信件上的內容。 它實際上沒有任何影響。 除了您的電子郵件客戶端,就像普通郵遞員一樣,會假設這是您想要發送電子郵件的人。

然而,實際交付的工作可能大不相同。 因此,您可以將電子郵件(或副本)放入完全不同的人的郵箱中。

這有多種原因。 例如轉發 To:標題字段在轉發時不會更改,但是電子郵件會被放入不同的郵箱。

smtp.sendmail命令現在負責實際交付。 email.Message只是信件的內容,而不是遞送。

在低級SMTP ,您需要一個一個地提供收件人,這就是為什么地址列表(不包括姓名!)是合理的 API。

對於標題,它還可以包含例如名稱,例如To: First Last <email@addr.tld>, Other User <other@mail.tld> 因此,不推薦使用您的代碼示例,因為它將無法傳遞此郵件,因為僅將其拆分為,您仍然沒有有效的地址!

這個對我有用。

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

下面的解決方案對我有用。 它成功地向多個收件人發送了一封電子郵件,包括“抄送”和“密件抄送”。

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)

所以實際上問題是 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())

我嘗試了下面的方法,它就像一個魅力:)

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

我想出了這個可導入的模塊功能。 它在本例中使用 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()

幾個月前我想通了這一點,並寫了博客 總結是:

如果要使用smtplib向多個收件人發送電子郵件,請使用email.Message.add_header('To', eachRecipientAsString)來添加它們,然后在調用sendmail 方法時, use email.Message.get_all('To')將消息發送給所有人。 抄送和密送收件人也是如此。

我使用 python 3.6,以下代碼適用於我

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

好吧,這個 asnwer方法中的方法對我不起作用。 我不知道,也許這是一個 Python3(我使用的是 3.4 版本)或與 gmail 相關的問題,但經過一些嘗試,對我有用的解決方案是

s.send_message(msg)

代替

s.sendmail(sender, recipients, msg.as_string())

這里有很多技術上或部分正確的答案。 在閱讀了每個人的答案后,我想出了一個更可靠/通用的電子郵件功能。 我已經確認它有效,您可以為正文傳遞 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+

當您在文本文件上寫接收電子郵件時,您可以嘗試此操作

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

嘗試將所有收件人和 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()

這是一個老問題。 我發布新答案的主要原因是解釋如何使用 Python 3.6+ 中的現代email庫解決問題,以及它與舊版本有何不同; 但首先,讓我們回顧一下 Anony-Mousse 在2012 年的回答中所寫的內容。

SMTP 根本不關心標題中的內容。 您傳遞給sendmail方法的收件人列表實際上決定了郵件將被傳遞到哪里。

在 SMTP 用語中,這稱為消息的信封。 在協議級別,您連接到服務器,然后告訴它消息來自誰( MAIL FROM: SMTP 動詞)以及將其發送給誰( RCPT TO: ),然后單獨傳輸消息本身( DATA )與標頭和身體作為一個傾斜的字符串 blob。

現代smtplib通過提供一個send_message方法來簡化 Python 方面,該方法實際上發送到消息標題中指定的收件人。

現代email庫提供了一個EmailMessage object,它取代了您過去必須使用的所有各種單獨的 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: header 取決於郵件服務器。 如果您想真正確定收件人彼此不可見,也許根本不要放置Bcc: header,並像以前使用sendmail那樣單獨枚舉信封中的信封收件人( send_message讓您也這樣做,但如果您只想發送給標題中指定的收件人,則不必這樣做)。

這顯然向一個 go 中的所有收件人發送了一條消息。 如果您要向很多人發送相同的信息,這通常是您應該做的。 但是,如果每條消息都是唯一的,您將需要遍歷收件人並為每個消息創建和發送一條新消息。 (僅僅希望將收件人的姓名和地址放在To: header 可能不足以保證發送比要求更多的消息,但當然,有時您在正文中也有每個收件人的唯一內容。)

對於那些希望僅使用一個“收件人”header 發送消息的人,下面的代碼解決了這個問題。 確保您的接收器變量是字符串列表。

# 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"}

要將 email 發送給多個收件人,請將接收者添加為 email 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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM