Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Queue for mailing but laravel 5.4 does not seem to respond

Tags:

laravel-5.4

I am using queues for the first time and I don't seem to get it to work and laravel does not seem to throw any errors.

I am trying to queue mail on user registration, that is user should be redirected to dashboard immediately and the email should be queued. How do I know if queue is not working? on clicking register I have to wait for 8 seconds before I see dashboard, I get the email on registration but queue seems to fail.

I dont see any queue jobs on mysql jobs table at the time of registration.

Below are my settings:

jobs and jobs-failed table created using below command:

php artisan queue:table
php artisan queue:failed-table
php artisan migrate

.env

QUEUE_DRIVER=database

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="App name"

RegisterController create() function

at the end of create function just before return $user;

dispatch(new NewRegisteredUser($user));

jobs file: NewRegisteredUser

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Mail;
use App\User;
use App\Mail\RegisteredUserWelcome;

class NewRegisteredUser implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $email = new RegisteredUserWelcome($this->user);
        Mail::to($this->user->email)->queue($email);
    }
}

Mail file: RegisteredUserWelcome

namespace App\Mail;

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

class RegisteredUserWelcome extends Mailable
{
    use Queueable, SerializesModels;

    protected $user;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.registered')
            ->with([
                        'firstname' => $this->user->firstname,
                        'lastname' => $this->user->lastname,
                    ]);
    }
}

Am I missing something here? I dont get error and mail is sent but no queue jobs observed in mysql and long wait time on registration.

I am also confused how this: https://laravel.com/docs/5.4/mail#queueing-mail fits with this https://laravel.com/docs/5.4/queues Why do we need queues separately when we already have queuing in mail?

What should I use and how? All I need to do is reduce the wait-time of users and send mails behind the scene. Also, I am sending multiple emails in some cases. I guess this would help if I queue the jobs of multiple emails.

like image 303
Murlidhar Fichadia Avatar asked May 17 '17 23:05

Murlidhar Fichadia


1 Answers

I used markdown as the mail template

I got Queues to work and the output is seen in php artisan queue:listen.

The issue was .env file was not clear cached. php artisan config:clear made it to work. Queues can be made much more efficient by simply delaying the job.

I delayed the job by 60 seconds (1 minute) and things work really well.

I will post all the code and files I had to create to make it work.

setting .env file

QUEUE_DRIVER=database

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=password
MAIL_ENCRYPTION="tls or ssl"  (double quotes not required)
MAIL_FROM_ADDRESS="email address to be shown to email receiver" (double quotes not required)
MAIL_FROM_NAME="App Name" (double quotes not required only if you have blank space between the name)

This line of code makes sure new values in .env file is taken into consideration

php artisan config:clear

In my Controller file

use Mail;
use App\Jobs\NewprofileCreated;
use App\Mail\ProfileCreated;

Controller Code

dispatch((new NewprofileCreated($user))->delay(60));

Two files created using below command

php artisan make:job NewprofileCreated
php artisan make:mail ProfileCreated

NewprofileCreated Job file

use Mail;
use App\User;
use App\Mail\ProfileCreated;

class NewprofileCreated implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
         $email = new ProfileCreated($this->user);
        Mail::to($this->user->email)->queue($email);
    }
}

ProfileCreated Mail file

class ProfileCreated extends Mailable
{
    use Queueable, SerializesModels;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.profile_created');
    }
}

profile_created markdown template (folder: view/emails/profile_created)

@component('mail::message')

<h1>Your new profile is created</h1>

You have received this email because your profile was created for {{ config('app.name') }}


Thanks,<br>
{{ config('app.name') }}
@endcomponent
like image 185
Murlidhar Fichadia Avatar answered Oct 29 '22 15:10

Murlidhar Fichadia