Markdown Mail

Laravel Mail: Markdown Mail

Generating Markdown Mailables

php artisan make:mail OrderShipped --markdown=emails.orders.shipped
<?php

namespace App\Mail;

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

class OrderShipped extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * The order instance.
     *
     * @var \App\Models\Order
     */
    protected $order;

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

    /**
     * Get the message envelope.
     *
     * @return \Illuminate\Mail\Mailables\Envelope
     */
    public function envelope()
    {
        return new Envelope(
            subject: 'Order Shipped',
        );
    }

    /**
     * Get the message content definition.
     *
     * @return \Illuminate\Mail\Mailables\Content
     */
    public function content()
    {
        return new Content(
            markdown: 'emails.orders.shipped',
            with: [
                'orderName' => $this->order->name,
                'orderPrice' => $this->order->price,
            ],
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array
     */
    public function attachments()
    {
        return [
            // Attaching Files From Local
            Attachment::fromPath('/path/to/file'),
            Attachment::fromPath('/path/to/file')
                ->as('name.pdf')
                ->withMime('application/pdf'),
            // Attaching Files From Disk
            Attachment::fromStorage('/path/to/file'),
            Attachment::fromStorage('/path/to/file')
                ->as('name.pdf')
                ->withMime('application/pdf'),
            // Attaching Files From S3 Disk
            Attachment::fromStorageDisk('s3', '/path/to/file')
                ->as('name.pdf')
                ->withMime('application/pdf'),
        ];
    }
}
<div>
    Price: {{ $orderPrice }}
</div>

Component Example

<x-mail::message>
# Introduction

The body of your message.

<x-mail::button :url="''">
    Button Text
</x-mail::button>

<x-mail::button url="/order" color="success">
    View Order
</x-mail::button>

<x-mail::table>
| Laravel       | Table         | Example  |
| ------------- |:-------------:| --------:|
| Col 2 is      | Centered      | $10      |
| Col 3 is      | Right-Aligned | $20      |
</x-mail::table>

<x-mail::panel>
    This is the panel content.
</x-mail::panel>

<x-mail::subcopy>
    This is the subcopy content.
</x-mail::subcopy>

Thanks,<br>
{{ config('app.name') }}
</x-mail::message>

Markdown Mail Example

Component Usage Hint

The email component MUST NOT have the space before it, otherwise the layout will be ruin.

Correct component space indent

Without space in front of the component

<x-mail::message>

<x-mail::panel>
    This is the panel content.
</x-mail::panel>

<x-mail::subcopy>
    This is the subcopy content.
</x-mail::subcopy>

Thanks,<br>
{{ config('app.name') }}
</x-mail::message>

Markdown Mail Example

Wrong component space indent

The space is in front of the component

<x-mail::message>

    <x-mail::panel>
        This is the panel content.
    </x-mail::panel>

    <x-mail::subcopy>
        This is the subcopy content.
    </x-mail::subcopy>

Thanks,<br>
{{ config('app.name') }}
</x-mail::message>

Markdown Mail Example

Attachement

Attaching Files From Local

/**
 * Get the attachments for the message.
 *
 * @return array
 */
public function attachments()
{
    return [
        // Attaching Files From Local
        Attachment::fromPath('/path/to/file'),
        Attachment::fromPath('/path/to/file')
            ->as('name.pdf')
            ->withMime('application/pdf'),
    ];
}

Attaching Files From Disk

/**
 * Get the attachments for the message.
 *
 * @return array
 */
public function attachments()
{
    return [
        // Attaching Files From Disk
        Attachment::fromStorage('/path/to/file'),
        Attachment::fromStorage('/path/to/file')
            ->as('name.pdf')
            ->withMime('application/pdf'),
        // Attaching Files From S3 Disk
        Attachment::fromStorageDisk('s3', '/path/to/file')
            ->as('name.pdf')
            ->withMime('application/pdf'),
    ];
}

Inline Attachments

<body>
    Here is an image:
 
    <img src="{{ $message->embed($pathToImage) }}">
</body>

Embedding Raw Data Attachments

<body>
    Here is an image from raw data:
 
    <img src="{{ $message->embedData($data, 'example-image.jpg') }}">
</body>