There is a clear simple mail option in Laravel 10. So why do you use markdown mailable class mail? Cause markdown mailable messages allow us to take advantage of the pre-built templates and components of mail in our mailable. We do not need to define manually the mail template path of the email component. 

 So in this tutorial, we will look up about Laravel 10 markdown mailable class and we will see all the options of markdown mailable. Then we will send an example mail to the user using the markdown mailable class in Laravel 10.

In Laravel 10 mailable class, there are some methods like content(), attachments(), and envelope(). Content for sending data with the view. Envelope for setup reply to and who is sender with subject information and attachments for sending the file. Now see the preview mail example which is from mail trap inbox account to test our markdown mailable class:

laravel-10-markdown-mail-example

Step 1: Install Laravel 10

In the first step, we need a fresh Laravel 10 application. So open the terminal and run the below command:

composer create-project laravel/laravel example-app

 

Step 2: Make Configuration

For this tutorial purpose, we will use demo credentials from mail trap so that we can send mail from the localhost server. Update the env file with the following credentials.

.env

MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=bcca41cf7e855f
MAIL_PASSWORD=c1abf4f6f9596d
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="laravelia@tutorial.com"
MAIL_FROM_NAME="${APP_NAME}"

 

Read also: Laravel 10 Available Mail Options With Send Example

 

Step 3: Create Markdown Mail

There is an artisan command to create a markdown mailable class in Laravel. We can run make:mail command to create a mail class. Run the below command to create a mail class.

php artisan make:mail MarkDownMailTest --markdown=emails.markdown.welcome

 

This command will generate a markdown mailable class with a markdown component. Now update it like this:

Now, let's update the code on the MarkDownMailTest.php file as below:

app/Mail/MarkDownMailTest.php

<?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;
use Illuminate\Mail\Mailables\Address;

class MarkDownMailTest extends Mailable
{
    use Queueable, SerializesModels;

    public $data;

    /**
     * Create a new message instance.
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            from: new Address('laravelia@example.com', 'Mahedi Hasan'),
            subject: 'Laravel Ten Test Mail',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            markdown: 'emails.markdown.welcome',
            with: [
                'data' => $this->data
            ]
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}

 

If you would like, you may also specify a replyTo address:

return new Envelope(
    from: new Address('laravelia@example.com', 'Laravelia'),
    replyTo: [
        new Address('test@example.com', 'test'),
    ],
    subject: 'Good Friendship',
);

 

Step 4: Add Routes

We are not going to create any controllers. We will send mail from the web.php file. So update the file like below:

routes/web.php

<?php

use App\Mail\MarkDownMailTest;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;

Route::get('laravel_ten_markdown_test_mail', function () {
    $data = "We are learning laravel 10 markdown mail from laravelia.com";

    Mail::to('laravelia@test.com')->send(
        new MarkDownMailTest($data)
    );

    dd('Mail send successfully.');
});

 

Step 5: Update Markdown Email View

In this step, we will create a blade view file and write the email that we want to send. now we just echo out our dynamic message like:

resources/views/emails/welcome.blade.php

<x-mail::message>
# Welcome

{{ $data }}

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

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

 

Markdown panel component example:

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

 

Markdown table component example:

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

 

If you want to customize the markdown mail component then run the below command:

php artisan vendor:publish --tag=laravel-mail

 

This command will publish the Markdown mail components to the resources/views/vendor/mail directory. Now you can customize it like what you wanted. 

 

Customizing The Markdown Mail CSS

If you would like to customize the markdown mail CSS, then visit this path resources/views/vendor/mail/html/themes directory will contain a default.css file.

 

Now you can test by visiting the below URL:

URL
http://127.0.0.1:8000/laravel_ten_markdown_test_mail

 

Read also: How To Send Email In Laravel With Multiple CC And BCC

 

Conclusion

Now we know how to create a markdown mail component with a markdown mailable class. Now we also see how to send an email to the user using the markdown mailable class with the default component and we saw how to customize the markdown email component in a Laravel way. Hope this Laravel 10 markdown mail tutorial will help you.

Category : #laravel

Tags : #laravel , #laravel mail