How to send email using Laravel 8 job – Code examples

Sending mail is a vital part of many applications. For example, there can be automatic or on click email services, like confirmation emails, verification emails, promotions or events emails and so. Laravel Framework provides us with many ways to send an email. Let us go through the process to send email using Laravel 8 job. But, for that, you should first understand, what a job does in Laravel.

Job in Laravel are very simple classes, they normally contain only a handle function which gets invoked while processing the job in the queue.

You can create tasks in Laravel as Laravel Jobs and send them to a queue. The default Laravel structure stores all queueable jobs in the app/jobs directory. If your Laravel application does not has a directory, one will be created while running the  make:job Artisan command.

How to create a job in Laravel?

To create an email job in a Laravel application, type in the command below.

php artisan make:job SendVerifyEmail

This new generated class will implement the Illuminate\Contracts\Queue\ShouldQueue interface, and indicate Laravel that the job to send a verification email should be moved to the queue to run asynchronously.

How to dispatch?

To dispatch or start a job, or perform the operation behind the job, you can use the Laravel default function, as shown below.

use App\Jobs\SendEmailJob;

public function send_email(Request $request){
   $to = $request->to;
   $subject = $request->subject;
   $email_body = $request->email_body;
   SendEmailJob::dispatch($to, $subject, $email_body);
}

You can call this function on a button click or any other trigger you may define in the Controller. It will then initiate the job process.

Edit SendVerifyEmail.php

Your creates jobs will come under the app/Jobs directory. Open the SendVerifyEmail.php job, and you may edit it as required

<?php

namespace 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;

class SendEmailJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $to;
    protected $subject;
    protected $email_body;
    public function __construct($to, $subject, $email_body)
    {
        $this->to = $to;
        $this->subject = $subject;
        $this->email_body= $email_body;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Mail::send('email', ['email_body' => $this->email_body], function ($messageNew) {
            $messageNew->from('no-reply@example.com', 'From Name')
            ->to($this->to)
            ->subject($this->subject);
        });
    }
}

Leave a Comment