问题
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": "itsme@email.com",
"from_name": "Its Me",
"headers": {"Message-Id": ["<20150528161426.4265.93582@email.com>"]},
"subject": "Thesubject",
"text": "Thebody",
"to": [
{
"email": "john@email.com",
"name": "John",
"type": "to"
},
{
"email": "patrick@email.com",
"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...
回答1:
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": "itsme@email.com",
"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)
回答2:
From mandrill documentation you can retrieve the _id from the return value of the message.
来源:https://stackoverflow.com/questions/30512212/set-message-id-with-mandrill-for-bulk-emails