Tech AI Insights

Markdown Mailables: When to Use view() vs markdown()

Markdown Mailables

Markdown provides pre-built templates in Laravel, saving time by eliminating the need to create email layouts from scratch. With view(), you must manually design the Blade template, whereas markdown() offers built-in, well-formatted components.

In simple terms:

– Use view() when you need a fully customized email layout.
– Use markdown() for quick, responsive, and pre-styled email templates.

Difference Between view() and markdown() in Laravel Emails

1. view() – Using a Blade Template

  • You need to handle the styling and formatting yourself.
  • The view() method loads a Blade template.
  • Use for fully customized email layout.

Example: Using view()

01
02
03
04
05
06
07
08
09
10
11
public function build()
{
    return $this->from('your_email@gmail.com')
        ->subject(sprintf('Project Released - %s', $this->projectName))
        ->view('emails.project-released')
        ->with([
            'userName' => $this->userName,
            'projectName' => $this->projectName,
            'projectLink' => $this->projectLink
        ]);
}

Blade Template (resources/views/emails/project-released.blade.php)

01
02
03
04
05
06
07
08
09
10
11
<!DOCTYPE html>
<html>
<head>
    <title>Project Released</title>
</head>
<body>
    <h1>Hello, {{ $userName }}</h1>
    <p>User project "<strong>{{ $projectName }}</strong>" has been released.</p>
    <p>You can check it here: <a href="{{ $projectLink }}">{{ $projectLink }}</a></p>
</body>
</html>

2. markdown() – Using Laravel Markdown Templates

  • The markdown() method uses Laravel’s built-in email templates.
  • Easier to write & maintain
  • Limited, but structured
  • For quick, responsive, and pre-styled email templates.

Example: Using markdown()

01
02
03
04
05
06
07
08
09
10
11
public function build()
{
    return $this->from('your_email@gmail.com')
        ->subject(sprintf('Project Released - %s', $this->projectName))
        ->markdown('emails.project-released')
        ->with([
            'userName' => $this->userName,
            'projectName' => $this->projectName,
            'projectLink' => $this->projectLink
        ]);
}

Markdown Template (resources/views/emails/project-released.blade.php)

01
02
03
04
05
06
07
08
09
10
11
12
13
@component('mail::message')
 
Hello, {{$userName}}
 
User project "{{ $projectName }}" has been released.
 
You can check it here:[link]({{$projectLink}})
 
@endcomponent
 
Best regards, 
{{ config('app.name') }}
@endcomponent

Quick and well-styled emails without writing custom HTML.

@component('mail::message') and @component('mail::button') These are predefined components in Laravel. You can use the predefined component button for the link. In my example above, I added the link to the email body using [link]({{$projectLink}}).

What Do They Do?

✅ @component('mail::message')

– It wraps your email content and automatically applies a nice design.
– Laravel adds styles like padding, fonts, and background color.

✅ @component('mail::button', ['url' => 'your-link'])

– It creates a button with a link inside the email.
– The button is styled automatically (blue with white text by default).

How its workes
Styles the email body (mail::message)
Creates a button (mail::button)

Best Practice:

  1. If you’re building a simple email with links and buttons, use markdown().
  2. If you need a fully customized design, use view().