Laravel 中使用 Mailgun 的个性化批量电子邮件

Personalised batch email with Mailgun in Laravel

我希望向大量用户发送个性化 批量电子邮件。我的意思是我想设置一个模板电子邮件,并在发送之前将每个用户的信息注入其中。

当然,通过遍历用户数据并使用 Mailer(或 Mail 门面)方法(例如 send、[=14],这可以通过 Laravel 轻松实现=], queue 等):

foreach ($users as $user) {
    $data = ['user' => $user];
    $this->mailer->queue($views, $data, function($message) use($user) {
        $message->to($user->email, $user->name);
    });
}

但是,考虑到我要发送的电子邮件数量,这对我的需求来说太慢了。经过一些研究,我发现 Mailgun 支持使用他们的 API 发送个性化的批量电子邮件。来自他们的网站:

Batch sending

With a single API call, you can send up to 1000 fully personalized emails.

Mailgun will properly assemble the MIME message and send the email to each of your users individually. That makes sending large volumes of email a lot faster and a lot less resource intensive.

当然,我可以很高兴地直接使用 Mailgun 的 API 或使用任何可用的 SDK 来实现它,但只是想先检查 Laravel 是否支持它。

这是我解决相同情况的方法,因为我找不到任何现成的解决方案。

        $subscribers = Subscriber::active()->get();
        $batch = 0;
        $batch_subscribers = array();
        $batch_subscribers_data = array();
        foreach ($subscribers as $subscriber)
        {
            $batch_subscribers[] = $subscriber->mail;
            $batch_subscribers_data[$subscriber->mail] = array(
                "id" => $subscriber->id,
                "mail" => $subscriber->mail,
                "name" => $subscriber->name
            );
            $batch++;
            if($batch < 999){
                continue;
            }
            $input['to'] = $batch_subscribers;
            $input['vars'] = $batch_subscribers_data;
            Mailgun::send('email/email-base', ['input' => $input],
                function ($message) use ($input) 
                {
                    $message->subject($input['asunto']);
                    $message->to($input['to']);
                    $message->replyTo("reply@address.com");
                    $message->recipientVariables($input['vars']);
                });
            $batch_subscribers = array();
            $batch_subscribers_data = array();
            $batch = 0;
        }

致所有仍想使用 Laravel Mailable 的人,

您可以覆盖 Mailgun 传输 class 以调整负载:

  /**
   * Get the HTTP payload for sending the Mailgun message.
   *
   * @param  \Swift_Mime_SimpleMessage  $message
   * @param  string  $to
   * @return array
   */
  protected function payload(Swift_Mime_SimpleMessage $message, $to)
  {
    // use laravel official mailgun transport when
    // batch_sending in config not set, or
    // only 1 recipient
    if (! $this->batchSending || count($message->getTo()) === 1) {
      return parent::payload($message, $to);
    }

    // batch sending
    $ret = [
      'auth' => [
        'api',
        $this->key,
      ],
      'multipart' => [
        [
          'name' => 'message',
          'contents' => str_replace(
            $message->getHeaders()->get('to')->toString(),
            'To: %recipient%'.PHP_EOL,
            $message->toString()
          ),
          'filename' => 'message.mime',
        ],
      ],
    ];

    $recipients = [];
    foreach ($message->getTo() as $address => $name) {
      $ret['multipart'][] = [
        'name' => 'to',
        'contents' => "$name <$address>",
      ];

      $recipients[$address] = [
        'name' => $name,
      ];
    }

    $ret['multipart'][] = [
      'name' => 'recipient-variables',
      'contents' => json_encode($recipients),
    ];

    return $ret;
  }

我刚刚为此创建了一个包https://github.com/leondeng/laravel-batch-mailgun,请尝试一下,如果有问题请反馈给我,谢谢!