Today now in this post i will show you How to send mail by using mailable in laravel ? We are know that at first time in laravel 5.3 is introduce the mailable class for mail sending as like Laravel 5.3 event and mailable class provide several method as like view(), from(), text() etc. Now this Mail send example is a very pretty interesting then laravel other version and also the very simple with fully customize.
Now here , i am going to tell you about how to send the simple email with gmail smtp configuration by using laravel mailable class. We know It is a very simple and also best way. We need to just follow the few step and we will get the simple mail send example in our laravel application.
Step 1: Mail Configuration
At first step we need to add our gmail smtp configuration as like our username, password etc, so need to open our .env file and then need to add our configuration.
.env
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=codingspoint@gmail.com MAIL_PASSWORD=codingspoint MAIL_ENCRYPTION=tls
Step 2: Create Mailable Class
Laravel is introduce the mailable class on that way we can use this simply as like laravel event, So we can re-use this anywhere in our laravel application. So at first nee to create the Mailable class by using artisan command, so at fire the bellow command:
php artisan make:mail MyTestMail
Ok, Now we can see the new file in our app(app/Mail/MyTestMail.php) folder. So, need to open that file and put the 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
Now In this step, we need to add a new route for out testing mail so need to open our route file and add bellow route.
routes.php
Route::get('my-test-mail','HomeController@myTestMail');
Step 4: Add Controller Method
So Now, we will add the myTestMail() function in “HomeController” Controller file, now in this file we will write the code of mail send, so if we haven’t HomeController then need create HomeController.php file and put the 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
Finally in the last step, we need to create the email template file, so at first create the “myemails” folder in our resources folder and then create MyTestMail.blade.php file and then put bellow code.
resources/views/emails/myTestMail.blade.php
Hi,
This is My Test Mail.
Thank you.
Read Also: How to implement infinite ajax scroll pagination in Laravel?
Thanks for read. I hope it help you. For more you can follow us on facebook