0

I'm about to integrate into a project some bulk email features using mandrill and djrill 1.3.0 with django 1.7, since I'm sending html content I'm using the following approach:

from django.core.mail import get_connection

connection = get_connection()
to = ['testaddress1@example.com', 'testaddress1@example.com'] 
for recipient_email in to:
    # I perform some controls and register some info about the user and email address
    subject = u"Test subject for %s" % recipient_email
    text = u"Test text for email body"
    html = u"<p>Test text for email body</p>"
    from_email = settings.DEFAULT_FROM_EMAIL
    msg = EmailMultiAlternatives(
        subject, text, from_email, [recipient_email])
    msg.attach_alternative(html, 'text/html')
    messages.append(msg)
# Bulk send
send_result = connection.send_messages(messages)

At this point, send_result is a int and it equals to the number of sent (pushed to mandrill) messages.

I need to get the mandrill response for every sent message to process the mandrill_response['msg']['_id'] value and some other stuff.

djrill provided 'send_messages' connection method uses a _send call and it is adding mandrill_response to every message but is just returning True if success.

So, do you know how to get mandrill response for every message when sending bulk html emails with djrill?

Gabriel
  • 1,014
  • 9
  • 14
  • I'd simply suggest skipping djrill, which offers standard limited Django API, and using underlying Mandrill Python libraries directly. – Mikko Ohtamaa May 06 '15 at 11:23
  • Using python mandrill library the only approach is to loop and send the messages one by one and get the response for every api call, am I right? If I am, the best approach is to user msg.send() since this is returning the mandrill response as expected. What do U think? – Gabriel May 06 '15 at 15:24
  • Unless Mandrill API documentation offer any other methods then yes. – Mikko Ohtamaa May 06 '15 at 15:33

1 Answers1

0

Djrill attaches a mandrill_response attribute to each EmailMessage object as it's sent. See Mandrill response in the Djrill docs.

So after you send the messages, you can examine that property on each object in the messages list you sent. Something like:

# Bulk send
send_result = connection.send_messages(messages)

for msg in messages:
   if msg.mandrill_response is None:
       print "error sending to %r" % msg.to
   else:
       # there's one response for each recipient of the msg
       # (because an individual message can have multiple to=[..., ...])
       for response in msg.mandrill_response:
           print "Message _id %s, to %s, status %s" % (
               response['_id'], response['email'], response['status'])

>>> Message _id abc123abc123abc123abc123abc123, to testaddress1@example.com, status sent
>>> ...
medmunds
  • 5,950
  • 3
  • 28
  • 51