📧 Laravel 12 Send Emails Using Gmail’s SMTP Server (Step by Step Guide)

Sending emails is a core part of most web applications. In this tutorial, we’ll walk you through how to send emails in Laravel 12 using Gmail’s SMTP server with clear steps and sample images.


🛠️ Step 1: Install Laravel 12 Project

If you haven’t created a Laravel project yet, start with this:

composer create-project laravel/laravel laravel-email-app cd laravel-email-app

🔑 Step 2: Enable Less Secure App Access in Gmail

To use Gmail SMTP, you need to allow access:

  1. Go to Google Account Security.
  2. Enable 2-Step Verification and generate an App Password.
  3. Use the app password in your .env file.


📝 Step 3: Configure .env File

Open the .env file in your project root and update mail configuration:

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


📤 Step 4: Create Mail Class

Run the artisan command to create a Mailable class:

php artisan make:mail TestEmail

Open app/Mail/TestEmail.php and update:

namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class TestEmail extends Mailable { use Queueable, SerializesModels; public $details; public function __construct($details) { $this->details = $details; } public function build() { return $this->subject('Test Email from Laravel 12') ->view('emails.test'); } }

🖼️ Step 5: Create Email View Blade File

Create a new view file:

resources/views/emails/test.blade.php

And add:

<!DOCTYPE html> <html> <head> <title>Laravel Email</title> </head> <body> <h1>{{ $details['title'] }}</h1> <p>{{ $details['body'] }}</p> <p>Thank you!</p> </body> </html>

🧪 Step 6: Send Email via Route or Controller

Option A: Using Route

use Illuminate\Support\Facades\Mail; use App\Mail\TestEmail; Route::get('/send-email', function () { $details = [ 'title' => 'Hello from Laravel', 'body' => 'This is a test email sent using Gmail SMTP.' ]; Mail::to('recipient@example.com')->send(new TestEmail($details)); return "Email sent successfully!"; });

Option B: Using Controller

php artisan make:controller MailController
// app/Http/Controllers/MailController.php namespace App\Http\Controllers; use Illuminate\Support\Facades\Mail; use App\Mail\TestEmail; class MailController extends Controller { public function send() { $details = [ 'title' => 'Laravel Email', 'body' => 'This is a test email from Gmail SMTP.' ]; Mail::to('recipient@example.com')->send(new TestEmail($details)); return "Email Sent!"; } }

Add the route:

Route::get('/send-mail', [MailController::class, 'send']);

✅ Step 7: Test in Browser

Start your Laravel server:

php artisan serve

Open in browser:

http://127.0.0.1:8000/send-mail

If configured correctly, you’ll see:

Email Sent!


🧩 Bonus: Handle Email Failures

You can handle failures with:

if (Mail::failures()) { return 'Email Failed!'; }

📌 Conclusion

In this tutorial, you learned how to send emails from Laravel 12 using Gmail SMTP with step-by-step configuration and practical examples. Always make sure to use App Passwords for Gmail and never expose credentials in public repositories.

Post a Comment

Previous Post Next Post

Contact Form