i had been trying to solve the queue mail for few days, but still i can't find the solution for that. Please look at my controller, job, mail
Controller
<?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();
}
}
job
<?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,
]);
}
}
After that i add php artisan queue:work But the result is still the same as using SEND. is there any wrong with my coding?
Implement ShouldQueue
in your mailable class.
use Illuminate\Contracts\Queue\ShouldQueue;
class EmailContact extends Mailable implements ShouldQueue
Also verify your queue driver. By default laravel sets the queue driver to sync
. Which means any queue jobs will run like normal code even if you are trying to queue it. You need to implement a queue first for it to work. Try database or redis queues.
QUEUE_DRIVER=sync
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.