Queuing emails in Laravel with Background Jobs

by Sebastian Schiau , 5 years ago

So most of the web apps nowadays are featuring email notifications, newsletters and all kinds of different email implementations.  Laravel eases up the process a lot with its suit of tools that allows you to send email via any driver you wish, template and customize your emails and even queue them for later execution.


The queue functionality comes in handy in particular when you want to speed up up your application by not having to wait for server response when doing requests that are sending emails. This basically allows emails to be sent asynced on the server side. Couple examples of use cases:


  • When for example pressing on the register button, the UI won’t have to wait for the server response, while the server is sending the email.
  • When your server / service sends your email to slow.
  • When sending large number of emails, maybe even bypassing Mailgun’s 100 emails / hour limit.


But enough with the long talk and let’s get to business. For this particular example, I have used Laravel 5.4, but this should be pretty much the same on newer and even older versions. Let's get started!

Step 1. For easier to edit code in the future, we will use a Service provider to easily dispatch email actions across our controllers. To get the based of your provider, you can run the following command

php artisan make:provider EmailProvider

After the base file has been created, make sure it looks something like this

<?php
namespace App\Providers;
use App\Jobs\SendEmail;
use Illuminate\Support\ServiceProvider;
class EmailProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     *
     * Generic email template method
     *
     * @param $email
     * @param $header
     * @param $content
     */
    public static function sendEmail($subject, $title, $content){
        dispatch(new SendEmail($subject,$title,$content));
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Step 2. Create a Laravel Job

php artisan make:job SendEmail

After the base file has been created, make sure it looks something like this

<?php
namespace App\Jobs;
use App\Mail\GenericEmail;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Mail;
class SendEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $emailSubject,$emailTitle,$emailContent;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($emailSubject,$emailTitle,$emailContent)
    {
        //
        $this->emailSubject = $emailSubject;
        $this->emailTitle = $emailTitle;
        $this->emailContent = $emailContent;
    }
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        Mail::to($user->email)->later(Carbon::now()->addMinute(1), new GenericEmail($this->emailSubject,$this->emailTitle,$this->emailContent));
    }
}

Step 3. Create a Mailable Class

php artisan make:mail GenericEmail


<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class GenericEmail2 extends Mailable
{
    use Queueable, SerializesModels;
    public $subject = 'Mass email';
    public $title = 'Email header';
    public $content = 'Email content';
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($emailTitle,$emailTitle,$emailContent)
    {
        //
        $this->subject = $emailTitle;
        $this->title = $emailTitle;
        $this->content = $emailContent;
    }
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this
            ->subject($this->subject)
            ->view('emails.template');
    }
}

Step 4. Create an email template file in a directory like resources/views/emails/template.blade.php with a content like


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" style="font-size: 100%; font-family: 'Avenir Next', 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; line-height: 1.65; margin: 0; padding: 0;">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width" />
<!-- For development, pass document through inliner -->
  </head>
  <body style="font-size: 100%; font-family: 'Avenir Next', 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; line-height: 1.65; width: 100% !important; height: 100%; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; background: #efefef; margin: 0; padding: 0;">
  <h2>{!!$emailTitle!!}</h2>
  <p>{!!$content!!}</p>
  </body>
</html>

Now you should be be able to send queued emails from wherever you want in your app with something like

EmailProvider::sendGlobal(‘This is a test email’,’Welcome friend’,’Just testin things around’);


But before actually sending the emails, we will need to configure laravel and our server to run the service worker continuously and send actually send the previously queued emails.
Now, there are many options you can chose from when it comes to service worker drivers, including self hosted worker, AWS or Redis.

In this example we will work with a self hosted worker, so let’s start by creating the tables it needs to store the tables.

php artisan queue:table
php artisan migrate


Then open up your dot env file and make sure the following entry is present
QUEUE_DRIVER=database

Bonus: Installing service supervisor

Now, in real life usage, on sites with decent amounts of traffic the service is very likely to fail from time to time, so Supervisor makes sure the worker gets restarted if this happens.

This time I am using a Centos system, but the commands should be relatively similar on *nix systems.

easy_install supervisor
yum install supervisor
echo_supervisord_conf > /etc/supervisord.conf
nano /etc/supervisord.conf

And add following content:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=/usr/bin/php /var/www/html/artisan queue:work --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
numprocs=2

Create log file

sudo mkdir -p /var/log/supervisor/laravel/
sudo touch /var/log/supervisor/laravel/worker.log

Start the service

sudo service supervisord start
sudo supervisorctl reload

And enable it at system start

systemctl enable supervisord

If you have config changes

supervisorctl reread
supervisorctl update
supervisorctl restart all

That’s it for this tutorial! If everything went well, then your Laravel email setup should be able to handle all the emails you need.

If you are having any questions, don’t hesitate to ask me via a comment!

Register and post a comment

You may also be interested in


PHP on steroids? Swoole introduction and benchmarks

What is Swoole? Swoole is an open-source C extension for PHP that enables true event-driven, async programming for PHP via its coroutines implement...

Top self hosted ecommerce platforms in 2020

The self-host eCommerce platform- a potentially cheaper, but definitely more customizable, more flexible, and more transparent solution if you’re look...

Mitigating and securing hacked Wordpress sites

As you may know already, Wordpress is one of the most common solution for rapidly building small to medium websites, offering powerful customization c...