【问题标题】:Why queue still waiting when send mail in laravel?为什么在laravel中发送邮件时队列仍在等待?
【发布时间】:2018-09-14 18:55:14
【问题描述】:

我想在队列中发送邮件并且发送邮件时没有等待 https://laravel.com/docs/5.7/queues#connections-vs-queues

我运行命令创建表jobs:

php artisan queue:table    
php artisan migrate

我创建了一个发送邮件的工作:php artisan make:job SendEmailJob
并编辑代码:

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Mail;
class SendEmailJob  implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public $body;
    public $emailto;

    public function __construct($body,$email)
    {
        //
         $this->body=$body;
         $this->emailto=$email;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
       $email=$this->emailto;
       Mail::send("body_email.confirm_order",['Body'=> $this->body], function($message) use ($email)
       {
           $message->from(env('MAIL_USERNAME'),"Eye glasses");
           $message->subject("Confirm Email");
           $message->to($email);
       });  
    }
}

我从控制器调用队列:

use App\Jobs\SendEmailJob;

public function index()
{
     $Body="test";
     $email="daitb@vnitsolutions.com";     
     SendEmailJob::dispatch($Body,  $email);
     $calendars= AppointmentModel::GetAppointmentofDoctor($id,$datetime);      
     return view('frontend.appointment',["calendars"=>$calendars]);
}

QUEUE_DRIVER=database 添加到文件.env 运行命令:

php artisan queue:work

如果我运行控制器,进程仍在等待发送邮件完成并运行其他进程。 我尝试更改为:

SendEmailJob::dispatch($Body,  $email)->delay(now()->addMinutes(3));

不延迟,5s后仍然发送邮件。

为什么在 laravel 中发送邮件时队列仍在等待? 我用的是win 32。

【问题讨论】:

  • 您的队列是否正常工作,即您是否运行过php artisan queue:work
  • 我从网站运行控制器,它可以发送邮件。我必须运行 php artisan queue:work 吗?
  • 如果您的队列不工作,您如何发送邮件?
  • 我只调用函数 index(),邮件发送正常。

标签: laravel laravel-5.7


【解决方案1】:

.env 文件中将QUEUE_CONNECTION=sync 更改为QUEUE_CONNECTION=database 解决了我的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2014-08-04
    • 1970-01-01
    相关资源
    最近更新 更多