Laravel中使用队列发送邮件问题排查与正确实现方式
问题分析与解决方案
Let's break down why your queue job shows "processing" but fails to send the data correctly, then fix it step by step:
核心问题原因
- 数据传递链条完全断裂:You didn't pass the form submission data from the controller to the queue Job, or from the Job to the Mailable. No data flows through the entire process, so the Mailable has nothing to send.
- Mailable构造函数语法错误:The line
$this->$data = $data;has an extra dollar sign, which breaks the assignment of the class property$data.
分步修正代码
1. 修正控制器(AskEmailController.php)
First, validate the form data (to ensure required fields exist) and pass it to the queue Job:
<?php namespace App\Http\Controllers; use App\Jobs\HelpEmailJob; use Illuminate\Http\Request; use Carbon\Carbon; class AskEmailController extends Controller { public function askemail() { return view('help.question'); } public function store(Request $request) { // Validate required fields (adjust based on your form) $validatedData = $request->validate([ 'sirname' => 'required|string', 'email' => 'required|email', 'bodymessage' => 'required|string' ]); // Pass validated data to the queue Job HelpEmailJob::dispatch($validatedData) ->delay(Carbon::now()->addSeconds(5)); // Optional: Add a success feedback for the user return back()->with('success', 'Your request has been submitted, we will get back to you soon!'); } }
2. 修正队列Job(HelpEmailJob.php)
Add a class property to store the form data, receive it in the constructor, then pass it to the Mailable:
namespace App\Jobs; use App\Mail\HelpEmailMailable; use Illuminate\Support\Facades\Mail; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; class HelpEmailJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; // Store form data (protected/public for serialization) protected $formData; /** * Create a new job instance. * * @param array $formData * @return void */ public function __construct(array $formData) { $this->formData = $formData; } /** * Execute the job. * * @return void */ public function handle() { // Pass the data to the Mailable Mail::to('mikeshasaco@gmail.com')->queue(new HelpEmailMailable($this->formData)); } }
3. 修正Mailable(HelpEmailMailable.php)
Fix the constructor assignment error and simplify the build logic:
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; class HelpEmailMailable extends Mailable { use Queueable, SerializesModels; protected $data; /** * Create a new message instance. * * @param array $data * @return void */ public function __construct(array $data) { // Remove the extra $ to assign the property correctly $this->data = $data; } /** * Build the message. * * @return $this */ public function build() { // Pass the data directly to the view return $this->view('help.contactinfo') ->with('data', $this->data); } }
额外检查事项
- Verify your queue driver configuration in
.env(e.g.,QUEUE_CONNECTION=database— if using database, runphp artisan queue:tableandphp artisan migratefirst) - Run the queue worker with
php artisan queue:work(add--tries=3in development to avoid infinite loops) - Check your mail settings in
.env(MAIL_* parameters) to ensure emails can be sent successfully
内容的提问来源于stack exchange,提问作者jmike
相关产品推荐
相关产品推荐

