简体   繁体   English

使用Mailgun批量发送和单独的消息ID

[英]Batch sending and individual message ID with Mailgun

I'm using Mailgun to send payment reminders to a list of customers. 我正在使用Mailgun向客户列表发送付款提醒。

My initial solution was to use their REST API to send an email to a group of recipients ( batch sending .) Something like this: 我最初的解决方案是使用他们的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;
}

(For the ToJSON() custom extension see this blog post ) (对于ToJSON()自定义扩展,请参阅此博客文章

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. 文档指出使用webhook(一个用户打开电子邮件时获取POST数据的简单网页),我可以获得有关此操作的大量信息,包括一些很酷的数据,如地理位置。

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? 有没有办法使用Mailgun API在一批发送中为每个电子邮件添加标识变量? I've resorted to send individual emails but that's highly inconvenient if I have to send emails to a big list. 我已经使用发送个人电子邮件,但如果我必须将电子邮件发送到一个大清单,那将非常不方便。

The solution to this problem was to use a custom variable parameter ( v: .) These can be later captured by setting up a webhook. 此问题的解决方案是使用自定义变量参数( v: :。)稍后可以通过设置webhook来捕获这些参数。 In my case, the cupon-id POST parameter in my webhook would contain the value for the cupon custom variable. 在我的例子中,我的webhook中的cupon-id POST参数将包含cupon自定义变量的值。

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 . 请注意, 根据文档 ,这些自定义变量必须是JSON字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM