Question

Email With Mandrill to multiple emailId but it only deliver to id which is first in the list to rest it does not send.I want to send mail to multiple users using 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() 

and here i am calling the function

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)
Was it helpful?

Solution

Since you're using smtplib, you'll want to review the documentation for that SMTP library on how you specify multiple recipients. SMTP libraries vary in how they handle multiple recipients. Looks like this StackOverflow post has information about passing multiple recipients with smtplib: How to send email to multiple recipients using python smtplib?

You need a list instead of just strings, so something like this:

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

So, your family variable is declared properly, and you shouldn't need to do anything to that.

OTHER TIPS

Try:

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

From the docs it looks like they expect this structure:

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

where you can omit name and type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top