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:
- Install the package via Composer:
composer require swiftmailer/swiftmailer
- 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
- 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 video 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.