Sending Email in Laravel with Mailables

I recently found out about a feature in Laravel 5.3 as I was working with email. That was when I discovered Laravel Mailables. This is a great, clean way to send email; especially if you’re planning to send email more than one model.

How To Work with Mailables

Creating a “mailable” class is as easy as running this artisan command:

php artisan make:mail MyFunkyEmail

This will generate a class within the app\Mail directory. You’ll see these contents when you open the MyFunkyEmail class:

<?php

namespace App\Mail;

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

class MyFunkyEmail 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');
  }
}

In the constructor, you can pass in your model via dependency injection and other arguments. Let’s say we have a project model, we can add this in our constructor like so:

public $project

/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct(Project $project)
{
    $this->project = $project;
}

Also, don’t forget to add in your model class.

   use App\Project;

We can also chain other methods such as subject(), from(), etc. within the build method.

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->subject('Hello From Funky Email')
                ->from('funkytowninc@funkycorp.com')
                ->view('email.funkymail');
}

Perfect! Now within one of our controllers, we can call this class via the Mail facade.

 // let's say we are creating a new project

 $project = Project::create([
   // data and stuff
 ]);

 Mail::to('hello@email.com')->send(new MailRequester($project));

Attachments

I also had to deal with attachments. This wasn’t too difficult to accomplish. What I did was added another parameter within the constructor of the Mailable class and then I could pass it in the build method.

public $project;

public $attachment;

/**
 * Create a new message instance.
 *
 * @return void
 */
public function __construct(Project $project, $attachment)
{
    $this->project = $project;
    $this->attachment = $attachment;
}


public function build()
{
    return $this->subject('Project Requested')
                ->from('some@email.com')
                ->view('email.funkymail')
                ->attach($this->attachment);
}

Then all I had to do was pass the second argument in my controller.

$file = '/some/file/path';
Mail::to('hello@email.com')->send(new MailRequester($project, $file));

Now we are all gravy! 🙂

Closing Thoughts

Mailables are a neat why of keeping your controllers lean and clean. Also very useful for when you need to send the same email in a different controller. Less typing == happy developer. Hope this post got you interested in Laravel Mailbables! Happy coding!

Cheers.

Tyler Souza

Tyler is a very passionate full-stack developer who thrives on a challenge. He specializes in programming (mainly in Python), REST API development, and keeps up with the latest front-end technologies. When not coding, he loves to eat ramen, BBQ, and travel.

You may also like...