Quick Note: Implement a simple Laravel 8 Queue for sending mail

Prerequisites

Before you start, you should familiar with Laravel and it’s a quick note only. AWS SES settings and using mailable.

##### Install package
$ composer require aws/aws-sdk-php -W
##### Check config/services.php# 'ses' => [
# 'key' => env('AWS_ACCESS_KEY_ID'),
# 'secret' => env('AWS_SECRET_ACCESS_KEY'),
# 'region' => env('AWS_DEFAULT_REGION', 'us-west-2'),
# ],
##### Check .env # MAIL_MAILER=ses
# MAIL_FROM_ADDRESS=noreply@example.com
# AWS_ACCESS_KEY_ID=
# AWS_SECRET_ACCESS_KEY=
# AWS_DEFAULT_REGION=us-east-1

Create a mailable class:

$ php artisan make:mail Hey

Edit app/Mail/Hey.php

<?phpnamespace App\Mail;use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class Hey extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('mail.hey');
}
}

Create resources/views/mail/hey.blade.php

<p>
Hey! How are you? You can customize HTML.
</p>

Using database driver

$ php artisan queue:table###### Edit.env
# QUEUE_CONNECTION=database
$ php artisan migrate###### Create Job
$ php artisan make:job SendMail

Edit app/Jobs/SendMail

<?phpnamespace App\Jobs;use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
use App\Mail\Hey;
class SendMail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// Implement job task
$mail = new Hey();
Mail::to('hey@example.com')->send($mail);
}
}

Dispatch Job

$ php artisan make:controller MailController<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;
use App\Jobs\SendMail;
class MailController extends Controller
{
public function send()
{
$job = new SendMail();
$this->dispatch($job);
return "ok";
}
}

Define route

use App\Http\Controllers\MailController;Route::get('send-queue-mail', [MailController::class, 'send']);

Start queue worker

$ php artisan queue:work

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

DevOps: The Answer to Your Prayers

Manico Mac App 2022 REVIEW — MacSources

12 Factor App Principles and Cloud-Native Microservices

Open Source IoT Edge Projects

Build An Image Search Engine Using Python

CS371p Spring 2022: Gavin Garcia

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
andyyou

andyyou

More from Medium

Handling Routes With My Controller Setup

Creating an API URL on AWS for judge0

Clone of Apple TV+

How to automate Accessibility testing by using XCUI Automation Tool.