Mailgun API:批量发送与个人呼叫

Mailgun API: Batch Sending vs. Individual Calls

背景

我们正在构建一个将通过 Mailgun 处理和发送电子邮件的应用程序。这些有时是一次性消息,由交易发起。不过,有些电子邮件会一次发送到 30k+。

例如,给所有会员的时事通讯。

注意事项

Mailgun 提供 Batch Sending 选项及其 API。使用 "Recipient Variables",您可以包含与特定用户配对的动态值。

但是,此批量发送功能是有限的。您不能为每个请求发送超过 1,000 个收件人,这意味着我们必须为每组 1,000 个收件人列表(在我们的数据库中)进行迭代。 Mailgun provides an example 这可能是如何工作的,使用 Python(向下滚动约 2/3)。

问题

批量发送(即通过单个 API 调用向一组收件人发送电子邮件,使用收件人变量)与制作我们自己的循环、变量替换和个人 API 来电?

我认为这会给我们的服务器带来更多负担,因为它会自己处理每条消息,而不是仅仅将所有数据卸载到 Mailgun 的服务器上,让他们完成繁重的工作。但我也喜欢在我们这边处理它并向 Mailgun 发送 "fully-rendered" 消息的灵活性和简单性,一次一个,而不必一次迭代 1k。

关于最佳实践的任何想法或我们应该考虑的注意事项?

今天偶然发现了这个,感觉它为我原来的问题提供了一个很好的 summary/answer。我想 post 这个作为答案,以防其他人有这个问题并且没有找到这个 Mailgun post。也直接从马嘴里说出来。简而言之版本:

对于PHP,至少SDK有一个Mailgun class,有一个BatchMessage()方法。这实际上会为您处理收件人的计数,因此您可以根据需要排队任意数量的电子邮件地址(即超过 1k),Mailgun 将根据需要发送到 API 端点。很光滑!

这是他们原来的措辞,加上 link 到页面。
Sending a message with Mailgun PHP SDK + Batch Message:

Batch Message

In addition to Message Builder, we have Batch Message. This class allows you to build a message and submit a template message in batches, up to 1,000 recipients per post. The benefit of using this class is that the recipients tally is monitored and will automatically submit the message to the endpoint when you've added the 1,000th recipient. This means you can build your message and begin iterating through your database. Forget about sending the message, the SDK will keep track of posting to the API when necessary.

// First, instantiate the SDK with your API credentials and define your domain.

$mgClient = new Mailgun("key-example");
$domain = "example.com";

// Next, instantiate a Message Builder object from the SDK, pass in your sending domain.  

$batchMsg = $mgClient->BatchMessage($domain);

// Define the from address.

$batchMsg->setFromAddress("dwight@example.com", 
                          array("first"=>"Dwight", "last" => "Schrute"));
// Define the subject. 

$batchMsg->setSubject("Help!");

// Define the body of the message.

$batchMsg->setTextBody("The printer is on fire!");

// Next, let's add a few recipients to the batch job.
$batchMsg->addToRecipient("pam@example.com", 
                          array("first" => "pam", "last" => "Beesly"));
$batchMsg->addToRecipient("jim@example.com", 
                          array("first" => "Jim", "last" => "Halpert"));
$batchMsg->addToRecipient("andy@example.com", 
                          array("first" => "Andy", "last" => "Bernard"));
// ...etc...etc...
// After 1,000 recipeints, 
// Batch Message will automatically post your message to the messages endpoint. 

// Call finalize() to send any remaining recipients still in the buffer.

$batchMsg->finalize();

@cdwyer 和@nikoshr 的回答很有帮助,但有点遗留。示例中使用的方法已弃用。这是 lib 的当前用法:


    $batchMessage = $this->mailgun->messages()->getBatchMessage('mydomain.com');


    $batchMessage->setFromAddress('user@domain.com');
    $batchMessage->setReplyToAddress('user2@domain.com');
    $batchMessage->setSubject('Contact form | Company');
    $batchMessage->setHtmlBody('<html>...</html>');

    foreach ($recipients as $recipient) {
        $batchMessage->addToRecipient($recipient);
    }

    $batchMessage->finalize();

更多信息请见 documentation