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.
To send email using Gmail in Laravel, you can use the built-in mailer. Here are the steps you can follow:
-
Laravel includes the mailer out of the box, so no additional Composer package is required.
-
In your
.envfile, set the following values to configure your Gmail account:
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=your_gmail_password
MAIL_ENCRYPTION=tlsNote: Gmail requires an App Password for authentication. Enable 2-Step Verification in your Google Account and generate an App Password to use here.
- In your Laravel application, you can use the built-in
Mailfacade to send emails. Here's an example of how you can send an email:
Example of sending email using Gmail in Laravel
use Illuminate\Support\Facades\Mail;
class EmailController extends Controller
{
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');
});
}
}
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
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.