简体   繁体   English

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

[英]Personalised batch email with Mailgun in Laravel

I'm looking to send personalised batch e-mails to a large number of users.我希望向大量用户发送个性化的批处理电子邮件。 By this I mean that I would like to set up a template e-mail and inject each user's information into it before sending it.我的意思是我想设置一个模板电子邮件,并在发送之前将每个用户的信息注入其中。

Of course, this can easily be achieved with Laravel by looping through user data and using the Mailer (Or Mail facade) methods (Such as send , raw , queue etc.):当然,这可以通过 Laravel 通过循环用户数据和使用 Mailer(或Mail门面)方法(例如sendrawqueue等)轻松实现:

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

However, considering the volume of e-mails I would like to send, this would be far too slow for my needs.但是,考虑到我要发送的电子邮件量,这对于我的需要来说太慢了。 After some research I have discovered that Mailgun supports sending personalised batch e-mails using their API.经过一些研究,我发现 Mailgun 支持使用他们的 API 发送个性化的批处理电子邮件。 From their website:从他们的网站:

Batch sending批量发送

With a single API call, you can send up to 1000 fully personalized emails.通过一次 API 调用,您最多可以发送 1000 封完全个性化的电子邮件。

Mailgun will properly assemble the MIME message and send the email to each of your users individually. Mailgun 将正确组合 MIME 消息并将电子邮件分别发送给您的每个用户。 That makes sending large volumes of email a lot faster and a lot less resource intensive.这使得发送大量电子邮件的速度更快,资源占用更少。

  • I was wondering if Laravel supports personalised batch e-mail sending in this way?我想知道 Laravel 是否支持以这种方式发送个性化的批量电子邮件? I haven't managed to find anything in the documentation or the code to support this.我还没有在文档或代码中找到任何支持这一点的内容。
  • Are there any existing packages available for Laravel to support this? Laravel 是否有可用的现有软件包来支持这一点?

Of course, I could happily implement this using Mailgun's API directly or using any available SDKs, but just wanted to check to see if it is supported by Laravel first.当然,我可以很高兴地直接使用 Mailgun 的 API 或使用任何可用的 SDK 来实现它,但只是想先检查一下 Laravel 是否支持它。

Here's how I solved an identical situation since I couldn't find any ready made solution.这是我解决相同情况的方法,因为我找不到任何现成的解决方案。

        $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;
        }

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

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