Skip to content

How to send Mails Using Queues in Laravel

Last updated on February 17, 2018

I have recently spent some time trying with Laravel queue system, in this tutorial I am going to show you how to get started with the implementation of queues using database queue driver.

Let’s start. In order to use the database queue driver, you will need a database table to hold the jobs. To generate a migration that creates this table, run the queue:table Artisan command. Once the migration is created, you may migrate your database using the migrate command:

php artisan queue:table

php artisan migrate

Above migration will create “jobs” table with following structure –

CREATE TABLE  `databaseName`.`jobs` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `queue` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `payload` longtext COLLATE utf8_unicode_ci NOT NULL,
  `attempts` tinyint(3) unsigned NOT NULL,
  `reserved` tinyint(3) unsigned NOT NULL,
  `reserved_at` int(10) unsigned DEFAULT NULL,
  `available_at` int(10) unsigned NOT NULL,
  `created_at` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `jobs_queue_reserved_reserved_at_index` (`queue`,`reserved`,`reserved_at`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

In order to use Laravel 5’s mail functionality, it first needs to be configured. Look at the mail settings that come out of the box in the .env file.

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

I am going to use Gmail SMTP settings, here are my confirmations –

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=YOUR-GMAIL-PASSWORD
[email protected]
MAIL_NAME=YOUR-NAME

Now open you config/mail.php and change the address and name values to use env config values.

// Find the following line
  'from' => ['address' => null, 'name' => null],
// Change it to
  'from' => ['address' => env('MAIL_FROM'), 'name' => env('MAIL_NAME')],

Now you can send emails using configured SMTP servers as below shown, this tutorial assumes that you have basic knowledge of laravel.

function createAccount() {
  ....
  ....
  Mail::send('emails.confirm',
  [user => $user],
  function($message) use ($user) {
   $message->to($user->email)
          ->subject('Confirm your account...');
  });
  ...
  ...
}

You may notice there will be a slight delay in receiving a response of success or failure because of Email sending. This delay can be especially long when you are using the SMTP mail driver.

To avoid this delay we are going to use Queues here. Queues allow you to defer the processing of time-consuming tasks, such as email sending. Queues allow your web requests to respond quicker to the user.

Laravel providing several different queue implementation, however, we are going to use Database driver as I told in the post beginning.

Most of the configurations we have already completed by creating jobs table. One final step is, edit .env and change the QUEUE_DRIVER setting from sync to database.

To queue the email, there is only a single small change to make. replace your Mail::send() method with Mail::queue() that is it, now laravel will queue all the emails.

Full code after change –

function createAccount() {
  ....
  ....
  Mail::queue('emails.confirm',
  [user => $user],
  function($message) use ($user) {
   $message->to($user->email)
          ->subject('Confirm your account...');
  });
  ...
  ...
}

we need to run the background watching for handling items arriving in the queue. Laravel includes an Artisan command that will run new jobs as they are pushed onto the queue. You may run the listener using the queue:listen command:

  php artisan queue:listen

That is it.

0 0 votes
Article Rating
Subscribe
Notify of
guest

10 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments