最近要用python写邮件给几个客户发结果,根据找到的例子造轮子,但是每次只能发给一个人,就是列表里第一个人,
比如 tolist=['[email protected]', '[email protected]', '[email protected]']
根据例子写的代码如下:
msg = MIMEMultipart()
msg['from'] = fromuser
msg['subject'] = subject
msg['to'] = ','.join(tolist)
发送时用
server.sendmail(msg['from'], msg['to'], msg.as_string())
我尝试了许多方法,无论是直接用
msg['to'] = ','.join(tolist)
msg['to'] = ', '.join(tolist)
msg['to'] = ';'.join(tolist)
msg['to'] = '[email protected], [email protected], [email protected]'
都是一样的结果,即邮件发给了第一个人
最后在 http://stackoverflow.com/questions/20509427/python-not-sending-email-to-multiple-addresses 找到结果
python的smtplib发送email给多人要传列表而不是','.join(LIST),所以修改发送的部分就成了
server.sendmail(msg['from'], to_list, msg.as_string())
而不在构造msg时增加'to'字段
_(:з」∠)_