Question

I'm using Mailgun to send payment reminders to a list of customers.

My initial solution was to use their REST API to send an email to a group of recipients (batch sending.) Something like this:

public static bool SendBatch(string From, Dictionary<string, Object> vars, string txtEmail, string htmlEmail)
{
    RestClient client = new RestClient();
    client.BaseUrl = "https://api.mailgun.net/v2";
    client.Authenticator =
            new HttpBasicAuthenticator("api",
                                       "my-mailgun-key");
    RestRequest request = new RestRequest();
    request.AddParameter("domain", "my-domain.tld.mailgun.org", ParameterType.UrlSegment);
    request.Resource = "{domain}/messages";
    request.AddParameter("from", From);
    foreach (KeyValuePair<string, Object> entry in vars)
    {
        request.AddParameter("to", entry.Key);
    }
    request.AddParameter("subject", "Payment option ready");
    request.AddParameter("text", txtEmail);
    request.AddParameter("html", htmlEmail);
    request.AddParameter("recipient-variables", vars.ToJSON());
    request.Method = Method.POST;
    client.Execute(request);
    return true;
}

(For the ToJSON() custom extension see this blog post)

This worked great. However, now I want to track opens. The documentation states that using a webhook (a simple web page that get POST data when a user opens an email) I can get a nice deal of information on this action, including some cool data like geo location.

There are two values that seem promising: tag and custom variables. However, both of these values are to be included at the moment of creating the request, but in batch sending these don't work (they will identify the batch send itself, not individual emails.)

Is there any way to add identification variables to every email in one batch send using the Mailgun API? I've resorted to send individual emails but that's highly inconvenient if I have to send emails to a big list.

Was it helpful?

Solution

The solution to this problem was to use a custom variable parameter (v:.) These can be later captured by setting up a webhook. In my case, the cupon-id POST parameter in my webhook would contain the value for the cupon custom variable.

request.AddParameter("recipient-variables", vars.ToJSON());
request.AddParameter("v:cupon-id", "{cupon:\"%recipient.cupon%\"}");
request.AddParameter("o:tracking", true);

Note that these custom variables must be JSON strings according to the documentation.

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