Laravel Mail – Sending Mailable, Markdown Mailable, and Raw HTML

Larvel provides several ways of sending the email. That could be through mailable, custom email template or through raw HTML. Laravel has used the popular SwiftMailer library in the backend. Laravel also provides built-in drivers for SMTP, Mailgun, SparkPost, Postmark, Amazon SES and sendemail.

Table of content

Let’s dive into the various topics one by one to understand that clearly.

Laravel Mail with Mailable

The mailable feature was introduced in Laravel 5.3 to simplifying the way to sending emails. Laravel introduced the very simple command to create mailable class i.e.

php artisan make:mail SampleEmail

The above command will create a class SampleEmail.php at app/Mail directory. Don’t worry if the directory does not exist in the app directory. The make:mail command will automatically check if the Mail directory exists or not. The structure of the file would be similar to the below codes.

namespace App\Mail;

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

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

Take a close look on build() method, because this is the place where we can add the subject, view, from or attachment. Let’s modify the existing build method.

....

use Queueable, SerializesModels;

public $subject;

public $data;

/**
 * Create a new message instance.
 *
 * @param string $subject Email Subject
 * @param mixed $data    Template data
 *
 * @return void
 */
public function __construct($subject, $data)
{
    $this->subject = $subject;
    $this->data    = $data;
}


public function build()
{
    return $this->subject($this->subject)
        ->view('emails.sample_email', [
            'data' => $this->data
        ]);
}

....

In the above codes, we have defined two custom properties $subject and $data. The $subject property is used to define the Email Subject and the $data property is used to add the custom data like name, email or message into the template. We have attached the template emails.sample_email, which means the blade template exists on resources/views/emails/sample_email.blade.php. Now let’s send the email.

$subject       = 'Welcome to Laravel Mailable';
$receiverEmail = 'mail@example.com';
$receiverData  = \App\User::where('email', $receiverEmail)->first();

Mail::to($receiverEmail)->send(new \App\Mails\SampleEmail($subject, $receiverData));

Laravel Mail with Markdown Mailable

Markdown mailable was introduced in Laravel 5.4. Markdown mailable allows you to take advantages of the pre-built templates and components. Markdown mailable will automatically generate the beautiful and responsive HTML template. Let’s generate the mailable with a corresponding Markdown template.

php artisan make:mail SampleMarkdownEmail --markdown=emails.sample_markdown_email

The --markdown parameter asks the email directory path and the template name. And generate the template similar to the below codes.

@component('mail::message')
# Introduction

The body of your message.

@component('mail::button', ['url' => ''])
Button Text
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

If you are not familiar with the Markdown tags the please take a look on the markdown documentation.

The SimpleMarkdownEmail class will look like the same as the mailable class except for the markdown() method. You can use the subject, from, etc., the same way as the previous.

public function build()
{
    return $this->markdown('emails.sample_markdown_email');
}

Take a look at the documentation to know more about the existing markdown. You can also take a look at how to customize the header and footer of the markdown mailable.

Laravel Mail with Raw HTML

There are some cases when we have to use the raw HTML instead of Mailable or Markdown classes. This is how we can attach the raw HTML with Laravel Mail class.

Mail::send([], [], function($message) use ($data) {
    $message->from($data['from']);
    $message->to($data['to']);
    $message->subject($data['subject']);
    $message->setBody($data['content'], 'text/html');
});

Posted by Jogesh Sharma

Jogesh Sharma is a web developer and blogger who loves all the things design and the technology, He love all the things having to do with PHP, WordPress, Joomla, Magento, Durpal, Codeigniter, jQuery, HTML5 etc. He is the author of this blog.