如何发送给多个收件人

2024-06-26 14:27:10 发布

您现在位置: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()

Tags: textfromimportcommainemaildefbody
1条回答
网友
1楼 · 发布于 2024-06-26 14:27:10

documentation

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

Send mail. The required arguments are an RFC 822 from-address string, a list of RFC 822 to-address strings (a bare string will be treated as a list with 1 address), and a message string.

所以你需要给它发一份清单:

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

相关问题 更多 >