Saturday, July 20, 2024
LaravelLaravl 5.7

Best Practices Send Mail from Localhost in Laravel

Best Practices Send Mail from Localhost in Laravel - The Rich Post

Hello to all, welcome to 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(’’)“, 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('');


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('')->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,


the authortherichpost
Hello to all. Welcome to Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.


Leave a Reply

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