How to send mail using mailable in laravel 5.3?

By Hardik Savani | August 31, 2016 | | 25217 Viewer | Category : PHP Laravel


Share this post:



Laravel 5.3 released few days ago, that update several part from laravel 5.2, Laravel 5.3 also change mail sending function. Laravel 5.3 introduce mailable class for mail sending like Laravel 5.3 event and mailable class provide several method like view(), from(), text() etc. Mail send example is pretty interesting then laravel other version and also very simple with fully customize.

Today, i am going to tell you how to send simple email with gmail smtp configuration using laravel 5.3 mailable class. It is very simple and best way. you have to just follow few step and you will get simple mail send example in your laravel 5.3 application.

Follow bellow step in your laravel 5.3 project.

Step 1: Mail Configuration

In first step you have to add your gmail smtp configuration like your username, password etc, so open your .env file and add your configration.

.env

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

MAIL_USERNAME=itsolutionstuff@gmail.com

MAIL_PASSWORD=mypassword

MAIL_ENCRYPTION=tls

If you don't know how to do configuration or found any error about gmail authentication then you can follow this link : How to set gmail configration for mail in Laravel?.

Step 2: Create Mailable Class

Laravel 5.3 introduce new mailable class that way we can use simply like laravel 5.2 event, you can re-use anywhere in your laravel application. So first create Mailable class using artisan command, so fire bellow command:

php artisan make:mail MyTestMail

Ok, Now you can see new file in your app(app/Mail/MyTestMail.php) folder. So, open that file and put bellow code.

app/Mail/MyTestMail.php

namespace App\Mail;


use Illuminate\Bus\Queueable;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;

use Illuminate\Contracts\Queue\ShouldQueue;


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->view('emails.myTestMail');

}

}

Step 3: Add Route

In this step, we will add new route for out testing mail so open your web route file and add bellow route.

routes/web.php

Route::get('my-test-mail','HomeController@myTestMail');

Step 4: Add Controller Method

Now, we will add myTestMail() in "HomeController" Controller file, in this file we will write code of mail send, so if you haven't created HomeController then create HomeController.php file and put bellow code.

app/Http/Controllers/HomeController.php

namespace App\Http\Controllers;


use Illuminate\Http\Request;

use App\Http\Requests;

use Mail;

use App\Mail\MyTestMail;


class HomeController extends Controller

{


/**

* Send My Test Mail Example

*

* @return void

*/

public function myTestMail()

{

$myEmail = 'aatmaninfotech@gmail.com';

Mail::to($myEmail)->send(new MyTestMail());


dd("Mail Send Successfully");

}


}

Step 5: Add View File

In last step, we will create email template file, so first create "emails" folder in your resources folder and create myTestMail.blade.php file and put bellow code.

resources/views/emails/myTestMail.blade.php

Hi,


This is My Test Mail.


Thank you...

Ok, now you ready to run our test example, so check it...



We are Recommending you: