Laravel 6 – Send Email Using Google SMTP Example

send-mail-smtp-gmail-laravel-6

Laravel provides a clean, simple API over the popular SwiftMailer library with drivers for SMTP, Mailgun, Postmark, SparkPost, Amazon SES, and sendmail, allowing you to quickly get started sending mail through a local or cloud based service of your choice.

In this tutorial, I will show you how to send email using smtp gmail in laravel 6. Here i will use smtp gmail credentials to send email in laravel 6.

Enable Less Secure Apps In Google

You can’t send mail without enable less secure apps in google with third party apps. So lets enable it.

You have successfully enable less secure apps in google.

NOTE :: Make sure if you dont require then disable it becuase it will harm your account with spammy third party apps.

Configure Mail Credentials

Next, I hope that you have already installed laravel 6 in your system. If you haven’t installed it then do it first.

Now, open .env file and setup gmail credentials

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]     
MAIL_PASSWORD=xxxxxxxxxx
MAIL_ENCRYPTION=tls

Generate Mailable Class

Now, lets create a laravel mailable class using below command.

php artisan make:mail TestMail

Above command will generate TestMail.php mailable class in app/Mail directory. All of a mailable class’ configuration is done in the build method. Within this method, you may call various methods such as fromsubjectview, and attach to configure the email’s presentation and delivery.

Related:  Form Validation In Laravel 6 With Example - Part 1

Lets add some dummy data in TestMail mailable class.

app/Mail/TestMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this
                ->from('[email protected]') // Sender mail
                ->subject('Test Subject') // Mail subject
                ->view('mail.index') // View file resource/views/mail/index
                ; 
    }
}

In the build method, we have add from, subject, view methods. We can add attachment using attach method. Here view shows resource/views/mail/index directory file.

resource/views/mail/index.blade.php

<!DOCTYPE html>
<html>
<head>
	<title>Test Mail File</title>
</head>
<body>
	<div id="text">
		<h1>Congratulations, You have successfully sent mail!</h1>
		<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>
	</div>
</body>
</html>

Generate Controller File

Next, lets create a controller using below artisan command.

php artisan make:controller MailController

Above command will generate MailController.php under app/Http/Controllers directory shown as below.

MailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MailController extends Controller
{
    //
}

Next we will write a method to send email from laravel. lets add sendMail() method in controller.

<?php

namespace App\Http\Controllers;

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

class MailController extends Controller
{
    /**
     * Send mail function
     *
     */
    public function sendMail()
    {
    	Mail::to('[email protected]')
    		->cc('[email protected]')
    		->bcc('[email protected]')
    		->send(new TestMail());
	    
	    if (Mail::failures()) {
	        // return with failed message
	    }

	    // return with success message
    }
}

In sendMail method, we use Mail class with to, cc, bcc and send methods to send mail using TestMail mailable. Its quite simple.

Related:  Generate Fake Data Using Laravel Faker

Add Route In web.php

Next, lets add route for sendMail method in web.php route file.

web.php

<?php

/*
|--------------------------------------------------------------------------
| 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('send-mail', '[email protected]');

Here we have used get method.

Start app server using php artisan serve command and navigate http://127.0.0.1:8000/send-mail url. You will get mail in your inbox successfully.

Congratulations you have successfully sent mail using smtp gmail in laravel 6.

I hope that you like this tutorial. Let me know if you have any question regarding this tutorial. Please share this tutorial with your friends.

About Chintan Panchal

I am web developer having 4+ year of experience of web development. I have worked on codeigniter, symfony, laravel and wordpress.

View all posts by Chintan Panchal →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.