In a simple application that requires sending only a few emails, there would be no problem creating Mail class for each of the emails. But in a large app or one that requires sending multiple emails, it becomes tedious creating Mail class and probably Job class too for each of the emails.
Imagine sending about 20 different emails in an application. This would require creating 20 Mail classes and 20 Job classes. That’s a lot of work!
This article tries to address this problem by creating a single global function that uses only one Mail class and one Job class.
- Knowledge of Laravel
- A Laravel project
- Mail service provider in your project. You can use Gmail SMTP or Mailtrap.io
Before we dive into the main business, it is important we understand that this article is just a fragment of a project and therefore does not aim to teach Laravel in-depth. If you have met the requirements above then you’re ready to do something awesome! We’ll create a Mail class, a Job class and finally a helper function
Create the Mail class
Open your terminal cd to your laravel project and run the following commands.
$ php artisan make:mail AppMailer --markdown=emails.app-mailer
With the command above, we’ve created a Mail class called AppMailer. We also created a markdown email template we’d use to test our email sending.
Next up, edit the AppMailer class as follows:
Looking at the snippet above, the AppMailer expects an
Array $params which should contain the necessary info we need to send a mail.
$data variable will be used to pass data to the email template, This means that in the email template we will be accessing the data passed to it using the
$template variable contains the path to the email template we want to use for a given mail.
$template_type specifies whether the email template is a markdown or a custom view template.
$subject holds the email subject if any. The
$from_email holds the name and email of the sender respectively.
build() method, we checked for the template type to enable us to use the correct method for rendering the template.
Create the Job class
It is not compulsory to use a Job for dispatching the email, but we are assuming you want to dispatch the email using queue worker for optimum performance.
$ php artisan make:job AppMailerJob
Open the Job class and edit as follows:
Notice that the $params array has a
to key which holds the recipient email address.
sendmail() helper function
Create a folder named Helper in the app directory or anywhere of your choice and create a new file named
helper.php. That is, it should be in
Open the helper.php and edit as follows:
Next up, we will add the helper.php to the autoloader of
composer.json. Open the
composer.json file and edit the part with “autoload” as follows:
The change we just made in the composer.json makes it possible for every function created in the
helper.php to be accessible anywhere in the application.
If you ever need to create a function that’s globally accessible in your Laravel app, this is how you should do it.
Next up, we need to reload the composer autoload, run
$ composer dump-autoload
At this point, our global
sendmail function is ready for use.
To use the sendmail function anywhere in your app, follow the steps below:
Below shows how you can access the data passed to the template:
You can clone the repo for the example used on GitHub. I implemented a quick test which can be found in the route
In this article, we have demonstrated how we can create a single global function for sending all emails in our application. We also showed how we can create a global helper function in a Laravel application.
Feel free to share a better way you’ve got on how to achieve what we’ve done in this article. You can reach me here or on Twitter. Cheers!