Home >Backend Development >PHP Tutorial >How to Send Emails from Localhost Using Gmail in Laravel?

How to Send Emails from Localhost Using Gmail in Laravel?

Barbara Streisand
Barbara StreisandOriginal
2024-12-03 21:48:13569browse

How to Send Emails from Localhost Using Gmail in Laravel?

Sending Mail from Localhost with Gmail Using Laravel

This guide addresses the common challenge of sending emails from the Laravel development environment. Many developers encounter issues with this, leading to errors when testing email functionality. Here's a comprehensive solution to solve this problem:

In config/mail.php:
Ensure your settings match these:

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => '[email protected]', 'name' => 'Do not Reply'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),

In your .env file:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls

Crucial Step:

To resolve the error, you must enable two-step verification in your Gmail account and then generate an App Password. Use this App Password in the MAIL_PASSWORD field of your .env file.

Note:
After modifying the .env file, remember to run php artisan config:cache. This will update the compiled configuration cache.

The above is the detailed content of How to Send Emails from Localhost Using Gmail in Laravel?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn