簡體   English   中英

使用Mailgun批量發送和單獨的消息ID

[英]Batch sending and individual message ID with 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;
}

(對於ToJSON()自定義擴展,請參閱此博客文章

這很有效。 但是,現在我想跟蹤打開。 文檔指出使用webhook(一個用戶打開電子郵件時獲取POST數據的簡單網頁),我可以獲得有關此操作的大量信息,包括一些很酷的數據,如地理位置。

有兩個看似有希望的值: 標記自定義變量 但是,這兩個值都將在創建請求時包含在內,但在批量發送時這些值不起作用(它們將識別批量發送本身,而不是單個電子郵件。)

有沒有辦法使用Mailgun API在一批發送中為每個電子郵件添加標識變量? 我已經使用發送個人電子郵件,但如果我必須將電子郵件發送到一個大清單,那將非常不方便。

此問題的解決方案是使用自定義變量參數( v: :。)稍后可以通過設置webhook來捕獲這些參數。 在我的例子中,我的webhook中的cupon-id POST參數將包含cupon自定義變量的值。

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

請注意, 根據文檔 ,這些自定義變量必須是JSON字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM