Email Send in Laravel Using Mailable Class

Email Send in Laravel Using Mailable Class

In this article , We’ll show you how to send email in laravel 5.7 using mailable class with example. We will learn how to send email in laravel using SMTP driver. Laravel provides a API with driver support for SMTP, SendMail,Mailgun, Sendgrid, Mandrill, Amazon SES, SpartPost etc.
And this example also work with laravel 5.8 version.

We will give you very simple example of mailable class to send email in laravel 5.7 application. Benefit of using SMTP server is, you can send email in laravel app from your local server also.

You want to send email in laravel without mailable class, click Send Mail Without Mailable

Contents

  • Install Laravel 5.7 Setup
  • Configuration SMTP in .env
  • Mailable Class
  • Create Controller & Method
  • Create Route & Blade View
  • Start Development Server
  • Conclusion

Install Laravel 5.7

First We need Download fresh Laravel 5.7 setup. Use the below command to download the laravel 5.7 fresh setup on your system.

 composer create-project --prefer-dist laravel/laravel LaravelMail

Configuration SMTP in .env

In this step, we will set SMTP credential in .env file. Let’s open .env file.

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

Note that:- If you are sending a mail using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.

Once less secure apps are enabled; now you can use your Gmail for sending the emails.

Mailable Class

We need to create mailable class for sending emails, use the below command :

php artisan make:mail MailNotify
<?php

namespace App\Mail;

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

class MailNotify 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 Route & Blade View

We need to create route, open your routes/web.php in put the below route :

 Route::get('laravel-send-email', 'EmailController@sendEMail');

Next, we need to create email blade view file in views folder of view. Go to the resources/views/emails and create a view file name email.blade.php. Put bellow code:

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

<h1>This is test mail from Tutsmake.com</h1>
<p>Thank you, {{ $user->name }}</p>

</body>
</html>

Create Controller & Method

We need to create new controller EmailController that will manage one method. lets use this below command and create Controller.

php artisan make:controller EmailController

Now open the controller let’s go to the => app/Http/Controllers/EmailController.php. Put the below Code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Redirect,Response,DB,Config;
use Mail;
class EmailController extends Controller
{
    public function sendEmail()
    {
      $user = auth()->user();
      Mail::to($user)->send(new MailNotify($user));

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

Start Development Server

In this step, we will use the php artisan serve command . It will start your server locally

php artisan serve

If you want to run the project diffrent port so use this below command

php artisan serve --port=8080

Now we are ready to run our example so run bellow command to quick run.

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

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

http://localhost/LaravelMail /public/laravel-send-email

If you want to remove public or public/index.php from URL In laravel, Click Me

Conclusion

In this article , We have successfully install laravel new setup and send email using SMTP detail . our examples run quickly.

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

4 replies to Email Send in Laravel Using Mailable Class

  1. Nice Post Sir…
    Please share how to implement laravel events its usage

  2. Good Tutorial

  3. Goodluck

  4. Thanks, but I don’t want to use gmail smtp.
    I want to use mailable with MAILGUN, how do I configure this?

Leave a Reply

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