How to to send mail using gmail in Laravel?

To send email using Gmail in Laravel, you can use the built-in Swift Mailer library. Here are the steps you can follow:

  1. Install the package via Composer:
composer require swiftmailer/swiftmailer
  1. In your .env file, set the following values to configure your Gmail account:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=your_gmail_password
MAIL_ENCRYPTION=tls
  1. In your Laravel application, you can use the built-in Mail facade to send emails. Here's an example of how you can send an email:
use Mail;

public function sendEmail()
{
    $data = [
        'title' => 'Hello from Laravel',
        'content' => 'This is a test email sent from Laravel using Gmail.'
    ];

    Mail::send('emails.test', $data, function($message) {
        $message->to('[email protected]', 'Recipient Name')
                ->subject('Test Email');
    });
}

Watch a course Learn object oriented PHP

This will send an email to the specified recipient with the subject "Test Email" and the contents of the emails.test view as the email body.