Laravel send mail in Laravel 9 tutorial

Laravel send mail

Hello,

Today, I will guide you through an example of how to Laravel send mail in Laravel 9. We will explore the step-by-step process of sending emails in Laravel 9 using the SMTP driver.

Laravel 9 comes with a built-in mail configuration that allows you to send emails using various drivers. You have the option to choose from SMTP, Mailgun, Postmark, Amazon SES, and others. To use a specific driver, you need to configure it in the .env file.

In this tutorial, I will provide you with clear instructions on how to send emails in Laravel 9. You will learn how to create a blade file with a customized design and dynamic information for the email layout. Let’s dive into the step-by-step guide and start sending emails according to your requirements.

Laravel send mail in Laravel 9, follow these steps:

  1. Create a Laravel project by running the following command in your terminal:
laravel new project-name
  1. Generate a controller using the artisan command:
php artisan make:controller MailController
  1. Open the .env file in the root directory of your project and configure the email settings. Set the mail driver, host, port, username, password, and any other necessary information based on your email service provider. For example:
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=email@example.com
MAIL_PASSWORD=xxxxxxxxxx
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your-email@example.com
MAIL_FROM_NAME="${APP_NAME}"
  1. Create a blade file in the resources/views directory. For example, let’s create email.blade.php. This file will contain the HTML content of the email you want to send.
  2. Open the MailController.php file generated in step 2. Add the necessary imports at the top of the file:
use Illuminate\Support\Facades\Mail;
use App\Mail\DemoEmail;
  1. Create a method inside the MailController that will handle sending the email. For example:
public function sendEmail()
{
    $details = [
        'title' => 'Mail from Laravel',
        'body' => 'This is a demo email from Laravel 9.'
    ];

    Mail::to('recipient@example.com')->send(new DemoEmail($details));

    return "Email sent successfully!";
}
  1. Create a new Mailable class using the artisan command:
php artisan make:mail DemoEmail
  1. Open the DemoEmail.php file generated in step 7. In the build method, specify the subject and view for the email:
  1. Open the DemoEmail.php file located in the app/Mail directory.
  2. Inside the build method, update the code to pass the $details array to the view:
public function build()
{
    return $this->subject('Demo Email')
                ->view('email')
                ->with(['details' => $this->details]);
}

3. In the DemoEmail class, define a public property $details and update the constructor to accept the $details parameter:

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

class DemoEmail extends Mailable
{
    use Queueable, SerializesModels;

    public $details;

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

    // Rest of the class code...
}
  1. Customize the email.blade.php file to display the email content. You can use the provided $details array to access the data passed to the view.
  1. Create a blade file called email.blade.php in the resources/views directory.
  2. Open the email.blade.php file and add the following content:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ $details['title'] }}</title>
</head>
<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>
</body>
</html>
  1. You can now call the sendEmail method from your routes or wherever you want to trigger the email sending.
  1. Open the routes/web.php file in your Laravel project.
  2. Add the following route definition to the file:
use App\Http\Controllers\MailController;

Route::get('/send-email', [MailController::class, 'sendEmail'])->name('send.email');

Remember to configure your local development environment and use valid email credentials or services like Mailtrap for testing purposes.

These steps should help you send an email using Laravel 9. Make sure to adjust the code and configuration according to your specific requirements.


Output Image of send emails in Laravel:

send emails in laravel

Our Recommendation

Avatar of Akhand Pratap Singh

Akhand Pratap Singh

Greetings and a warm welcome to my website! I am Akhand Pratap Singh, a dedicated professional web developer and passionate blogger.

Related Post

Leave a Comment





Newsletter

Subscribe for latest updates

We don't spam.

Loading

Categories