Laravel Gmail SMTP Setup

User
laravel gmail smtp app password setup
Assistant
To set up Gmail SMTP with an App Password in your Laravel application, follow these steps:

### 1. Enable Two-Factor Authentication (2FA) on Gmail
– Go to your Google account's [Security settings](https://myaccount.google.com/security).
– Under "Signing in to Google," enable **2-Step Verification**.

### 2. Create an App Password
– Once 2-Step Verification is enabled, go back to the **Security** settings.
– Look for the **App Passwords** section and select it.
– Choose **Mail** as the app and **Other (Custom name)** for the device name (you can call it something like "Laravel SMTP").
– Click **Generate** to get your App Password. **Copy** this password as you will need it for Laravel.

### 3. Configure .env for SMTP
Add the following to your Laravel application's `.env` file, replacing the values with your Gmail credentials and App Password:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_gmail_address@gmail.com
MAIL_PASSWORD=your_app_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_gmail_address@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

### 4. Test the Configuration
You can test sending an email by using Laravel’s default Mail functionality. Try using `php artisan tinker`:

use IlluminateSupportFacadesMail;

Mail::raw('Test email from Laravel with Gmail SMTP.', function ($message) {
    $message->to('recipient@example.com')->subject('Test Email');
});

This setup should allow you to send emails using Gmail's SMTP with Laravel. If you encounter issues, check your Gmail account for security alerts, as Google may sometimes flag external SMTP attempts.

Leave a Comment

Your email address will not be published. Required fields are marked *