Sending emails using Laravel Mailable

Nino Korent
4 min readNov 23, 2018

--

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('example@q-software.com')->send(new WelcomeEmail);

Getting started

To get you started, you should first create a mailable. Fortunately, Laravel has a great tool — artisan! Artisan is a 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 the command. The command we are looking for is „make:mail“ and it accepts 1 parameter — the name of the mailable class. In our instance, we need a mailable class named ‘WelcomeEmail’.

So our command will look like this:

php artisan make:mail WelcomeEmail

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

You can notice the class consists of a constructor and a build() method which invokes a view ‘mail.welcome’. Here you can add all the parameters you need. For instance, you can add bcc, cc, replyTo and set email subject.

namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;
public function build()
{
return $this->view('mail.welcome')
->from('hello@q-software.com', 'Q Software')
->subject('Hello & Welcome!')
->replyTo('hello@q-software.com', 'Q Software')
}
}

You can see I’ve added additional settings to mail, like from email address, email subject, In the particular case, the view ‘welcome.blade.html’ which is located in resources/views/email folder, will be picked up for rendering. In addition, from address, subject and reply-to parameters were defined.

Passing data

After you got all that set up, the question is — how exactly do you use the data in 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 a promotional discount code for our newly subscribed user.

namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;
class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;
public $gift; public function build()
{
$this->gift = Giftbox::generateDiscountCode();
return $this->view(‘email.welcome’);
}
}

In your view the $gift variable will automatically be available for use. If you have a reason not to use public properties, there’s an option to specifically send data to view, which would then look like this.

namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;
class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;
public function build()
{
$gift = Giftbox::generateDiscountCode();
return $this->view(‘email.welcome’)
->with([
'gift', $gift
]);
}
}

Sending the mailable

Finally, you want to send the email as a part of some subscriber action in your controller.

namespace App\Http\Controllers;

use App\Subscriber;
use App\Mail\WelcomeEmail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;

class SubscriptionController extends Controller
{
... /**
* Send welcoming email to new subscriber
*
* @param Request $request
* @param int $subscriberId
* @return Response
*/
public function sendSubscriberWelcome(
Request $request,
$subscriberId
)
{
$subscriber = Subscriber::findOrFail($subscriberId);

Mail::to($subscriber)->send(new WelcomeEmail);
}
...
}

How and why?

I was reading a lot about newcomers grasping the mailable functionality and they usually went for just sending raw emails using smtp services. So I’ve decided to create this short tutorial on how to send mail the Laravel way. The idea was to keep it as simple as possible and to keep it easy to follow. I hope this will help a lot of fresh developers who are just starting to learn Laravel. If this is proven to be useful I’ll make more of similar articles for Laravel and Symfony on topics that are often asked in places where I usually hang out on the interwebz, or even a few examples on the more complex stuff we work on at Q Software to keep things interesting.

Thank you for reading!

--

--