DEV Community

Cover image for Create a re-useable Mailing Code in Laravel
Muhamad Sulaiman
Muhamad Sulaiman

Posted on

Create a re-useable Mailing Code in Laravel

Hi there! In today's posting, I would like to share with you guys how to create a Laravel Mailing reusable code.

What I mean here is not the php artisan make:mail. But the Mail::to Syntax.

I'm building one mechanism in my application to send the reminder email to all of our targeted users. There are two ways to send to them. One is done manually (by clicking a button and then sending the email), second is done automatically (by using Laravel Scheduler)

The problem I faced was a lot of Mail::to syntax logic that I needed to code. The full code is like this,

Mail::to($user->email)
->send(new SendFirstReminder($user->name, $user->email));
Enter fullscreen mode Exit fullscreen mode

It took me nearly 20 seconds to code it (I'm a slow typer)!

And there are a few places where I need to put the code. Such as Controller, and Console.

That's when I started to think. what if I need to use that mail code again?

So I need to spend another 20 seconds again? Of course, I don't want to do that again.

So what happened is, that I created a custom Services Class. I named it as EmailReminder. In this class where I gather all the reminder emails. Everything is in one place.

Then I use the static method to make it more readable.

As you can see in this image, after I created the EmailReminder services, I can call it wherever I want without spending 20 seconds to code it. It can be reused in all places.

I just need to call it statically and then it will work like usual. But it is shortened and more readable (at least from my point of view)

sulaiman-mail-code

Later I will create another post on how I created a reusable Local Scope logic for my application.

If you have other suggestions, comment below. I'd love to hear about your code strategy.

Top comments (3)

Collapse
 
xwero profile image
david duymelinck • Edited

If the problem that you want to solve is to save time, just copy paste your initial code.

So instead of Mail::to($user->email)->send(new SendFirstReminder($user->name, $user->email)); you have to type EmailReminder::sendFirstReminder($user->email,$user->name , $user->email);
It doesn't look like a lot less typing.

Even if you removed the second mail parameter. Your examples don't show a use where the mail addresses are different, and you wrote you have to type the same code in different parts of your code.

Be comfortable with copy pasting code. Creating layers that add nothing or almost nothing, you are slowing your code down and can create bugs.

Collapse
 
msulaimanmisri profile image
Muhamad Sulaiman

Hi David. Thank you for giving your opinion.

I think you misunderstood between copy & pasting with code reusability. What I tried to implement here is code reusability.

Even if there's a bug, I strongly believe that you can focus on fixing it in one place only. Rather than do some shotgun debugging.

Take the example Laravel Validation Form Request approach. Why do we separate it into their own folder and then call the class? Can't we just put it into the controller?

Of course, you can. But if you take away the code and make it can be reused again and again, that can save you a lot of time. The same goes for what I'm doing here.

You can agree or disagree with my method though. But my VSCode is not giving me a hint when I code Mail::to. But if I type EmailReminder, VSCode will automatically give me the full code together with the method inside it. Which saves me a lot of time.

At the end of the day, there's always a debate between "Should I code faster or Should I code cleaner and readable".

Collapse
 
xwero profile image
david duymelinck • Edited

Maybe I focused too much on the part where you wrote about losing time.

I wanted to convey that if you only use the code a couple times and it is a oneliner, there is no need to create an extra function. If something is wrong with it, fix it and find and replace the old code with the new in the rest of the project.

If you need it multiple times and it is more code, a better way to add extra functionality is to use events.
The event code could look like this;

NewUserAdded::dispatch($user);
Enter fullscreen mode Exit fullscreen mode

This allows you to add as many listeners you need as an added bonus.

I really hate classes that are made only to work better with intellisense of an IDE. For me it is code waste.

The Form request is a totally different beast than your example. To quote the documentation; For more complex validation scenarios, you may wish to create a "form request".
So it follows the idea I have, abstract code if it has too many lines.

For me it is not a debate about faster or cleaner, it is about finding the best compromise for the code to be fast and clean.