我尝试在后台发送邮件,但无法正常工作。
我运行php artisan队列:侦听并点击控制器发送邮件,但未收到邮件。
运行常见的php artisan队列后:监听其显示作业进行处理。
在作业中,表作业条目已成功插入。
.env文件
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=database
邮件文件
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class NewOrderSupplierMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.test');
}
}
作业文件
<?php
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;
use Illuminate\Contracts\Mail\Mailer;
use App\Mail\NewOrderSupplierMail;
class SendEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $details;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(Mailer $mailer)
{
$email = new NewOrderSupplierMail();
Mail::to($this->details['email'])->send($email);
}
}
控制器文件
use Mail;
use App\Jobs\SendEmailJob;
$details['email'] = 'sumitsinghnaruka@gmail.com';
dispatch(new SendEmailJob($details));
在终端上
php artisan queue:listen database
[2018-09-29 11:27:05][14143] Processing: App\Jobs\SendEmailJob
[2018-09-29 11:27:05][14144] Processing: App\Jobs\SendEmailJob
[2018-09-29 11:27:05][14145] Processing: App\Jobs\SendEmailJob
[2018-09-29 11:27:05][14146] Processing: App\Jobs\SendEmailJob
[2018-09-29 11:27:06][14147] Processing: App\Jobs\SendEmailJob
[2018-09-29 11:27:06][14148] Processing: App\Jobs\SendEmailJob
[2018-09-29 11:27:06][14149] Processing: App\Jobs\SendEmailJob
[2018-09-29 11:27:06][14150] Processing: App\Jobs\SendEmailJob
[2018-09-29 11:27:06][14151] Processing: App\Jobs\SendEmailJob
[2018-09-29 11:27:06][14152] Processing: App\Jobs\SendEmailJob
[2018-09-29 11:27:07][14153] Processing: App\Jobs\SendEmailJob
[2018-09-29 11:27:07][14154] Processing: App\Jobs\SendEmailJob
[2018-09-29 11:27:07][14155] Processing: App\Jobs\SendEmailJob