2014-02-20 49 views
0

电子邮件山魈多个EMAILID但它只提供给ID是列表中的第休息不send.I希望将邮件发送给多个用户使用山魈APIPython的邮件与山魈到多个电子邮件ID

here is my code : 

class mandrillClass: 
    def mandrillMail(self,param): 

     import smtplib 
     from email.mime.multipart import MIMEMultipart 
     from email.mime.text import MIMEText 
     msg = MIMEMultipart('alternative') 
     msg['Subject'] = param['subject'] 
     msg['From'] = param['from'] 
     msg['To']  = param['to'] 
     html = param['message'] 
     print html 
     text = 'dsdsdsdds' 



     part1 = MIMEText(text, 'plain') 
     part2 = MIMEText(html, 'html') 
     username = '[email protected]' 
     password = 'uiyhiuyuiyhihuohohio' 

     msg.attach(part1) 
     msg.attach(part2) 

     s = smtplib.SMTP('smtp.mandrillapp.com', 587) 

     s.login(username, password) 
     s.sendmail(msg['From'], msg['To'], msg.as_string()) 

     s.quit() 

我在这里调用函数

from API_Program import mandrillClass 
msgDic = {} 
msgDic['subject'] = "testing" 
msgDic['from'] = "[email protected]" 
#msgDic['to'] = '[email protected]','[email protected]' 
COMMASPACE = ', ' 
family = ['[email protected]','[email protected]'] 
msgDic['to'] = COMMASPACE.join(family) 

msgDic['message'] = "<div>soddjags</div>" 
mailObj = mandrillClass()     
mailObj.mandrillMail(msgDic) 
+0

究竟是什么问题? – RickyA

+0

当我们点击mandrill api查找单个电子邮件地址时,它会发送到该电子邮件地址,但我们希望通过在列表中传递所有电子邮件地址将电子邮件发送到多个电子邮件地址。当我尝试使用时,邮件仅发送给第一个列表中的电子邮件ID – kadamb

+0

您是否正在使用他们的python api? – RickyA

回答

1

由于您使用的是smtplib,因此您需要查看该SMTP库的文档,了解如何指定多个收件人。 SMTP库在处理多个收件人的方式上有所不同。貌似这个StackOverflow的帖子有关于通过多个收件人用的smtplib信息:How to send email to multiple recipients using python smtplib?

你需要一个列表,而不是只是字符串的,所以这样的事情:

msgDic['to'] = ['[email protected]','[email protected]']

所以,你的家人变量是正确声明,你不需要为此做任何事情。

0

尝试:

msgDic['to'] = [{"email":fam} for fam in family] 

docs,它看起来像他们期望这种结构:

msgDic['to'] = [ {"email":"[email protected]", "name":"a.b", "type":"to"}, 
       {"email":"[email protected]", "name":"x.z", "type":"cc"} 
       ] 

在那里你可以省略nametype

+0

这是真的,如果你使用API​​客户端发送的话。但是,看起来他正在使用SMTP库而不是Python的官方API封装器,因此这种结构可能无法正常工作。 – Kaitlin