如何发送给多个收件人

时间:2013-09-03 06:29:52

标签: python

我正在尝试使用以下电子邮件向多个收件人发送电子邮件,但它只发送到第一封电子邮件,知道为什么以及如何发送给多个收件人?

from email.mime.text import MIMEText
from smtplib import SMTP

def email (body,subject,SendToList):
    msg = MIMEText("%s" % body, 'html')
    msg['Content-Type'] = "text/html; charset=UTF8"
    msg['Subject'] = subject
    s = SMTP('localhost',25)
    s.sendmail('fromuserid@company.com', SendToList,msg=msg.as_string())

def main ():
    SendToList = 'userid1@company.com,userid2@company.com'
    with open('email.txt', 'r') as f:
        body = f.read()
    subject = "test email"
    email(body,subject,SendToList)
    print "Done"

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:4)

来自documentation

  

SMTP.sendmail(from_addr,to_addrs,msg [,mail_options,rcpt_options])

     

发送邮件。必需参数是RFC 822 from-address字符串,    RFC 822到地址字符串的列表(裸字符串将被视为   一个包含1个地址的列表)和一个消息字符串。

所以你需要发一份清单:

def main ():
    send_to_list = ['userid1@company.com','userid2@company.com']