我一直试图解决队列邮件几天,但我仍然无法找到解决方案。 请查看我的控制器,工作,邮件
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Contact;
use App\Jobs\SendEmail;
class MailController extends Controller
{
public function getContact(){
return view ('mail');
}
public function postContact(Request $request){
$contact = new Contact;
$contact->email = $request['email'];
$contact->name = $request['name'];
$contact->subject = $request['subject'];
$contact->save();
dispatch(new SendEmail($contact));
return back();
}
}
工作
<?php
namespace App\Jobs;
use App\Contact;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Mail\Mailer;
Use App\Mail\EmailContact;
class SendEmail implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
public $contact;
public function __construct(Contact $contact)
{
$this->contact = $contact;
}
public function handle(Mailer $mailer)
{
$email = new EmailContact($this->contact);
$mailer->to($this->contact->email)->send($email);
}
}
邮件
<?php
namespace App\Mail;
use App\Contact;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class EmailContact extends Mailable
{
use Queueable, SerializesModels;
public $contact;
public function __construct(Contact $contact)
{
$this->contact = $contact;
}
public function build()
{
return $this->view('emails.contact')->with([
'email' => $this->contact->email,
'name' => $this->contact->name,
'subject' => $this->contact->subject,
]);
}
}
之后我添加php artisan队列:work 但结果仍然与使用SEND相同。 我的编码有什么问题吗?
答案 0 :(得分:0)
在您的可邮寄课程中实施ShouldQueue
。
use Illuminate\Contracts\Queue\ShouldQueue;
class EmailContact extends Mailable implements ShouldQueue
同时验证您的队列驱动程序。默认情况下,laravel将队列驱动程序设置为sync
。这意味着即使您尝试对队列作业进行排队,任何队列作业也会像普通代码一样运行。您需要首先实现队列才能使其正常工作。尝试数据库或redis队列。
QUEUE_DRIVER=sync