How to send email with HTML template using gmail SMTP in Laravel 8

In this post, I will let you know how to send email in laravel using Gmail SMTP in simple and easy steps. Laravel 8 provides several ways to send an email and you can use any email service provider. First of all, you need to install laravel and after that need to follow the following steps.

Step 1: .env Configuration

Set up the SMTP configuration in your .env file and you can find out the .env file in the root directory of the project. You can use your Gmail login email and password in MAIL_USERNAME, MAIL_PASSWORD but if you want to prevent your email password from attackers then you should use App Password. As I suggest you must use the App Password in place of the Gmail login password.

You can generate App Password from your Gmail account using a few steps. Here is a reference link:

Note: In case, you are not able to see the .env file in the root directory of your project then you should show the hidden file because the .env file is a hidden file. If you are only able to see .env.example then need to create a .env file first.


Step 2: Create a Controller

Now, you need to create a controller with any name or if you want to use any existing controller then you can use it.
I am creating here controller named as UserController with the help of the following command.

php artisan make:controller UserController

Once your controller is created you are able to see some default codebase you can replace and modify by using the following code.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mail;

class UserController extends Controller
    public function testSendMail()
        $data = array('name' => "User");

        Mail::send('mail', $data, function($message) {
            $message->to('', 'Welcome Mail')
                    ->subject('Laravel HTML Testing Mail');
            $message->from('', 'Programming With Ajay');

        dd('Mail Send Successfully');

Step 3: Create a Route

After creating the controller you need to set a route for it. You need to add the following code to the web.php file. You can find the web.php file in the routes directory.

use Illuminate\Support\Facades\Route;

| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

Route::get('/sendemail', [App\Http\Controllers\UserController::class, 'testSendMail']);

Step 4: Create a Blade file

Now, you need to create a blade file for the email template and the name should be mail.blade.php . Because we had set the mail name in the controller. You can change it as per your requirements.

<h1>Hi, {{ $name }}</h1>
<p>Sending dynamic data Mail from Laravel.</p>

After this, you need to run the following URL, and the server URL may be different in your case. So, you must run your server base URL with the route name sendemail .


Hope this blog is helpful to you. If you have any queries or feedback please comment to me.

How to install Laravel 7, 8, 9 by composer

Server Requirement:

The Laravel framework has a few system requirements. If you are installing laravel 7. If you are trying to install Laravel by composer then it will automatically pick the laravel version according to your PHP version. But you can specify the version while installing it using composer.

  • PHP >= 7.2.5
  • BCMath PHP Extension
  • Ctype PHP Extension
  • Fileinfo PHP extension
  • JSON PHP Extension
  • Mbstring PHP Extension
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

Step 1:

Visit the following URL and download composer to install it on your system.

Note: In case, the composer already installed your system then you don’t need to run the first step.

To verify composer is installed or not, by running  composer the command in terminal/cmd

Step 2:

You can install a specific version using the following command. Just need to change the version in place of 7.0

composer create-project --prefer-dist laravel/laravel:^7.0 blog

Install laravel according to the installed PHP version in your system by the following command.

composer create-project --prefer-dist laravel/laravel blog

It will take few minutes according to your internet speed.

Note: the blog is your folder/directory name, So you can change it as per your choice.


Step 3:

First, you need to go blog directory by the following command.

cd blog

Start the Laravel service by executing the following command. This command will start a development server at http://localhost:8000

php artisan serve


Copy the URL from the terminal in the above screenshot and open that URL in the browser. If you see the following screen, it implies Laravel has been installed successfully.

The above screenshot URL might be different in your case so use them. Also, the Web screen may be different for you due to the different versions of Laravel.




Hope this blog is helpful to you. If you have any queries or feedback please comment to me.