【问题标题】:Laravel: How to Queue mails to send laterLaravel:如何排队邮件稍后发送
【发布时间】:2014-06-23 21:46:21
【问题描述】:

我正在尝试使用 Mail::queue 发送和发送电子邮件,但是当我调用此函数时,它会简单地发送邮件,并且响应会延迟......我认为使用 Mail::queue 的目的是排队....

我希望响应即时到来,不必等待发送电子邮件

例如

Mail::queue('emails.template', $data, function($message) {
    $message->to('somemail@gmail.com');
    $message->subject('Notificacion');
});

return Response::json(array('error' => 0, 'message' => 'Ok'));

我想在不等待邮件发送的情况下接收响应。 我该怎么做???

【问题讨论】:

  • Mail::later 方法做同样的事情,它只是等待我告诉它等待的任何时间并且响应被延迟
  • 这几乎是不可能的。您将从执行操作的服务器获得响应。
  • @ gabrielmatusevich我可以知道你是否得到任何关于这个的解决方案?我在使用 laravel5 时也遇到了同样的问题

标签: php laravel queue sendmail


【解决方案1】:

您使用的是什么队列驱动程序 (app/config/queue.php - 'default' param)?如果您使用的是sync,并且没有设置其他任何一个,那么您使用的是同步驱动程序,它的功能正如其名称所说:一旦创建任务就运行您的排队任务。

你需要为 Laravel 配置一个 MQ 服务器来与之通信。你可以为此获取一个免费的 Iron.io 帐户,然后你需要对其进行配置,例如:

'iron' => array(
    'driver'  => 'iron',
    'project' => 'iron-io-project-id',
    'token'   => 'iron-io-queue-token',
    'queue'   => 'queue-name',
),

然后当您使用Mail::queue() 时,它会将指令推送到iron.io。然后,您必须让另一个线程监听队列 - 只需运行 php artisan queue:listen 并在消息推送到队列时让它运行。

【讨论】:

  • 这真的很有帮助,但我实际上正在寻找一些方法来在本地执行此操作而不依赖于其他服务:P
  • beantalkd 呢?你有它的示例配置吗?
  • 使用 iron.io 确实是设置它的最快方法。对于 beanstalkd,一旦你安装了它,你应该能够使用配置的默认值 - 只需将第 18 行的 default 更改为 beanstalkd
【解决方案2】:
 /**
 * Get all email recipients and include their user details for Mailgun's
 * template tags - %recipient.userToken%
 */
private function getRecipients()
{
    foreach (User::get() as $user)
    {
        $this->recipients[$user->email] = [
            'id' => $user->id,
            'userToken' => $user->user_token,
            'first_name' => $user->first_name,
            'last_name' => $user->last_name,
            'email' => $user->email
        ];
    }
}

private function sendEmail()
{
    $subject = 'Demo Subject';
    /**
     * Data for the Blade template
     */
    $data = [
        'foo' => 'bar'
    ];
    // Inline the CSS for the email
    $inliner = new InlineEmail('emails.some-email', $data);
    $content = $inliner->convert();

    // Create Emails table entry for this email. Used for Mailgun webhooks
    $email = Email::create(['user_id' => $this->userId, 'subject' => $subject, 'email_id' => str_random()]);

    // Prepare the email addresses
    $emailAddresses = array_column($this->recipients, 'email');

    $this->mailgun->sendMessage('demo.org', [
        "from" => 'support@demo.org',
        "to" => implode(',', $emailAddresses), // Comma separated list of email addresses
        "subject" => $subject,
        "html" => $content, // Inlined CSS HTML from Blade
        "text" => "Plain text message here",
        "recipient-variables" => json_encode($this->recipients), // Required for batch sending, matches to recipient details
        "v:messageId" => $email->id, // Custom variable used for webhooks
    ]);
}

【讨论】:

    猜你喜欢
    • 2019-03-09
    • 1970-01-01
    • 2015-11-27
    • 2020-05-22
    • 1970-01-01
    • 2014-08-04
    • 2018-08-22
    • 2022-11-19
    • 2019-03-10
    相关资源
    最近更新 更多