Introduction to the Mailable Class in Laravel

Introduction to the Mailable Class in Laravel

Laravel 8.x Mailable. Laravel comes with a Mailable class that you can use to send emails effortlessly with. A good example of when to use the Mailable class is when the user registers. Upon registration completion, a welcome email is sent to the user. To strip out all unnecessary code, we’ll create an application that sends an email every time that you visit a specific page.

Laravel comes with a _Mailable _ class that you can use to send emails effortlessly with. A good example of when to use the _Mailable _ class is when the user registers. Upon registration completion, a welcome email is sent to the user. To strip out all unnecessary code, we’ll create an application that sends an email every time that you visit a specific page.

Lets use MailTrap.io to test our mailing. If you haven’t yet, open up a MailTrap account. Go to Inboxes and select your Inbox. Under Integrations, select Laravel. Copy the configuration settings into your project’s .env file. You’ll see that Laravel already pre-populates the MAIL_HOST _with _smtp.mailtrap.io in anticipation that you’ll use MailTrap for your email testing.

Image for post

Mailable

To create a new _Mailable _class, use the artisan make:mail command:

php artisan make:mail RandomEmail

As soon as you run that command, the RandomEmail class will be constructed and saved in the new _Mail _directory:

app/Mail/RandomEmail.php

Image for post

If you open the RandomEmail.php _file, you’ll see that it contains a constructor and a _build() method.

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class RandomEmail 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('view.name');
    }
}

The build method returns a view.name view. Let’s create a

_mail/random-email.blade.php _view.

Image for post

To begin, add some text to your random-email.blade.php file:

Hey there, 

This is just a random email. 
Randomly, 
Dino Cajic

Go back to the RandomEmail _Mailable class and change the return view to _random-email.blade.php.

<?php
// ...

class RandomEmail extends Mailable
{
    // ...
    public function build()
    {
        return $this->view('mail.random-email');
    }
}

Returning the view is just one thing that we can do in the _build() _method. We can also specify who sent the email and the email’s subject. According to the Laravel documentation, “All of a mailable class’ configuration is done in the build method. Within this method, you may call various methods such as from, subject, view, and attach to configure the email's presentation and delivery.”

We can just chain these methods to our return statement. Let’s start off by chaining the _from() _method.

<?php
// ...

class RandomEmail extends Mailable
{
    // ...
    public function build()
    {
        return $this->from('[email protected]')
                    ->view('mail.random-email');
    }
}

Next, let’s add a subject.

<?php
// ...

class RandomEmail extends Mailable
{
    // ...
    public function build()
    {
        return $this->from('[email protected]')
                    ->subject('Pure Randomness')
                    ->view('mail.random-email');
    }
}

Everything that we’ve done so far has been static. Our email template should be dynamic. At the very least, it should specify the name of the person receiving the email. Since the _RandomEmail _class is just a class, we can utilize the constructor to pass data to it upon instantiation.

  • Create a private property called $name.
  • Declare the $name constructor parameter.
  • Assign the $name constructor argument to the $name property.
  • Pass the $name property to the mail.random-email view.

php laravel web-development programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...