How to Send Email using a Queue in Laravel 5

This post explains how to send emails using a queue in your Laravel 5 application. Laravel is a PHP framework for web artisans and sending emails using a queue is a cinch. Follow me now.

Edit .env File

Look for a .env file in the root directory of your project and fill in the following. If there is no .env, rename the file .env.example to .env.

Mail configuration

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

Create an account on mailtrap.io and replace null on MAIL_USERNAME and MAIL_PASSWORD with your username and password provided by mailtrap. Mailtrap is a test mail server solution that allows testing email notifications without sending them to the real users of your application. Not only does Mailtrap work as a powerful email test tool, it also lets you view your dummy emails online, forward them to your regular mailbox, share with the team and more! Mailtrap is a mail server test tool built by Railsware Products

Set queue driver

QUEUE_DRIVER=database

By setting the queue driver to database, queued items will be temporarily stored in the database.

Run the Migration

Create the table (jobs) where the queued items would be temporarily stored using the command below:

divosays@divosays-hp /opt/lampp/htdocs/oksa $ php artisan queue:table
Migration created successfully!
divosays@divosays-hp /opt/lampp/htdocs/oksa $ php artisan migrate
Migrated: 2016_10_09_130436_create_jobs_table

Create a View containing the Message

Laravel’s emailer always uses views to send email, so create a file called test.blade.php in directory resources/views/emails.

Content of emails.test view

<p>
This is a test, an email test and its working.
</p>

Edit config/mail.php

Change this

'from' => ['address' => null, 'name' => null]

, to

'from' => ['address' => 'from@example.com', 'name' => 'DivoSays'].

Now that the queue is set up, use the method below in your Controller to send email using queue:

function send_email($user){
    Mail::queue('emails.test', ['user' => $user], function($message) use ($user){
            $message->to($user->email, $user->name)->subject('Sending Email Using Queue in Laravel 5');
        });
}

After doing this and testing your code, you won’t see any email in your mailtrap inbox. This is normal because there is no process running in the background, watching for and handling items arriving in the queue.

To process the next item on the queue, manually run the artisan’s queue:work command.

This command will do nothing if the queue is empty, but if there’s an item on the queue it will fetch the item and attempt to execute it.

divosays@divosays-hp /opt/lampp/htdocs/oksa $ php artisan queue:work
Processed: mailer@handleQueuedMessage

As you can see here it handled the queued email message. Now the email should arrive in your inbox within moments.

Automatically Processing the Queue

In the file app/Comsole/Kernel, add the following code in the schedule method like below:

protected function schedule(Schedule $schedule)
{
    $schedule->command('inspire')->hourly();

    $schedule->command('queue:work')->everyMinute(); // This would run the queue:work command every minute
}

Now in /etc/crontab, add the entry below:

* * * * * divosays cd /opt/lampp/htdocs/oksa/ && /usr/local/bin/php artisan schedule:run >> /dev/null 2>&1

After doing the above, the queue:work command will execute every minute and process items in the queue. At this point you don’t need to worry about manually running the queue:work which is not a practical thing to do.

That’s it for today. I hope this post is informative to you and that you now able to send email using queue in laravel 5. Queues allow you to defer the processing of time consuming tasks, such as emails. This allows your web requests to respond quicker to the user. If you have any question or contribution, leave a comment below. please share this post. Click on any of the social media icons to easily share it. Nice day.

Leave a Reply

Your email address will not be published. Required fields are marked *