In this article, I'll create how to send email using Gmail in laravel 11. In laravel 11 I'll create step by step process for sending emails using Gmail. Laravel provides a clean, simple email API powered by the popular Symfony Mailer component.
Laravel and Symfony Mailer provide drivers for sending email via SMTP, Mailgun, Postmark, and Amazon SES. Laravel's email services are configured via your application's config/mail.php
configuration file.
So, let's see laravel 11 sends emails using Gmail, and sends emails in laravel 11 using Gmail SMTP.
In the first step, I'll install laravel 11 using the following composer command.
composer create-project laravel/laravel Laravel-11-Send-Mail
In this step, I'll configure mail in the .env file. So, add the following configuration in the .env file and change the necessary details.
.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=test@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=from@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Google considers some applications and devices to be less secure because they don't use modern security standards like OAuth 2.0.
Here are the steps to turn on access for less secure apps:
Sign in to your Gmail account: Go to the Gmail website and sign in with your email address and password.
Access your Google Account settings: Click on your profile picture in the top-right corner of the Gmail interface. Then, select "Google Account" from the dropdown menu. This will take you to your Google Account settings.
Navigate to the Security section: In your Google Account settings, find and click on the "Security" tab on the left-hand side. This will open the security settings for your Google Account.
Enable less secure app access: Scroll down the Security settings page until you find the section labeled "Less secure app access." Click on the toggle switch next to it to turn it on. If prompted, confirm your action.
Confirm changes (if necessary): Google may ask you to confirm your decision to enable less secure app access. Follow any on-screen prompts to confirm the changes.
Done: Once you've completed these steps, less secure app access will be enabled for your Gmail account. You can now use less secure apps with your Gmail account.
Now, I'll create a mail class using the artisan command.
php artisan make:mail TestMail
app/Mail/TestMail.php
<?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 TestMail 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: 'Test Mail',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.testMail'
);
}
/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments(): array
{
return [];
}
}
Then, I'll create TestMailController and send mail using Gmail in laravel. So, let's create a controller using the following command.
php artisan make:controller TestMailController
app/Http/Controllers/TestMailController.php
<?PHP
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\TestMail;
class TestMailController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$mailData = [
'title' => 'This is Test Mail',
'body' => 'Test mail from techsolutionstuff in laravel 11 using gmail.'
];
Mail::to('to@gmail.com')->send(new TestMail($mailData));
dd("Email is sent successfully.");
}
}
After creating the controller, I'll define the routes in the web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestMailController;
Route::get('test-mail', [TestMailController::class, 'index']);
Now, I'll create a blade file for the view. So, create a testMail.blade.php file in the emails folder and add the following HTML to that file.
resources/views/emails/testMail.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How to Send Email using Gmail in Laravel 11 - Techsolutiontuff</title>
</head>
<body>
<h1>{{ $mailData['title'] }}</h1>
<p>{{ $mailData['body'] }}</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.</p>
<p>Thank you</p>
</body>
</html>
In the last step, I'll run the laravel 11 application using the following command.
php artisan serve
Congratulations! You have successfully sent mail using Gmail in the laravel 11 application.
You might also like: