How To Send Email In Laravel 8 With Mailable And Mailtrap Example

Laravel

211

Sumit Suryavanshi

19-12-2020


Laravel send email with mailable and mailtrap example. In this tutorial i will show you how to send mail in laravel with the mailable class. And also you will learn how to create mailable notification class in the laravel web applications

Laravel 8 Send Email/Mail With Mailable

  • Setup SMTP Credentials in .env
  • Make Mailable Class
  • Create Controller with Method
  • Create Route & Blade View
  • Email Send and Test

Setup SMTP Credentials in .env

This is the first step, go to .env file and set SMTP details in .env file.

You could do like:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com 
MAIL_PORT=587 
MAIL_USERNAME=Add your user name here
MAIL_PASSWORD=Add your password here
MAIL_ENCRYPTION=tls 

The above email SMTP details are default and necessary. You can also set the following things:

MAIL_FROM_ADDRESS=
MAIL_FROM_NAME=

The above are used by Laravel web applications when you will send emails/mails to users. The from email address and name part of the email that is displayed to users.

Make Mailable Class

You can use the php artisan make:mail command to create a new mailable class in your laravel web application.

You could do like:

php artisan make:mail MailNotification

After that go to app/Mail and update the following code into your MailNotification class:

<?php
 
namespace App\Mail;
 
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
 
class MailNotification 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('view.name');
        //here is your blade view name
    }
}

Create Controller with Method

This is the second step. in this step, we will create a controller for sending emails/mails.

Just open your command prompt (CMD) and go to your project root directory. Then type the below command for creating a new controller.

php artisan make:controller MailController

After that, You need to create one method in your controller. So go to the => app/Http/Controllers/MailController.php And update the below given code into your controller:

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Redirect,Response,DB,Config;
use Mail;

class MailController extends Controller
{
    public function sendEmail()
    {
        $data['title'] = "This is first mail from w3alert1.com";
        
        $data['name']= 'w3alert1.com';

        Mail::to($user)->send(new MailNotify($data));
 
        if (Mail::failures()) {
           return response()->Fail('Sorry! Please try again latter');
         }else{
           return response()->success('Great! Successfully send in your mail');
         }
    }
}

Create Route & Blade View

Next step, here you need to create one route and one blade view file.

So go to routes/web.php and update the following route in your file:

use App\Http\Controllers\MailController;

Route::get('lara-email-send', [MailController::class, 'sendEmail']);

After that, go to your resources/views/ and create a new blade view file name email.blade.php and update the below code into your file:

<!DOCTYPE html>
<html>
<head>
 <title>Laravel Send Email Example</title>
</head>
<body>

 <h1>This is first mail using laravel mailable class </h1>
 <p>Thank you, {{ $name }}</p>

</body>
</html>

Email Send And Test

Hit the following URL in your browser and test:

http://localhost:8000/lara-email-send

If you are not run PHP artisan server command, direct go to your browser and type the URL

http://localhost/your-project-name/public/lara-email-send