Subscribe to newsletter to receive news about discounted courses and more:

close


Laravel is one of the most popular PHP frameworks and offers a lot of features which makes developers life easier and enables for rapid development. It has a lot of helper methods which allow for quick and easy everyday development of your applications.

In this short tutorial I'll show you how to send emails in Laravel using Mailable classes. It's a feature which was introduced with Laravel 5.3 and it makes for easier mail sending. All you need to do is create a mailable class, view which will be used by the mailable and invoke the sending of the mail.

How does it work

Mailable class in Laravel will abstract the level of building emails. Meaning, mailable class is responsible for collecting data and passing that data to a view. All you have to do is instantiate the mailing API and tell it which mailable to use.

Mail::to('info@elea-online.com')->send(new NewSubscriber);

Getting started

To get you started, you should first create a mailable. Fortunately, Laravel has a great tool – artisan! Artisan is the command-line interface included with Laravel and provides a number of helpful commands that can assist you while you build your application. So fire up your terminal and run a command. The command we are looking for is „make:mail“ and it accepts 1 parameter – the name of the mailable class.

php artisan make:mail ClassName

But in this instance we need a mailable named „NewSubscriber“

php artisan make:mail NewSubscriber

This will create a new mailable class in app/mail folder with App\Mail namespace. 


Here's what it looks like.

You can notice the class consists of a constructor and a build() method which invokes a dummy view.name. Here you can add all the different paramteres you need. For instance, you can add a bcc, cc, replyTo, set email subject, etc.


We want to change the „view.name“ to be the name of our view. For this instance, I've created a view named „new-subscriber.blade.html“ which is located in resources/views/email folder.

laravel mailable folder structure
Passing data

After you got all that set up, the question is – how exactly do you use the data in the view?
Well, luckily, the answer is quite simple. Every public property in the mailable class is automatically available to your view file. So to pass data to your views you just have to make a variable public.
For example, we want to send the name of the subscriber which just signed up.

After doing that, you can just use the $user variable inside the view just like you would normally.

If for some reason you don't want to use public variables, you can also explicitly forward the data to the view, just like you would with controller. In such instance your code would look something like this.

Finally
Was this useful for you? Please take a look at some of the courses we have on this site for Laravel to learn even more!

Keep an eye on Elea Online blog posts! More cool articles about Laravel are coming soon!

If you like this content, please consider sharing it...


Tags: laravel php mailable

Nino Korent

Software Engineer

Similar Stories


Subscribe and stay on track

Join our newsletter and get news in your inbox every week! We hate spam too, so no worries about this.

mail