How to track your emails sent from the laravel application

How to track your emails sent from the laravel application?

Hi Guys,
I got one question while building the laravel application. “How to track your emails sent from the laravel application?”. I have searched on Google for this question. I have one solution. In this article, we are going to study the Mailtracker package to track users’ activity on emails. It is easy to use and maintain all the records.

We have installed the Mailtracker package to track all activity of users like Open, Click, send, Etc. MailTracker will hook into all outgoing emails from Laravel and inject a tracking code into it. It will also store the rendered email in the database. There is also an interface to view sent emails.

This package contains many features which I want to implement in this project. 

Check the link: https://github.com/jdavidbakr/mail-tracker.

A step-by-step guide to track your emails sent from laravel

Install the Mailtraker Package

Please check the version of Mailtracker which is compatible with your project. I have installed the Mailtracker package of version 5.0. Run the below command.

composer require jdavidbakr/mail-tracker ^5

Publish the vendor of the package

You need to register and publish the vendor. For that, execute the below command. When you run the php artisan vendor:publish command, simple views will add to your resources/views/vendor/emailTrakingViews that you can customize.

php artisan vendor: publish --provider="jdavidbakr\MailTracker\MailTrackerServiceProvider"

Migrate the table for Mailtracker

The above command makes the view. You generate a table to store email statistics. 

php artisan migrate

Extra functionality after each Event

Now, you can make a listener class for each event if you want to do different operations. Run the below command to create a new listener class.

php artisan make:job EmailSent    

php artisan make:job EmailViewed

php artisan make:job EmailClicked

Register custom events in an application

Register these listener classes inside EventServiceProvider Providers. Check the code snippet below.

'jdavidbakr\MailTracker\Events\EmailSentEvent' => [
                        'App\Listeners\EmailSent',
],

'jdavidbakr\MailTracker\Events\ViewEmailEvent' => [
            'App\Listeners\EmailViewed',
],

'jdavidbakr\MailTracker\Events\LinkClickedEvent' => [
            'App\Listeners\EmailLinkClicked',
],

Restrict the email statistics role-wise

The default configuration with the package puts it behind the can:see-sent-emails middleware; you may create a gate for this rule or change it to use one of your own. For example, we can make the Gate in the AuthServiceProvider file. See below code snippet.

public function boot()
{
        $this->registerPolicies();
         Gate::define('see-sent-emails', function ($user) {
                return auth()->user()->roles[0]->name == "admin";
         });
}

You may also change the default prefix and disable the admin routes. We can customize all views of the mail tracker in the resources/views/vendor/emailTrakingViews directory.

Thank you for reading this article. Please share this article with your friend circle. That’s it for the day. Stay Connected!
Cheers

Loading

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top