Laravel mail markdown
In Laravel, you can use Markdown to format your email messages using the Illuminate\Mail\Message
class and the Markdown
class from the Illuminate\Mail\Markdown
namespace.
Here's an example of how you can use Markdown in your email messages:
Step 1: Install the illuminate/mail
package
If you haven't already, install the illuminate/mail
package using Composer:
composer require illuminate/mail
Step 2: Create a Markdown email message
In your Laravel controller, create a new instance of the Illuminate\Mail\Message
class and set the subject
and view
properties:
use Illuminate\Mail\Message;
use Illuminate\Mail\Markdown;
$message = new Message();
$message->subject('Hello from Laravel!');
$message->view('emails.hello', ['name' => 'John Doe']);
In this example, we're creating a new email message with a subject of "Hello from Laravel!" and a view named emails.hello
. The view
property takes an array of data to pass to the view.
Step 3: Use Markdown in your email view
In your email view (e.g., emails.hello.blade.php
), you can use Markdown syntax to format your email message:
Hello {{ $name }},
This is a test email sent using Laravel's Markdown email feature.
Best regards,
{{ $name }}
In this example, we're using Markdown syntax to format the email message. The {{ $name }}
syntax is used to inject the name
variable passed to the view.
Step 4: Send the email
Finally, send the email using the Illuminate\Mail\MailFacadin
class:
use Illuminate\Mail\MailFacadin;
$mailFacadin = app(MailFacadin::class);
$mailFacadin->send($message);
That's it! When you send the email, Laravel will render the Markdown view and send the formatted email message to the recipient.
Tips and Variations
- You can use Markdown syntax in your email views to format text, links, images, and more.
- You can also use Laravel's built-in email templates (e.g.,
emails.default
) as a starting point for your email views. - If you need to use Markdown in a specific part of your email message, you can use the
Markdown
class to render the Markdown content separately:$markdown = new Markdown(); $markdown->render('Hello {{ $name }}!');