Categories

Tuesday, December 24, 2024
#919814419350 therichposts@gmail.com
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 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

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

4 Comments

Leave a Reply

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