Home Laravel Best Practices Send Mail from Localhost in Laravel

Best Practices Send Mail from Localhost in Laravel

by therichpost
Published: Updated: 4 comments
laravelmail

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.

mailgun_setup

 

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:

<?php

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:

     emailfolder

 

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

        namemailfile

 

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

<div>
    Hi, mail sent..
</div>

 

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:

<?php

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()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home');
    }
  
  /*mail*/
    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
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@mgun.test.com
MAIL_PASSWORD=*********
MAIL_ENCRYPTION=tls

 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.

gotmailmailgun

 

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

Thank you,

Therichpost

You may also like

4 comments

Xur July 23, 2019 - 1:22 pm

InvalidArgumentException
Route [login] not defined.

Reply
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’]);

Reply
MEDP January 5, 2021 - 1:01 pm

its showing as email send but not got any mail

Reply
Ajay Malhotra January 5, 2021 - 4:17 pm

Did you check inside spam folder?

Reply

Leave a Comment

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