Laravel 8.x Mailable
By: Dino Cajic
Tags: PHP, Laravel, Programming, Web Development, Software Development

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.

Prerequisites

First, create a new application. If you need assistance, you can read my article on creating a new application below.

Next, 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.

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

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

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

mail/random-email.blade.php view.

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.

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.

Next, let’s add a subject.

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.

Modify the random-email so that it can accept the $name variable. We’ll use the {{ $name }} blade syntax to render the $name.

// resources/views/mail/random-email.blade.php Hey there {{$name}}, This is just a random email. Randomly,

Dino Cajic

We’re ready to call the RandomEmail Mailable class and send the email. We’ll start off by creating a route. Open routes/web.php and create a random route. Inside of your callback,

Create the $name variable.

Use the Mail facade.

Specify to who you want to send the email to inside of the to() method.

Chain the send() method. The send() method accepts the RandomEmail instance as an argument.

Pass the $name variable to the RandomEmail class.

Time to test it. Start up your server: php artisan serve and navigate to the random route: localhost:8080/random . If you get a blank screen, check your MailTrap inbox. It’s a blank screen since we’re not passing any HTML back from the route.

Congratulations, it worked. Our formatting was lost, but that’s because it was a pure text file. Let’s add some HTML to the random-email view.

If we refresh the page, we should have a properly formatted HTML email.

Using Markdown

We could use the optional Markdown template to streamline this process even further. According to the Laravel documentation, “Markdown mailable messages allow you to take advantage of the pre-built templates and components of mail notifications in your mailables. Since the messages are written in Markdown, Laravel is able to render beautiful, responsive HTML templates for the messages while also automatically generating a plain-text counterpart.”

Let’s create another Mailable class, but this time with the Markdown template option. To generate a Markdown template file, you will need to pass the -m flag and the new markdown view location. In this instance, we want the view to be stored under resources/views/emails/ and we’ll name it funny.blade.php.

php artisan make:mail FunnyEmail -m emails.funny

The command generate the FunnyEmail Mailable class inside of the app/Mail directory and a resources/views/emails/funny.blade.php view.

If we open up Mail/FunnyEmail, we can see that the build() method returns a markdown.

Let’s take a look at the funny.blade.php markdown view.

We can now modify the FunnyEmail Mailable class to chain the from() and subject() methods like we did before. But, before we do that, wouldn’t it be nice to be able to see our email before we send it? Let’s do it.

In your web.php file, return a new instance of the FunnyEmail Mailable class.

Start your server and visit the funny page. You should see something like this.

Pretty nice looking already.

“Markdown mailables use a combination of Blade components and Markdown syntax which allow you to easily construct mail messages while leveraging Laravel’s pre-crafted components.”

Let’s modify the funny view (resources/views/emails/funny.blade.php).

Refresh your funny page.

At the bottom of the page, you see the 2020 Laravel copyright. To change that, simply modify the APP_NAME variable in your .env file. I changed mine to Mailable. If I refresh the page, the copyright has been updated. If you’re using php artisan serve , you will have to stop your server and restart it to see the changes take effect.

You’ll notice that the Laravel logo was also replaced with your App Name. If you wanted to modify the template even further, like changing the footer, you’ll first have to publish the laravel-mail component and then edit the necessary files. Run the vendor:publish command and enter the tag number next to laravel-mail.

php artisan vendor:publish

...

13

We can see the files that have been made available to us in the newly created resources/views/vendor/mail directory.

To modify the footer, open up mail/html/footer.blade.php. You should see the following:

Of course, you can modify the footer content directly from here, but if you wanted to know exactly where the copyright came from, it’s actually located in messages.blade.php (resources/views/vendor/mail/html/message.blade.php). Open it up and you’ll see your footer content there.

Change the All rights reserved to Funny rights reserved. Save the file and refresh your page.

To modify the CSS, modify the resources/views/vendor/mail/themes/default.css file.