Laravel 8 Send Mail using Gmail SMTP Server

Reading Time: 5 minutes
14,663 Views

Sending an email in web application is very common. Laravel provides the predefined Mail class used to send mail. We only need to use & configure it. The content of this article is very useful to understand the things that we need to send a normal email using SMTP.

Inside this article we will see the concept of Laravel 8 Send Mail using Gmail SMTP Server. We will use Gmail and it’s SMTP details with Laravel 8.

Send Email in Laravel 8 using Markdown Email template, Click here.

To get the complete concept of this article you need SMTP details.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

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

To start the development server of Laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


SMTP Configuration

To configure SMTP details, open up the file .env from application root. We will pass mail driver as gmail server, mail host, mail port, mail username, mail password.

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME="mygmail@gmail.com"
MAIL_PASSWORD="mypassword"
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="mygmail@gmail.com"
MAIL_FROM_NAME="Online Web Tutor"
  

Make sure you have enabled google security setting form 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 as ON.

Have a look into these images.


Create Mail Class of Laravel 8

Inside this we will create mail class via artisan command which will be responsible for sending emails.

$ php artisan make:mail MyTestMail

When you run this command via terminal, application will creates a folder with the name of Mail inside app folder. You should see a file /app/Mail/MyTestMail.php.

Open file MyTestMail.php

<?php
  
namespace App\Mail;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
  
class MyTestMail extends Mailable
{
    use Queueable, SerializesModels;
  
    public $details;
  
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }
  
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Mail from Online Web Tutor')
                    ->view('emails.myTestMail');
    }
}

Create Mail Blade Template

Let’s create a folder inside /resources/views/emails. Inside this folder simply create a blade template file with following content.

Open up the file /resources/views/emails/myTestMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Online Web Tutor</title>
</head>
<body>
  
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>
   
    <p>Thank you</p>
</body>
</html>

Configure Application Route

To configure application routes, open up the file /routes/web.php

use Illuminate\Support\Facades\Mail;

Route::get('send-mail', function () {
   
  $details = [
      'title' => 'Mail from Online Web Tutor',
      'body' => 'Test mail sent by Laravel 8 using SMTP.'
  ];
 
  Mail::to('send_to_email@gmail.com')->send(new \App\Mail\MyTestMail($details));
 
  dd("Email is Sent, please check your inbox.");
});

Application Testing

Run this command into project terminal to start development server,

php artisan serve

Open up the URL – http://127.0.0.1/send-mail

We hope this article helped you to learn about Laravel 8 Send Mail using Gmail SMTP Server in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more