When working on large applications, you may want to organize and separate each mail functions into their classes instead of writing long mail functions inside your controller or on just one long file.

We have created a package that can help you achieve that. https://github.com/stanwarri/mail-dispatcher


Add this to your composer.json

"stanwarri/mail-dispatcher": "dev-master"

  1. Create a Mail Handler

You can then create a folder where all your email sending classes are stored. For use we created a folder called “Mailers” inside the “app” folder

The For each mail function, you can then create it’s class like this. For example, Sending confirmation email

namespace App\Mailers

use Illuminate\Contracts\Mail\Mailer;

class SendConfirmation
protected $user;

public function __construct(User $user){
$this->user = $user;

public function mail(Mailer $mailer){
$confirmation_code = str_random(32);

'user' => $this->user,
'code' => $this->user->confirmation_code
function ($message) {
$message->to($this->user->email)->subject(sprintf('%s %s', config('app.title'), 'Email Verification'));

Note: You can type hint mail() with Objects you want resolved out of the Container (As is Mailer in the example).

  1. Just add the SendsMail trait to any class you want to dispatch a mail like so:
use Stanwarri\MailDispatcher\SendsMail;

  use App\Mailers\SendConfirmation

  class UserAuthController
    use SendsMail;    

    public function register(){        
        $user = User::whereId(1)->first();        
        $this->mail( new SendConfirmation($user));