Home Laravel Best Practices Send Mail from Localhost in Laravel

Best Practices Send Mail from Localhost in Laravel

by therichpost
Published: Last Updated on 4 comments

Hello to all, welcome to therichpost.com. In this post, I will tell you, Best Practices Send Mail from Localhost in Laravel.

I am doing this in laravel 5.7.

In this post, I am sending the email in laravel through localhost and I am using Mailgun for this.

For Mailgun setup, first you need to create account on Mailgun and after create account, you need to add below information(Showing in first screenshot) into your laravel application, that I will tell in below laravel mail setup steps.



Here are the working steps you need to follow:


1. Very first, you need to run below command into your terminal:

$ php artisan make:mail PaymentDone


2. After ran above command, you will get below PaymentDone.php file into your app\Mail folder:

In this file, we can set the email template, like I have set “return $this->view(’email.name’)“, In this, ’email’ stands for folder name and ‘name’ stands for mail template:


namespace App\Mail;

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

class PaymentDone 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('email.name');


3. Now we need to do two things:

   a) First, we need to create new folder ’email’ in resources\views\ folder:



   b) Second, we need to create new view file  name.blade.php in email folder, that we have create above:



4. Add below code in resources\views\email\name.blade.php file:

    Hi, mail sent..


5. Now we need to define route in routes/web.php file:

Route::get("send/email", "HomeController@mail");


6. Now we need to add below code(highlighted) into app\Http\Controllers\HomeController.php file:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mail\PaymentDone;
use Illuminate\Support\Facades\Mail;
class HomeController extends Controller
     * Create a new controller instance.
     * @return void
    public function __construct()

     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function index()
        return view('home');
    public function mail()
        Mail::to('therichposts@gmail.com')->send(new PaymentDone());
        return 'Email was sent';



7. Finally but not the last, we need to set mailgun credentials into our .env file:

//You will get from mailgun

 8. Run php artisan serve command into you terminal and run this url: http://localhost:8000/send/email,  If everything done properly by your side then you get email like I got.



 If you have any query related to this post, then do comment below or ask questions.

Thank you,


You may also like


Xur July 23, 2019 - 1:22 pm

Route [login] not defined.

Ajay Malhotra July 24, 2019 - 5:23 pm

You’re trying to redirect to a named route whose name is login, but you have no routes with that name:

Route::post(‘login’, [ ‘as’ => ‘login’, ‘uses’ => ‘LoginController@do’]);

MEDP January 5, 2021 - 1:01 pm

its showing as email send but not got any mail

Ajay Malhotra January 5, 2021 - 4:17 pm

Did you check inside spam folder?


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.