Leo*_*era 3 c# api batch-processing mailgun
我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)
(有关 ToJSON() 自定义扩展,请参阅此博客文章)
这效果很好。但是,现在我想跟踪打开情况。文档指出,使用 webhook(一个简单的网页,当用户打开电子邮件时获取 POST 数据)我可以获得有关此操作的大量信息,包括一些很酷的数据,例如地理位置。
有两个值看起来很有前途:tag和custom Variables。但是,这两个值都将在创建请求时包含在内,但在批量发送中这些值不起作用(它们将识别批量发送本身,而不是单个电子邮件。)
有没有办法使用 Mailgun API 向批量发送的每封电子邮件添加标识变量?我已经采取了发送个人电子邮件的方式,但如果我必须向一个大列表发送电子邮件,那就非常不方便。
此问题的解决方案是使用自定义变量参数 ( 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);
Run Code Online (Sandbox Code Playgroud)
请注意,根据文档,这些自定义变量必须是 JSON 字符串。