Laravel:如何将邮件排队稍后发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23508936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: How to Queue mails to send later
提问by Gabriel Matusevich
Im trying to use the Mail::queue to send and email, but when I call this function it simple sends the mail, and the response is delayed ... I thought that the point of using Mail::queue was to queue ....
我试图使用 Mail::queue 发送和发送电子邮件,但是当我调用这个函数时,它简单地发送邮件,并且响应被延迟......我认为使用 Mail::queue 的目的是排队 .. ..
I want the response to came instantly, not having to wait for the email to be sent
我希望立即收到回复,而不必等待发送电子邮件
for eg
例如
Mail::queue('emails.template', $data, function($message) {
$message->to('[email protected]');
$message->subject('Notificacion');
});
return Response::json(array('error' => 0, 'message' => 'Ok'));
I want to receive the response without waiting for the mail to be sent. How can I do that???
我想在不等待邮件发送的情况下收到回复。我怎样才能做到这一点???
回答by Wogan
What queue driver (app/config/queue.php - 'default' param
) are you using? If you're using sync
, and haven't set up one of the others, then you're using the synchronous driver, which does exactly what the name says: Runs your queued task as soon as the task is created.
app/config/queue.php - 'default' param
您使用的是什么队列驱动程序 ( )?如果您正在使用sync
,并且尚未设置其他任何一个,那么您正在使用同步驱动程序,它完全符合名称的意思:任务一创建就运行排队的任务。
You need to configure an MQ server for Laravel to talk to. You can get a free iron.io account for this, and then you need to configure it, for instance:
您需要为 Laravel 配置一个 MQ 服务器以与之通信。您可以为此获得一个免费的 iron.io 帐户,然后您需要对其进行配置,例如:
'iron' => array(
'driver' => 'iron',
'project' => 'iron-io-project-id',
'token' => 'iron-io-queue-token',
'queue' => 'queue-name',
),
Then when you use Mail::queue()
it will push the instruction to iron.io. You'll then have to have another thread listening on the queue - just run php artisan queue:listen
and leave it running while messages are pushed to the queue.
然后当你使用Mail::queue()
它时会将指令推送到iron.io。然后,您必须让另一个线程侦听队列 -php artisan queue:listen
在消息被推送到队列时运行并让它继续运行。
回答by rajiv patel
/**
* 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" => '[email protected]',
"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
]);
}