Laravel 11 Send Email using Gmail Example
Teams applying “Laravel 11 Send Email using Gmail Example” in a production project can use read the supporting guide to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi, Dev
In this tutorial, I'll guide you through the process of sending emails using a Gmail account within a Laravel 11 application.
Laravel 11 simplifies email configuration by offering built-in support for various email drivers. These include SMTP, Mailgun, Postmark, Amazon SES, and more. For this demonstration, we'll focus on utilizing the Google Gmail mailer driver to facilitate email transmission.
Here's a comprehensive step-by-step guide:
Step for Laravel 11 Send Email using Gmail Example
Step 1: Install Laravel 11
Step 2: Make Configuration
Step 3: Create Mail Class
Step 4: Create Controller
Step 5: Create Routes
Step 6: Create Blade View
Run Laravel App
Step:1 Install Laravel 11
This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app Step 2: Make Configuration
In the second step, you have to add the mail configuration. Set the mail driver as "gmail", the mail host, mail port, mail username, and mail password. Laravel 11 will use these sender details for emails. You can simply add them as follows:
.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"Make sure you have to enable Google security setting from your Gmail. Go to Google account and click on ‘Account’. Once you are on the ‘Account’ page, click on ‘Security’. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set it as ON.
Step 3: Create Mail Class
In this step, we will create a mail class called `DemoMail` for sending emails. Here, we will write code for which view will be called and the object of the user. So let's run the below command.
php artisan make:mail DemoMail Now, let's update the code in the `DemoMail.php` file as follows:
app/Mail/DemoMail.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 DemoMail 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: 'Demo Mail',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.demoMail'
);
}
/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments(): array
{
return [];
}
} Step 4: Create Controller
In this step, we will create a `MailController` with an `index()` method where we will write code to send an email to a given email address. So first, let's create the controller by executing the following command and update the code in it:
php artisan make:controller MailController Now, update the code in the MailController file.
app/Http/Controllers/MailController.php
<?PHP
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\DemoMail;
class MailController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$mailData = [
'title' => 'Mail from NiceSnippets.com',
'body' => 'This is for testing email using smtp.'
];
Mail::to('[email protected]')->send(new DemoMail($mailData));
dd("Email is sent successfully.");
}
}Step 5: Create Routes
In this step, we need to create routes for a list of sending emails. So, open your "routes/web.php" file and add the following route.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MailController;
Route::get('send-mail', [MailController::class, 'index']);Step 6: Create Blade View
In this step, we will create a blade view file and write the email that we want to send. Now, we will just write some dummy text. Create the following files in the "emails" folder.
resources/views/emails/demoMail.blade.php
<!DOCTYPE html>
<html>
<head>
<title>NiseSnippets.com</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. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Thank you</p>
</body>
</html>Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serveNow, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/send-mail
I hope it can help you...
# Laravel 11
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial