Search code examples
laravelemailqueuelumen

Lumen framework sending emails using queues and jobs not working


Already made all the configuration for this case, installed the illuminate/mail package via Composer using same version as Lumen 8.0.

Configured mail server on .env file.

Created config folder with queue.php and mail.php file inside.

Uncommented $app->withFacades(); and $app->withEloquent(); and added:

$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);

Also already executed php artisan queue:table and php artisan migrate`

My job file 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));
}

}

Mailable file 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]);
}

}

And my controller method:

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);
}

Im not getting it what Im missing here


Solution

  • Ok the problem was solved by creating failed_jobs database table using this commands:

    php artisan queue:failed-table
    
    php artisan migrate
    

    also i had to declare a variable protected $details; in job class.

    Who is struggling with this or other things in lumen better check error log on: storage/logs/