How to send mail using gmail smtp

 First go to https://myaccount.google.com/security and click the app password inside the 

Signing in to Google .

and select mail in dropdown and computer window (whatever you are working on device).

Now open .env file
.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=""your maii address "
MAIL_PASSWORD='you generate password'
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="your mail address"
MAIL_FROM_NAME="${APP_NAME}"



Now create a mail class and blade template   using below command
php artisan make:mail MyTestmail --markdown=email.test

After run this command, will be create two file. a file MyTestmail.php inside the Mail folder 
in App.
and other will be create inside the email in views folder with test.blade.php

Now create a constant.php file inside the config folder.
<?php
return [
'email'=>'your mail address'
];


Now open web.php file which is located in route folder. And create a new route in my case my route is sendmaiil.
web.php
<?php

use App\Mail\MyTestMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;
use PHPUnit\Framework\TestResult;

/*
|--------------------------------------------------------------------------
| 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('/', function () {
    return view('welcome');
});

Route::get('sendmail', function () {

    // Mail::to('myj')->send(new WelcomeMail($user));
    if(Mail::to('usermail address')->send(new MyTestMail()))
    {
        return "Mail has been sent !";
    }else{
        return "Try to again";
    }
});



Now open MyTestMail.php which is located in Mail folder in app 
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;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        // return $this->markdown('email.testing');
        return $this->view('email.test')->from(config('constant.email'),"Welcome testing mail")
        ->subject("Testing purpose");
    }
}


And now open test.blade.php file which is located in email folder in views
test.blade.php 
<h3>Hello buddy how are you ! </h3>

Comments