In this guide, you'll learn how to send emails in Laravel 12 using Gmail SMTP, explained step by step.
Laravel offers support for several email drivers such as SMTP, Mailgun, Postmark, Resend, Amazon SES, and the default Sendmail. This makes it easy to integrate both local and cloud-based email services. In this tutorial, we’ll focus specifically on configuring Gmail SMTP to send emails from your Laravel 12 application.
In this step-by-step guide, you will learn:
- How to configure Gmail SMTP with Laravel 12
- How to create a custom HTML email template
- How to send an email using this template
- And we’ll include screenshots along the way to help you follow along easily
Whether you’re just starting out or already have some experience, this guide will help you implement email functionality in your Laravel project with ease.
Steps for How to Send Mail in Laravel 12 Using Gmail SMTP
Step 1: Install Laravel 12
Step 2: Set Up Gmail App for SMTP
Step 3: Add Mail Configuration
Step 4: Create Mail Class
Step 5: Create Controller
Step 6: Create Routes
Step 7: Create Blade View
Step 8: Run Laravel App
Step 1: Install Laravel 12
If you’ve already installed Laravel, you can skip this step. To create a new Laravel project, run the following command:
composer create-project laravel/laravel send-mail-using-gmail-smtp
Step 2: Set Up Gmail App for SMTP
When the Laravel project is installed successfully, the next step is:
- Sign in with your Gmail account.
- Go to My Account
- On the sidebar, click Security.
Check 2-Step Verification. If it’s not enabled, enable it first. If 2-Step Verification is already enabled, proceed to the next step.
Search for App Passwords in the top search bar and go to the App Passwords page.
- Enter your app name (I am using SMTP) and click the Create button. A password will be generated—copy this password.
Step 3: Add Mail Configuration
Open your .env file and paste the generated password into the MAIL_PASSWORD **field. Be careful not to **include any extra spaces, as that could lead to connection errors. Add the remaining Gmail SMTP configuration as shown below:
MAIL_MAILER=smtp
MAIL_SCHEME=null
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=spvaxqyulyqumrxh
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME=itstuffsolutions
Step 4: Create Mail Class
In this step, we will create a laravel mail class. The mail class is used to handle the process of sending emails. We’ll pass the user’s email data and the view path for the email template in this class. To generate the mail class, run the command below:
php artisan make:mail sendEmail
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class sendEmail extends Mailable
{
use Queueable, SerializesModels;
public $mailData;
/**
* Create a new message instance.
*/
public function __construct($mailData)
{
$this->mailData = $mailData;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Send Email Testing',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'mail.send_Email',
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
Step 5: Create Controller
Now, create a controller to send the mail. Run the below command to create the SendEmailController:
php artisan make:controller SendEmailController
Now open App\Http\Controllers\SendEmailController.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\sendEmail;
class SendEmailController extends Controller
{
public function sendMail()
{
$mailData = [
'title'=>"Welcome to Itstuffsolutions.io",
'name'=>'Itstuffsolutions',
'url'=>'https://itstuffsolutiotions.io/post/',
];
Mail::to("[email protected]")->send(new sendEmail( $mailData));
return "Email Send Successfully";
}
}
Originally published on itstuffsolutiotions.io read full blog here
Top comments (0)