Laravel 7/6 Send Email Using Mailable Class Tutorial

Laravel 7/6 Send Email Using Mailable Class Tutorial

Laravel 7 send email example using mailable. Here, We’ll show you how to send email in laravel using a mailable class with example. We will learn how to send email in laravel using SMTP driver. Laravel provides an API with driver support for SMTP, SendMail, Mailgun, Sendgrid, Mandrill, Amazon SES, SpartPost, etc.
This example tutorial also work with laravel 7.x, 6.x, 5.x version.

We will give you a very simple example of mailable class to send email in laravel application. The 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

Laravel Send Email Using Mailable Class Example

  • Install laravel Fresh Setup
  • Configuration SMTP in .env
  • Mailable Class
  • Create Route & Blade View
  • Create Controller & Method
  • Run Development Server
  • Conclusion

1. Install laravel Fresh Setup

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

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

2. Configuration SMTP in .env

In this step, we will set SMTP credentials in the .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

3. Mailable Class

We need to create a 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
    }
}

4. Create Route & Blade View

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

 Route::get('laravel-send-email', '[email protected]');

Next, we need to create an 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> 

5. Create Controller & Method

We need to create a new controller EmailController that will manage one method. lets use this below command and create a 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');
         }
    }
}

6. Run 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/blog/public/laravel-send-email

Conclusion

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

You may like

  1. Laravel Send Email without mailable class Example
  2. Laravel Tutorial From Scratch | Step By Step

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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