2013-12-11 168 views
4

我不明白我在哪裏出錯了,我希望有人可以發現問題。我想發送一封電子郵件到多個地址;但是,它只會將它發送到列表中的第一個電子郵件地址,而不是兩者。這裏是代碼:Python不發送電子郵件到多個地址

import smtplib 
from smtplib import SMTP 

recipients = ['[email protected]', '[email protected]'] 

def send_email (message, status): 
    fromaddr = '[email protected]' 
    toaddrs = ", ".join(recipients) 
    server = SMTP('smtp.gmail.com:587') 
    server.ehlo() 
    server.starttls() 
    server.ehlo() 
    server.login('example_username', 'example_pw') 
    server.sendmail(fromaddr, toaddrs, 'Subject: %s\r\n%s' % (status, message)) 
    server.quit() 

send_email("message","subject") 

有沒有人遇到過這個錯誤?

謝謝你的時間。

+0

可能重複http://stackoverflow.com/questions/8729071/is-there-any-way-to-add-multiple-接收機功能於蟒-的smtplib) –

回答

8

嘗試使用此代碼,沒有你的加入:

import smtplib 
from smtplib import SMTP 

recipients = ['[email protected]', '[email protected]'] 

def send_email (message, status): 
    fromaddr = '[email protected]' 
    server = SMTP('smtp.gmail.com:587') 
    server.ehlo() 
    server.starttls() 
    server.ehlo() 
    server.login('example_username', 'example_pw') 
    server.sendmail(fromaddr, recipients, 'Subject: %s\r\n%s' % (status, message)) 
    server.quit() 

send_email("message","subject") 

希望它能幫助!

5

變化

toaddrs = ", ".join(recipients) 

toaddrs = recipients 

因爲

server.sendmail(fromaddr, toaddrs, ...) 

預計toaddrs是電子郵件地址列表。 (或者,當然,只是代替toaddrs使用recipients

5
import smtplib 

    from email.mime.text import MIMEText 

    s = smtplib.SMTP('xxx.xx') 

    msg = MIMEText("""body""") 
    sender = 'xx.xx.com' 

    recipients = ['[email protected]', '[email protected]'] 

    msg['Subject'] = "test" 
    msg['From'] = sender 
    msg['To'] = ", ".join(recipients) 
    s.sendmail(sender, recipients, msg.as_string()) 
的[有沒有辦法在Python中添加的smtplib多個接收器?(
相關問題