Surfe.be - passive income

How to send an email using mailable in laravel

How to send an email

Today, i am going to tell you how to send simple email with gmail smtp configuration using laravel mailable class. We can send email using many procedure in laravel . But today we are going to learn how to send an email via mailable class in laravel . It is very simple and easy. you have to just follow few step to do it . So let’s start our tutorial. 

how to send email using mailble class in laravel

Mail Configuration

Go to your .env file and open it. Setup configuration like this . 

MAIL_DRIVER = smtp
MAIL_HOST = smtp.gmail.com
MAIL_PORT = 587
MAIL_USERNAME = your gmail
MAIL_PASSWORD = your gmail password
MAIL_ENCRYPTION = tls

Create Mailable Class

Now we have to create our mailable class . So go to your project root directory and run this command.

php artisan make:mail SendEmailUsingMailAbleClass

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

Mail/SendEmailUsingMailAbleClass.php

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

class SendEmailUsingMailAbleClass extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct()
    {
     
    }

    public function build()
    {
        return $this->view('emails.SendEmailUsingMailAbleClass'); // This is blade(SendEmailUsingMailAbleClass) file which you wanna send to your user . 
    }
}

Create Require Route

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

Route::get('mail','HomeController@SendEmailUsingMailAbleClass');

Now, we will add SendEmailUsingMailAbleClass() 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. If you face any problem to send email via smtp server then see this tutorial smtp server doesn’t work to send email in laravel

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Mail;
use App\Mail\SendEmailUsingMailAbleClass;

class HomeController extends Controller
{
 
    public function SendEmailUsingMailAbleClass()
    {
        $Email = 'mahedy150101@gmail.com';
        Mail::to($Email)->send(new SendEmailUsingMailAbleClass());
        dd("Mail Send Successfully");
    }
}

That’s it! hope it will work for you. if works then inform us in comment box and if you have any trouble then also share with me your error  in comment box

Leave a comments

LET'S SOCIALITE

Recent Tweets

RECOMMENDED POST