PHPackages                             benlehr/mail-campaign-tracker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. benlehr/mail-campaign-tracker

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

benlehr/mail-campaign-tracker
=============================

Logs and tracks all outgoing emails from Laravel

1.12(3y ago)327MITPHPPHP &gt;=7.2.0

Since Jul 1Pushed 3y ago1 watchersCompare

[ Source](https://github.com/BenLehr/mail-tracker-campaigns)[ Packagist](https://packagist.org/packages/benlehr/mail-campaign-tracker)[ Docs](https://github.com/benlehr/MailTracker)[ RSS](/packages/benlehr-mail-campaign-tracker/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (4)Versions (14)Used By (0)

MailTracker with Campaigns
==========================

[](#mailtracker-with-campaigns)

[![Latest Version on Packagist](https://camo.githubusercontent.com/559df9c80d6118c59bdbd9863addd8438afcb9ebbe0bd2a2d864fa59c5eb90d8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62656e6c6568722f6d61696c2d63616d706169676e2d747261636b65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/benlehr/mail-campaign-tracker)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/64e62e9630c93a8608c977e1f6f753c1f7db7cd01f60809e34f2cace9a221cb7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62656e6c6568722f6d61696c2d63616d706169676e2d747261636b65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/benlehr/mail-campaign-tracker)

This package is a extension of [J David Baker's MailTracker Package](https://github.com/jdavidbaker).

With Mail Campaign Tracker you can create Trackable Emails and Campaigns.

MailTracker will hook into all outgoing trackable 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.

Install
-------

[](#install)

Via Composer

```
$ composer require benlehr/mail-campaign-tracker
```

Publish the config file and migration

```
$ php artisan vendor:publish --provider="benlehr\MailTracker\MailTrackerServiceProvider"
```

Run the migration

```
$ php artisan migrate
```

Note: If you would like to use a different connection to store your models, you should update the mail-tracker.php config entry `connection` before running the migrations.

If you would like to use your own migrations, you can skip this library migrations by calling `MailTracker::ignoreMigrations()`. For example:

```
// In AppServiceProvider

public function boot()
{
    MailTracker::ignoreMigrations();
}
```

Usage
-----

[](#usage)

Once installed, you can create Trackable Mails with a command, this mails will be logged to the database.

The following config options are available in config/mail-tracker.php:

- **name**: set your App Name.
- **inject-pixel**: set to true to inject a tracking pixel into all outgoing html emails.
- **track-links**: set to true to rewrite all anchor href links to include a tracking link. The link will take the user back to your website which will then redirect them to the final destination after logging the click.
- **expire-days**: How long in days that an email should be retained in your database. If you are sending a lot of mail, you probably want it to eventually expire. Set it to zero to never purge old emails from the database.
- **route**: The route information for the tracking URLs. Set the prefix and middlware as desired.
- **admin-route**: The route information for the admin. Set the prefix and middleware.
- **admin-template**: The params for the Admin Panel and Views. You can integrate your existing Admin Panel with the MailTracker admin panel.
- **date-format**: You can define the format to show dates in the Admin Panel.
- **content-max-size**: You can overwrite default maximum length limit for `content` database field. Do not forget update it's type from `text` if you need to make it longer.

If you wish to have an email tracked in a campaign, then you have to create a new Trackable Mail with the command:

```
php artisan make:trackable-mail 'MailName'

```

When you send the email you have to create the campaign with the campaign helper which returns you the campaign id. The ID has to be injected into the email as parameter.

```
use benlehr\mail-tracker\MailCampaignHelper;

// get the helper
$helper = new MailCampaignHelper();
// create a campaign and get ID
$campaignId = $helper->createCampaign('name of campaign');

// fetch your users
$users = User::all();

foreach ($users as $user) {
    // send mail and inject campaign id
  Mail::to($user->email)->send(new TestMail($campaignId));
}
```

Note on dev testing
-------------------

[](#note-on-dev-testing)

Several people have reported the tracking pixel not working while they were testing. What is happening with the tracking pixel is that the email client is connecting to your website to log the view. In order for this to happen, images have to be visible in the client, and the client has to be able to connect to your server.

When you are in a dev environment (i.e. using the `.test` domain with Valet, or another domain known only to your computer) you must have an email client on your computer. Further complicating this is the fact that Gmail and some other web-based email clients don't connect to the images directly, but instead connect via proxy. That proxy won't have a connection to your `.test` domain and therefore will not properly track emails. I always recommend using [mailtrap.io](https://mailtrap.io) for any development environment when you are sending emails. Not only does this solve the issue (mailtrap.io does not use a proxy service to forward images in the emails) but it also protects you from accidentally sending real emails from your test environment.

Events
------

[](#events)

When an email is sent, viewed, or a link is clicked, its tracking information is counted in the database using the benlehr\\MailTracker\\Model\\SentEmail model. This processing is done via dispatched jobs to the queue in order to prevent the database from being overwhelmed in an email blast situation. You may choose the queue that these events are dispatched via the `mail-tracker.tracker-queue` config setting, or leave it `null` to use the default queue. By using a non-default queue, you can prioritize application-critical tasks above these tracking tasks.

You may want to do additional processing on these events, so an event is fired in these cases:

- benlehr\\MailTracker\\Events\\EmailSentEvent
    - Public attribute `sent_email` contains the `SentEmail` model
- benlehr\\MailTracker\\Events\\ViewEmailEvent
    - Public attribute `sent_email` contains the `SentEmail` model
    - Public attribute `ip_address` contains the IP address that was used to trigger the event
- benlehr\\MailTracker\\Events\\LinkClickedEvent
    - Public attribute `sent_email` contains the `SentEmail` model
    - Public attribute `ip_address` contains the IP address that was used to trigger the event

To install an event listener, you will want to create a file like the following:

```
