Php Laravel:如何将邮件排队等待以后发送
我试图使用Mail::queue发送和发送电子邮件,但当我调用此函数时,它只发送邮件,响应延迟。。。我认为使用Mail::queue的目的是排队 我希望即时收到回复,而不必等待电子邮件发送 例如Php Laravel:如何将邮件排队等待以后发送,php,laravel,queue,sendmail,Php,Laravel,Queue,Sendmail,我试图使用Mail::queue发送和发送电子邮件,但当我调用此函数时,它只发送邮件,响应延迟。。。我认为使用Mail::queue的目的是排队 我希望即时收到回复,而不必等待电子邮件发送 例如 Mail::queue('emails.template', $data, function($message) { $message->to('somemail@gmail.com'); $message->subject('Notificacion'); }); ret
Mail::queue('emails.template', $data, function($message) {
$message->to('somemail@gmail.com');
$message->subject('Notificacion');
});
return Response::json(array('error' => 0, 'message' => 'Ok'));
我想在不等待邮件发送的情况下收到回复。
我该怎么做呢?您使用的是什么队列驱动程序(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
,并在消息被推送到队列时让它保持运行。Mail::later方法也会做同样的事情,它只需等待我告诉它等待的任何时间,而响应是delayedIt,这实际上是不可能的。您的操作将得到服务器的响应。@GabrielMatusevich我可以知道您对此是否有任何解决方案吗?我在使用Laravel5时也遇到了同样的问题。这真的很有帮助,但我实际上正在寻找一种方法,在不依赖其他服务的情况下在本地实现这一点:pBeanstalkd呢??你有它的配置示例吗?使用iron.io确实是设置它的最快方法。对于beanstalkd,一旦安装,您应该能够使用配置的默认值-只需将第18行的default
更改为beanstalkd
。
/**
* 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
]);
}