Sending queued email with Laravel

Post Details

Sending queued email with Laravel

Sending queued email with Laravel

Laravel provides many api based services like the popular SwiftMailer library, Mailgun, Mailtrap, SparkPost, Amazon SES and so many, The API based approach is quite faster than the SMTP. All the API drivers require the Guzzle HTTP library.

Depending on your mail driver you need to configure the config/mail.php and config/services.php which I’m not going to discuss now, I’m sure you’ll find various helpful resources for them.

As you might already know In Laravel, each type of email sent by the application is represented as a "mailable" class which is really awesome and easy to find all of them together in app/Mail

Here is a basic queued mail sending example-

Generate the mailable class:

php artisan make:mail SendTestMail

open the mailable class file

 * Build the message.
 * @return $this
public function build()
    return $this->from('', ‘your name’)->subject(‘sending a test mail’)
                ->view('emails.demotemplate ');

**in the config/mail.php you can set global from address and if you do so then you don’t have to use the from method unless you want to override the global setup

**here we are using a demo template for the mail which is in resources/views/emails/demotemplate.blade.php

Now as we’ve created the mailable class we just need to send the mail using a controller

Let’s create a route:

Route::get('/queued-mail, 'MyCtrl@SendThemail');

Now the controller:

namespace App\Http\Controllers;

use App\Mail\SendTestMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;

class MyCtrl extends Controller
    public function MyCtrl(Request $request)
        Mail::to(‘’)-> queue((new SendTestMail())->onQueue(high'));

Here I’m specifying a high queue though you can leave it if you want default priority

There is another approach:

If you want your mailable class to always be able to send mail behind the scene then you may implement the ShouldQueue contract on the class, so even if you call the send method when mailing, the mailable will still be queued since it implements the contract

use Illuminate\Contracts\Queue\ShouldQueue;

class SendTestMail extends Mailable implements ShouldQueue
    Mail::to(‘’)->send(new SendTestMail());

**sending queued mail requires proper queue configuration in your application, to understand how to set up and configure queue see this post & just remember as you are queuing mail which can be thought as a queued task/job you don’t have to merge the mail to another job which can be queued, as mailing itself is the standalone job so you are actually pushing this mail sending job to the queue

Set the queue configuration & run the queue workers then just access the controller route/queued-mail and its done.

There are several other configurations available for the queued mail like setting up delay, pushing the mail sending on to specific queue connection etc.

Leave a comment

~ By Lord Buddha

Better than a thousand hollow words, is one word that brings peace.