Laravel Data-Driven Strategies #5: email tracking, did they read your emails?

Kalizi <Andrea>
Dev Genius
Published in
4 min readMay 25, 2021

--

“Email has an ability many channels don’t: creating valuable, personal touches — at scale” ~ David Newman

Emails are an amazing way to get in touch with your audience, and many times are the key for a business to get in touch with customers. Emails are customized, are coloured, are powerful, are professionals, even emotional sometimes. But… did you ever think if are read?

Many people out there rely on managed email services that give you all stats about your emails, but many times, these are paid services and to build a small service, your budget may not fit your needings.

So, in this article, we’ll build an email tracker for your laravel project!

Installation

I tried many packages and the one I‘m good with is jdavidbakr/mail-tracker.

“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.” ~ MailTracker

To start, just:

composer require jdavidbakr/mail-tracker

And Laravel will discover the package:

Discovered Package: jdavidbakr/mail-tracker

Now publish everything related to the package

> php artisan vendor:publish --provider="jdavidbakr\MailTracker\MailTrackerServiceProvider"Copied File [/vendor/jdavidbakr/mail-tracker/config/mail-tracker.php] To [/config/mail-tracker.php]
Copied Directory [/vendor/jdavidbakr/mail-tracker/src/views] To [/resources/views/vendor/emailTrakingViews]
Publishing complete.

This will publish the configuration file and the default views (we’ll get later on these).

The package needs to store stuff in the database, so we should execute its migrations

> php artisan migrate
Migrating: 2016_03_01_193027_create_sent_emails_table
Migrated: 2016_03_01_193027_create_sent_emails_table (22.75ms)
Migrating: 2016_09_07_193027_create_sent_emails_Url_Clicked_table
Migrated: 2016_09_07_193027_create_sent_emails_Url_Clicked_table (40.97ms)
Migrating: 2016_11_10_213551_add-message-id-to-sent-emails-table
Migrated: 2016_11_10_213551_add-message-id-to-sent-emails-table (3.18ms)
Migrating: 2020_04_15_112152_add_message_id_index_to_sent_emails_table
Migrated: 2020_04_15_112152_add_message_id_index_to_sent_emails_table (6.74ms)
Migrating: 2021_02_10_083945_add_sender_email_and_name_to_sent_emails_table
Migrated: 2021_02_10_083945_add_sender_email_and_name_to_sent_emails_table (3.95ms)
Migrating: 2021_02_10_095634_add_first_open_and_first_click_columns
Migrated: 2021_02_10_095634_add_first_open_and_first_click_columns (4.72ms)

And to prove that’s terribly easy… it’s ready to go!

The package will register the following routes

GET|HEAD  email-manager ................. mailTracker_Index
GET|HEAD email-manager/clear-search .... mailTracker_ClearSearch
POST email-manager/search .......... mailTracker_Search
GET|HEAD email-manager/show-email/{id} . mailTracker_ShowEmail
GET|HEAD email-manager/smtp-detail/{id}. mailTracker_SmtpDetail
GET|HEAD email-manager/url-detail/{id} . mailTracker_UrlDetail
GET|HEAD email/l/{url}/{hash} .......... mailTracker_l
GET|HEAD email/n ....................... mailTracker_n
POST email/sns ..................... mailTracker_SNS
GET|HEAD email/t/{hash} ................ mailTracker_t

To access the manager you should register a gate named see-sent-emails. For testing purposes, I just allowed everybody in AuthServiceProvider :

Gate::define('see-sent-emails', function ($user) {
return true;
});

You would obviously put there some custom rule like return $user->is_admin; or something like that.

And then visiting /email-manager you’ll get it

Mail tracker

Hello World

The easiest thing to do is to set up a view just saying “Hello world” somewhere.

Mail::send('mails.test', [], function ($mail) {
$mail->to('thisisa@test.com')->subject('Does this work?');
});

I’m currently using Mailhog so I saw the email coming

Mailhog

And refreshing the tracker, you’ll see the email with 0 opens and 0 clicks

Mail tracker

Now, going back to mailhog, opening the email, and refreshing the tracker will get you to this

They’ve seen the email!

The user saw the email, you see when he saw the email for the first time and if something was clickable, like a call to action, you’ll see when the user got to that Call to action.

But… how?

Let’s take a look at the source of the email

Hello World <img border="0" width="1" alt="" height="1" src="http://localhost:8080/email/t/45v3asPZjz7NzYPxY1NYNNBnoLvGH2gX">

The blade just had a “Hello World”, the img tag did the magic because images are practically always loaded on emails (if they’re not considered spam and blocked in advance).

And if you get to that URL you’ll see a 1x1px image:

The magic image

Would you notice a dead pixel in your emails?

So… did they read your emails?

Emails are really powerful. When I get a nice email, built for me, with the content I like to receive, I spend minutes on it because I think it’s worth it, even if an algorithm hides behind it, so if you’re building your emails manually, programmatically or in any other ways, be sure that they’re worth it!

Now it’s up to you to build something great! 🥂

Stay tuned for other Data-Driven Strategies and if you want, take a moment ️️to leave a comment about how you take data-driven decisions supported by tools! ☕️

--

--

IT Engineering Bachelor and still student 🎓 Mobile&Backend Dev 💻 Growth hacking Enthusiast ☕ Startupper 🚀 Metalhead 🤘🏻 With love, from Palermo ❤️