Laravel provides a powerful and clean API over the SwiftMailer library with drivers for Mailgun, SMTP, Amazon SES, SparkPost, and sending an email. With this API, we can send emails on a local server as well as the live server.

How to use mail() in Laravel?

Step 1. Add Mail Configurations in .env file

MAIL_DRIVER = smtp
MAIL_HOST = smtp.gmail.com
MAIL_PORT = 587
MAIL_USERNAME = email
MAIL_PASSWORD = password
MAIL_ENCRYPTION = tls

Step 2. Create email templates

Laravel allows us to store email messages in our view files. For example, to manage our emails, we can create an email directory within our resources/views directory.

Step 3. Use mail() in controllers

public function sendEmail(Request $request, $id)
{
      $user = Users::find($id);
      Mail::send('emails.reminder', ['user' => $user], function ($mail) use ($user)  {
           $mail->from('[email protected]', 'Feedback');
           $mail->to($user->email, $user->name)->subject('Thanks Message');
      });
}

BY Best Interview Question ON 18 Sep 2022

Suggest An Answer