Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Message-Id with Mandrill for bulk emails

Tags:

mandrill

I am sending emails to lists of contacts based on templates using Mandrill. I would like to track if the contacts have replied to my email and, to do so, I would like to check whether the Message-Id of my sent emails appears in the In-Reply-To header field of new messages.

The problem is that I have to generate and set the Message-Id manually since Mandrill only gives me their internal _id. However, since I am sending emails to various contacts at the same time, I set preserve_recipients to false. But then I can only set one Message-Id, which will therefore become not globally unique.

Here is an example JSON that I'm sending:

{
"from_email": "[email protected]",
"from_name": "Its Me",
"headers": {"Message-Id": ["<[email protected]>"]},
"subject": "Thesubject",
"text": "Thebody",
"to": [
    {
        "email": "[email protected]",
        "name": "John",
        "type": "to"
    },
    {
        "email": "[email protected]",
        "name": "Patrick",
        "type": "to"
    }
],
"preserve_recipients": false

}

In this case, two messages will be sent, but they'll have the same Message-Id. If I don't set it, Mandrill will automatically assign one, but then I can't retrieve it.

Any idea what I could do ? Maybe my whole approach is incorrect...

like image 245
Victor Avatar asked May 28 '15 16:05

Victor


1 Answers

I ended up looping over all the recipients and generating a new Message-Id at each iteration and send one email at a time. Probably not optimal since I'm not using Mandrill bulk capability, but at least now I can store the email id.

import email
import mandrill

mandrill_client = mandrill.Mandrill('YOUR_MANDRILL_KEY')

for recipient in recipients:
    # Generate RFC 2822-compliant Message-ID header
    message_id = email.Utils.make_msgid()
    m = {
        "headers": {"Message-Id": [message_id],
        "from_email": "[email protected]",
        "from_name": "Its Me",
        "subject": "The subject",
        "text": "The body",
        "to": [{"email": recipient["email"],
                "name": recipient["name"],
                "type": "to"}],
        "track_clicks": True,
        "track_opens": True
    }
    result = mandrill_client.messages.send(message=m)
like image 63
Victor Avatar answered Sep 28 '22 00:09

Victor