Confused about Laravel 5 Queue for sending mail (Laravel 5.4)

I am upgrading from Laravel 4.1 to 5.4 . Queue transfer is difficult.

Firstly, I commented on the iron route from the routes:

 Route::post('queue/receive', function() { return Queue::marshal(); }); 

Then I set up the database driver and migrated the jobs table:

 php artisan queue:table php artisan migrate 

I changed the Mail::queue codes to Mailables as follows:

 Mail::to(' person@gmail.com ')->send(new ForgotPassword($user->first_name)); 

I managed to send letters synchronously (without queue). Then I switched to the queue:

 Mail::to(' person@gmail.com ')->queue(new ForgotPassword($user->first_name)); 

And finally, I run this command from the console:

 php artisan queue:listen 

When the Mail::to line is executed, the line is inserted into the jobs table, but mail is not sent. How can I solve this problem?

Note: ForgotPassword is a Mailable class (should there be a Job class?).

+1
laravel laravel-5 laravel-mail laravel-queue
source share
3 answers

The main difference between synchronous send() and asynchronous queue() for your ForgotPassword object is that when sending a queue to an object for sending, it must be serialized to send to the queue, and unserialized when the queue workflow processes it.

Since send() works fine, but an error occurs with queue() , and we can see that the job in the queue is running and trying to be processed, most likely an error in serialization / unserialization.

Your ForgotPassword class probably uses the SerializesModels flag since the artisan command generates a new object to be delivered. This attribute defines the __sleep() and __wakeup() methods that affect the operation of serialization and non-serialization.

When the __sleep method __sleep implemented, PHP will only serialize the variables returned by the __sleep method. In this case, the implementation provided by the SerializesModels tag uses Reflection to navigate through the properties defined in the class to provide a special way to serialize Eloquent models and collections.

Because of this, this means that any variables in your ForgotPassword class that are not specifically defined as a class property will not be serialized and will not be available when processing a job in a queue, but a serialization class. This is the most likely cause for your problem. When your work is done, your uncertified email copy does not have the necessary data and does not work.

There are two ways to solve this problem. Firstly, if your ForgotPassword does not really need to serialize any models, you can remove the SerializedModels tag. This will remove the __sleep() definition from the class, and then all the variables assigned to the class, and not just the ones that are actually defined, will be serialized and will be available if the class is non-sterilized.

The second option, more suitable and more explicit, is to actually define the properties that you need in the ForgotPassword class.

If you define the properties of your class, you can leave the SerializesModels flag in your class. However, if you are not actually serializing the models, I would go and remove it. There is no need for additional serialization costs if you do not need it.

+2
source share

You may need to edit the existing .env file in the project folder on your computer. Find the .env file, open it through the sublime text and edit it.

Edit using the email id and password with which you are going to send emails.

0
source share

You can send mail using the laravel queue, see example,

Define a task

 public function handle() { $mail_meta_data = $this->send_data; $require_data = $this->require_data; Mail::queue($mail_meta_data['view_name'], ['data'=>$require_data], function($message) use ($mail_meta_data, $require_data) { //$message->from($mail_meta_data['to'], $mail_meta_data['name']); $message->to($mail_meta_data['to'])->subject($mail_meta_data['subject']); }); } 

Job Call

 dispatch(new SendEmailNotification($mail_meta_data, $require_data)); 

.env

  MAIL_DRIVER=smtp MAIL_HOST=smtp.sendgrid.net MAIL_PORT=587 MAIL_USERNAME=smtp_username MAIL_PASSWORD=smptp_password MAIL_ENCRYPTION=tls APP_EMAIL=email sent from APP_NAME=Name of Email sender 

Note

  • Make sure to configure mail settings in your .ENV file or in config / mail.php
  • Also php artisan queue: listening in progress
0
source share

All Articles