簡體   English   中英

使用 Python 向不同的收件人發送電子郵件

[英]Sending E-Mail to various recipients with Python

我想使用代碼中的給定表格向各種收件人(如 10 人)發送 email,但郵件僅到達第一個郵件地址。 有沒有辦法編碼它,我可以將 email 發送給各種收件人?

df = pd.DataFrame(Table)

filename = str(date.today()) + ".png"

#dir = pathlib.Path(__file__).parent.absolute()

folder = r"/results/"

#path_plot = str(dir) + folder + filename

from_mail = "abcdef@gmail.com"
from_password = cred.passwort 
to_mail = 'example1@hotmail.com,example2@live.com, example2@yahoo.com, example2@yahoo.de'
smtp_server = "smtp.gmail.com"
smtp_port = 465
def send_email( smtp_server, smtp_port, from_mail, from_password, to_mail):
    '''
        Send results via mail
    '''
 
    msg = MIMEMultipart()
    msg['Subject'] = 'Results'
    msg['From'] = from_mail
    COMMASPACE = ', '
    msg['To'] = COMMASPACE.join([from_mail, to_mail])
    msg.preamble = 'Something special'
    
    html = """\
    <html>
    <head></head>
    <body>
    {0}
    </body>
    </html>
    """.format(df.to_html())
    part1 = MIMEText(html, 'html')
    msg.attach(part1)
    
    server = smtplib.SMTP_SSL(smtp_server, smtp_port)
    server.ehlo()
    server.login(from_mail, from_password)
    server.sendmail(from_mail, [from_mail, to_mail], msg.as_string())
    server.quit()
send_email( smtp_server, smtp_port, from_mail, from_password, to_mail)

如果要發送給多個收件人,則to_addr參數應該是字符串列表。

您的代碼中的直接問題是您正在將from_addr與(字符串或列表) to_addr其中,您應該在其中創建一個列表以放入收件人字段中。

順便說一句,您的代碼似乎是為 Python 3.5 或更早版本編寫的。 email庫在 3.6 中進行了大修,現在更加通用和合乎邏輯。 可能扔掉你所擁有的,然后email文檔中的示例重新開始。 這是一個基本的重構(只是快速而骯臟;結構相當奇怪 - 為什么您將一些字符串作為參數傳遞,而其他是 function 之外的全局變量?)。 它消除了代碼中的一些問題,僅僅是因為現代 API 刪除了許多舊版本所必需的樣板。

import pandas as pd # I'm guessing ...?
from email.message import EmailMessage

...
df = pd.DataFrame(Table)

# Commenting out unused variables
# filename = str(date.today()) + ".png"
# folder = "/results/"  # no need for an r string, no backslashes here

from_mail = "abcdef@gmail.com"
from_password = cred.passwort 
to_mail = ['example1@hotmail.com', 'example2@live.com', 'example2@yahoo.com', 'example2@yahoo.de']

smtp_server = "smtp.gmail.com"
smtp_port = 465

def send_email(smtp_server, smtp_port, from_mail, from_password, to_mail):
    '''
        Send results via mail
    '''
 
    msg = EmailMessage()
    msg['Subject'] = 'Results'
    msg['From'] = from_mail
    msg['To'] = ', '.join(to_mail + [from_mail])
    # Don't muck with the preamble, especially if you don't understand what it is
    
    html = """\
    <html>
    <head></head>
    <body>
    {0}
    </body>
    </html>
    """.format(df.to_html())
    message.set_contents(html, 'html')
    
    with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
        server.ehlo()
        server.login(from_mail, from_password)
        # Some servers require a second ehlo() here

        # Use send_message instead of legacy sendmail method
        server.send_message(message)
        server.quit()

send_email(smtp_server, smtp_port, from_mail, from_password, to_mail)

生成的 HTML 仍然很可怕,但我們只希望沒人看消息源。

暫無
暫無

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

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