3

我正在使用Mailgun向客户列表发送付款提醒。

我最初的解决方案是使用他们的 REST API 向一组收件人发送电子邮件(批量发送)。像这样:

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;
}

(对于 ToJSON() 自定义扩展,请参阅此博客文章

这很好用。但是,现在我想跟踪打开。文档指出,使用 webhook(当用户打开电子邮件时获取 POST 数据的简单网页)我可以获得有关此操作的大量信息,包括一些很酷的数据,例如地理位置。

有两个值看起来很有希望:标签自定义变量。但是,这两个值都将在创建请求时包含在内,但在批量发送中这些值不起作用(它们将识别批量发送本身,而不是单个电子邮件。)

有没有办法使用 Mailgun API 为一批发送的每封电子邮件添加标识变量?我已经求助于发送单独的电子邮件,但是如果我必须将电子邮件发送到一个大列表,那将非常不方便。

4

1 回答 1

5

此问题的解决方案是使用自定义变量参数 ( v:.) 稍后可以通过设置 webhook 来捕获这些参数。在我的例子cupon-id中,我的 webhook 中的 POST 参数将包含cupon自定义变量的值。

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

请注意,根据文档,这些自定义变量必须是 JSON 字符串。

于 2013-10-24T16:13:16.497 回答