0

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)
kadamb
  • 1,532
  • 3
  • 29
  • 55
  • What is your problem exactly? – RickyA Feb 20 '14 at 14:13
  • when we hit the mandrill api for single email id then it send to that email address but we want to send email to multiple email address by passing in the list all the email id.when i try for it then mail is sent only to first email id in the list – kadamb Feb 20 '14 at 14:15
  • Are you using their python api? – RickyA Feb 20 '14 at 14:18
  • yes, from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText msg = MIMEMultipart('alternative') and username password also given – kadamb Feb 20 '14 at 14:20

2 Answers2

1

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.

Community
  • 1
  • 1
Kaitlin
  • 6,167
  • 32
  • 30
0

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.

RickyA
  • 15,465
  • 5
  • 71
  • 95
  • This is true, if you're sending with the API client. However, looks like he's using an SMTP library instead of the official API wrapper for Python, so that structure may not work. – Kaitlin Feb 20 '14 at 17:42