laravel
  1. laravel-send-email-with-laravel7-6-using-markdown-mailable-class

Send Email with Laravel 7/6 using Markdown Mailable Class

Sending emails is a critical part of any modern application. Laravel 7/6 makes it effortless to send beautiful emails using the Markdown Mailable Class. In this tutorial, we'll explore how to send an email with Laravel 7/6 using the Markdown Mailable Class.

Syntax

php artisan make:mail DemoEmail --markdown=emails.demo
Mail::to($email)->send(new DemoEmail($data));

Example

  1. Create a new Markdown Mailable Class using the following command:
php artisan make:mail DemoEmail --markdown=emails.demo
  1. Open the DemoEmail.php class and update the build function to include the data you want to send through the email. Here’s an example:
public function build()
{
    return $this->markdown('emails.demo')->with([
        'name' => $this->data['name'],
        'title' => $this->data['title'],
        'body' => $this->data['body'],
    ]);
}
  1. In your controller, use the following code to send the email:
use App\Mail\DemoEmail;
use Illuminate\Support\Facades\Mail;

public function sendEmail(Request $request)
{
    $data = [
        'name' => $request->name,
        'title' => $request->title,
        'body' => $request->body,
    ];

    $email = $request->email;

    Mail::to($email)->send(new DemoEmail($data));

    return redirect()->back();
}

Output

When the email is successfully sent, the recipient will receive the rendered HTML email.

Explanation

The Markdown Mailable Class provides a convenient way to create and send beautiful HTML emails. The php artisan make:mail command will generate a new class in the app/Mail directory which can be used to send the email. In the build function, you specify the view to be rendered and pass data to the template using the with method. Finally, you use the Mail facade to send the email to the specified email address.

Use

Sending email is an essential part of any application, and Laravel makes it easy with its built-in mail features. You can use this feature to send welcome emails, password reset emails, and other types of notifications to your users.

Important Points

  • With Laravel's Markdown Mailable Class, it's easy to create beautiful HTML emails.
  • The build function is used to specify the view to be rendered and to pass data to the template.
  • The Mail facade is used to send the email.
  • Laravel provides various configuration options for emails, such as specifying the email driver and SMTP settings.

Summary

In this tutorial, we explored how to send an email with Laravel 7/6 using the Markdown Mailable Class. We learned how to create a new Markdown Mailable Class, update the build function to include the required data, and send the email using the Mail facade. We also discussed various configuration options available for emails in Laravel. With Laravel's built-in mail features, sending beautiful, responsive emails is a breeze.

Published on: