How to send email queue without using job in Laravel?

There are various ways to send an email in Laravel application. In the previous tutorial we learnt how to send it using a Laravel Job. Let us now learn how to send email queue without using job.

You can use the ‘mailable’ class to send email by a Laravel application. The classes are stores in the app/Mail directory of your Laravel project. You can create a new mailable class using the command below.

php artisan make:mail EmailMail

Once you have generated a mailable class, open it up so we can explore its contents. Mailable class configuration is done in several methods, including the envelopecontent, and attachments methods.

After creating a mailable class, you can edit its contents. The Mailable class has several methods. For example, content, envelope, and attachment methods.

Envelope method will return the Illuminate\Mail\Mailables\Envelope object. It defines the email subject, and also the mail recipients.

Content method returns the Illuminate\Mail\Mailables\Content object. This makes up the blade template that helps generate the email message.

Mail::to Façade

The Mail::to façade in a controller class will be used to send an email. This method takes in an email address, for a single or multiple users (the email recipients). The Mail::to façade automatically use the recipients email and name to send the email. As shown below, the $data variable takes in all recipients details and passes to the Mail façade.

$data=array(
   'name' => $request->name,
   'email'=>$request->email,
   'phone'=>$request->phone,
   'subject'=>$request->subject,
   'comment'=>$request->message,
);

Mail::to($request->email)
    ->queue(new ContactusMail($data));

The queue() method above takes care of the queue process that is done while using job in Laravel.

Edit the Mailable Class

We can customize the Mailable class with any code. Open the newly generated Mailable class. It will appear somewhat as shown below.

use Queueable, SerializesModels;

private $data;
public function __construct($data)
{
    $this->data = $data;
}

public function build()
{
    $details=$this->details;
    return $this->from('no-reply@example.com', 'From name')
                 ->subject('Contact Us')
                 ->view('contact-us',compact('data'))
}

Specify required details in the build method, then create an email template in a blade file.

Email Template

Create a new blade, as emailus.blade.php in the resources/views directory. Edit it with the code as shown below.

<html>
<head>
    <title>Email Us</title>
    <style>
        table, td, th {
            border: 1px solid black;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }
    </style>
</head>
<body>
    <table>
        <tr>
            <th>Name</th>
            <td>{{$data['name']}}</td>
        </tr>
        <tr>
            <th>Email</th>
            <td>{{$data['email']}}</td>
        </tr>
        <tr>
            <th>Phone</th>
            <td>{{$data['phone']}}</td>
        </tr>
        <tr>
            <th>Subject</th>
            <td>{{$data['subject']}}</td>
        </tr>
        <tr>
            <th>Message</th>
            <td>{!! $data['comment'] !!}</td>
        </tr>
    </table>
</body>
</html>

All done, this makes up the email template, and the Laravel application handles sending it to the specified recipients to send email queue without using job in Laravel.

Leave a Comment