带有Mandrill到多个电子邮件ID的Python邮件

时间:2014-02-20 14:11:29

标签: python mandrill

使用Mandrill向多个emailId发送电子邮件,但它只传递到列表中第一个要停止的ID,而不发送。我想使用mandrill API向多个用户发送邮件

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 = 'xyz@gmail.com'
        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'] = "xyz@gmail.com"
#msgDic['to'] = 'abc@gmail.com','example@gmail.com'
COMMASPACE = ', '
family =  ['abc@gmail.com','example@gmail.com']
msgDic['to'] = COMMASPACE.join(family)

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

2 个答案:

答案 0 :(得分:1)

由于您正在使用smtplib,因此您需要查看有关如何指定多个收件人的SMTP库的文档。 SMTP库处理多个收件人的方式各不相同。看起来这个StackOverflow帖子有关于使用smtplib传递多个收件人的信息:How to send email to multiple recipients using python smtplib?

你需要一个列表而不仅仅是字符串,所以像这样:

msgDic['to'] = ['abc@gmail.com','example@gmail.com']

所以,你的家庭变量被正确宣布,你不应该做任何事情。

答案 1 :(得分:0)

尝试:

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

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

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

您可以省略nametype