Sending Email in Laravel using SMTP server of Google Gmail

Sending email with Gmail via a third-party application has undergone several changes for security reasons. Now to send email using Google SMTP you need to activate 2 step verification, the steps we will take this time may change in the future, but Google will definitely provide us with information via email or news.

This time we will make Laravel able to send emails using a Gmail account with the following steps:

Step 1 : Create laravel project

This step is optional, if you already have an existing Laravel project, you can skip this step and go directly into your Laravel project.

Open a terminal or command prompt and type the following command.

composer create-project laravel/laravel laravel-mail
cd laravel-mail

Step 2 : Create App Password for Gmail SMTP Account

Important: To create an app password, you need 2-Step Verification on your Google Account.

If you use 2-Step-Verification and get a “password incorrect” error when you sign in, you can try to use an app password.

  1. Go to your Google Account.
  2. Select Security.
  3. Under “Signing in to Google,” select 2-Step Verification.
  4. At the bottom of the page, select App passwords.
  5. Enter a name that helps you remember where you’ll use the app password.
  6. Select Generate.
  7. To enter the app password, follow the instructions on your screen. The app password is the 16-character code that generates on your device.
  8. Select Done.
Image 1
Image 2
Image 3

Save your application password which will be used as a password for your Laravel configuration.

Step 3 : Update env. file

Open the .env file and enter the following code according to your Gmail account

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

Step 4 : Create Mail Class

Go to your terminal or command prompt and type the following command.

php artisan make:mail TestMail

The command above will create a file at ./app/Mail/TestMail.php then open it and type the following code

<?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;
use Illuminate\Mail\Mailables\Address;

class TestMail extends Mailable
{
use Queueable, SerializesModels;

public $data;

/**
* Create a new message instance.
*/

public function __construct( $data )
{
$this->data = $data;
}

/**
* Get the message envelope.
*/

public function envelope(): Envelope
{
return new Envelope(
subject: 'Test Mail',
from: new Address('test@mail.dev', 'Test Mail'),
);
}

/**
* Get the message content definition.
*/

public function content(): Content
{
return new Content(
view: 'test-mail',
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/

public function attachments(): array
{
return [];
}
}

Step 5 : Create Test Mail Controller

Go to your terminal or command prompt and type the following command.

php artisan make:controller MailController

The command above will create a file at ./app/Http/Controllers/MailController.php then open it and type the following code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mail;
use App\Mail\TestMail;

class MailController extends Controller
{
public function index()
{
Mail::to('your_test_mail@gmail.com')->send(new TestMail([
'title' => 'The Title',
'body' => 'The Body',
]));
}
}

Step 6: Create Routes

Open file ./routes/web.php and change it to the following code

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MailController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/


Route::get('/', function () {
return view('welcome');
});

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

Step 7: Create Blade View

Create file ./resources/views/test-mail.blade.php and type the following code

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

Step 8 : Run The Application

Go to your terminal or command prompt and type the following command.

php artisan serve

Enter the address http://127.0.0.1:8000/send-mail in the browser and check your destination email to see if the email has arrived.

Happy Coding.

Comments

Popular posts from this blog

UI/UX - All you need to know

Best TikTok Alternatives for Android & iOS

Earn Money from this new Affiliate Program