在python脚本中提供多个电子邮件收件人

时间:2014-12-26 20:12:14

标签: python email

我正在使用python电子邮件程序,当我有1个收件人时可以查找但是当我有2个收件人时失败。

有人可以帮我解决这个问题吗?

Herr的代码:

#Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = subject
me = "user1@com"
recepient = "user1@hotmailcom;user2@hotmail.com"
msg['From'] = me
msg['To'] = ''.join(recepient)

2 个答案:

答案 0 :(得分:2)

电子邮件收件人必须逗号分开,不能使用分号;这只是微软的惯例:

recepient = "user1@hotmailcom,user2@hotmail.com"
msg['To'] = recepient

当您已经一个字符串时,您不需要使用str.join()

如果您有收件人列表,那么使用str.join()会有意义:

recepients = ["user1@hotmailcom", "user2@hotmail.com"]
msg['To'] = ', '.join(recepients)

答案 1 :(得分:0)

用逗号替换分号:

recipient = "user1@hotmailcom;user2@hotmail.com"
msg['To'] = recipient.replace(';', ', ')
显示地址的

The docs have examples应以逗号分隔:

COMMASPACE = ', '
# family = the list of all recipients' email addresses
msg['To'] = COMMASPACE.join(family)