Lumen 框架使用队列和作业发送电子邮件不工作

Posted

技术标签:

【中文标题】Lumen 框架使用队列和作业发送电子邮件不工作【英文标题】:Lumen framework sending emails using queues and jobs not working 【发布时间】:2021-02-08 15:00:22 【问题描述】:

已经为这个案例做了所有的配置,通过 Composer 安装了照明/邮件包,使用与 Lumen 8.0 相同的版本。

在 .env 文件上配置邮件服务器。

已创建配置文件夹,其中包含 queue.php 和 mail.php 文件。

取消注释 $app->withFacades();$app->withEloquent(); 并添加:

$app->withFacades(true, [
'Illuminate\Support\Facades\Mail' => 'Mail',
]);

$app->configure('app');
$app->configure('mail');

$app->register(Illuminate\Mail\MailServiceProvider::class);
$app->alias('mail.manager', Illuminate\Mail\MailManager::class);
$app->alias('mail.manager', Illuminate\Contracts\Mail\Factory::class);
$app->alias('mailer', Illuminate\Mail\Mailer::class);
$app->alias('mailer', Illuminate\Contracts\Mail\Mailer::class);
$app->alias('mailer', Illuminate\Contracts\Mail\MailQueue::class);

也已经执行了 php artisan queue:table 和 php artisan migrate`

我的工作文件 app/Jobs/SubmitEmailJob.php:

<?php

namespace App\Jobs;

use Illuminate\Support\Facades\Mail;

use App\Mail\FormEmail;

class SubmitEmailJob extends Job

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct($details)

    $this->details = $details;


/**
 * Execute the job.
 *
 * @return void
 */
public function handle()

    // $email = new FormEmail($this->details);
    // Mail::to($this->details['email'])->send($email);

    Mail::to($details['email'])->send(new FormEmail($details));

可邮寄文件 app/Mail/FormEmail.php:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class FormEmail extends Mailable

    use Queueable, SerializesModels;

// protected $details;

/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct($details)

    $this->details = $details;


/**
 * Build the message.
 *
 * @return $this
 */
public function build()

    return $this->from('my_email_address_here@gmail.com', 'Gmail')
    ->subject('Confirmation Email')
    ->view('email_text')->with(['details'=>$this->details]);

还有我的控制器方法:

public function create(Request $request)

    $user = new User;
    $user->first_name = $request->input('first_name');
    $user->last_name = $request->input('last_name');
    $user->email = $request->input('email');
    $user->birthday = $request->input('birthday');
    $user->gender = $request->input('gender');

    $file = $request->file('cv');
    $filename = base64_encode($file->getClientOriginalName());
    $destinationPath = storage_path('uploads/user_cv/');
    $userCvPathName = 'uploads/user_cv/'.base64_decode($filename);
    $request->file('cv')->move($destinationPath, base64_decode($filename));

    $user->user_cv = $userCvPathName;

    $user->save();

    //Email sending

    $details = [
        'first_name'    => $request->input('first_name'),
        'last_name'     => $request->input('last_name'),
        'email'         => $request->input('email'),
    ];

    $emailJob = (new SubmitEmailJob($details));
    dispatch($emailJob);

    //Already tried to send email without jobs and queues and its working
    // Mail::to($details['email'])->send(new FormEmail($details));

    return response()->json($user);

我不明白我在这里缺少什么

【问题讨论】:

【参考方案1】:

好的,通过使用以下命令创建 failed_jobs 数据库表解决了问题:

php artisan queue:failed-table

php artisan migrate

我还必须在作业类中声明一个变量 protected $details;

谁在流明中为这个或其他事情而苦苦挣扎,最好检查错误日志:storage/logs/

【讨论】:

以上是关于Lumen 框架使用队列和作业发送电子邮件不工作的主要内容,如果未能解决你的问题,请参考以下文章

在 Lumen 框架内通过 Mandrill 发送电子邮件

Laravel Lumen 队列失败与队列:工作但不是队列:听

Laravel/Lumen - 如何从网站触发工作流程?

如何将链式作业分派到 Lumen 6 中的队列?

如何创建队列系统以在zend框架2.2中发送电子邮件。

Lumen 5.3 发送邮件