PHPackages                             henryavila/email-tracking - 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. [Logging &amp; Monitoring](/categories/logging)
4. /
5. henryavila/email-tracking

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

henryavila/email-tracking
=========================

Track e-mail delivery with Mailgun Hooks. All data are stored in the database on Email model

v7.0.0(5mo ago)73.4k↓50%[3 PRs](https://github.com/henryavila/email-tracking/pulls)1MITPHPPHP ^8.2CI passing

Since May 4Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/henryavila/email-tracking)[ Packagist](https://packagist.org/packages/henryavila/email-tracking)[ Docs](https://github.com/henryavila/email-tracking)[ RSS](/packages/henryavila-email-tracking/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (13)Versions (52)Used By (1)

Laravel Email Tracking with Mailgun
===================================

[](#laravel-email-tracking-with-mailgun)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5f5d7213c0e0041e89d62dfce9db7424e7721cb01ebdb8273317328210c6b530/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68656e72796176696c612f656d61696c2d747261636b696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/henryavila/email-tracking)[![Total Downloads](https://camo.githubusercontent.com/e98cf53f1e4a603fc168c0f7732db1460dab19ced37b0c4e3de9a9ed393f2b69/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68656e72796176696c612f656d61696c2d747261636b696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/henryavila/email-tracking)[![Tests](https://github.com/henryavila/email-tracking/actions/workflows/run-tests.yml/badge.svg)](https://github.com/henryavila/email-tracking/actions/workflows/run-tests.yml)[![PHPStan](https://github.com/henryavila/email-tracking/actions/workflows/phpstan.yml/badge.svg)](https://github.com/henryavila/email-tracking/actions/workflows/phpstan.yml)[![Laravel Pint](https://github.com/henryavila/email-tracking/actions/workflows/laravel-pint.yml/badge.svg)](https://github.com/henryavila/email-tracking/actions/workflows/laravel-pint.yml)[![codecov](https://camo.githubusercontent.com/b20230e1d13ce6c9619d14a71f8e63e0a4a75fb0a16014b2dc4f5a51d52f2c40/68747470733a2f2f636f6465636f762e696f2f67682f68656e72796176696c612f656d61696c2d747261636b696e672f67726170682f62616467652e7376673f746f6b656e3d31544d4f523330303143)](https://codecov.io/gh/henryavila/email-tracking)[![License](https://camo.githubusercontent.com/91a6691d71d4058af597b7177173ce3e12827fb5c293b614bb9cc1d1a13cbc53/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f68656e72796176696c612f656d61696c2d747261636b696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/henryavila/email-tracking)

Track email delivery, opens, clicks, and more using Mailgun webhooks. All data is stored in your database for easy querying and analytics.

✨ Features
----------

[](#-features)

- 📧 **Complete Email Tracking** - Track sent, delivered, opened, clicked, bounced, and failed emails
- 🔗 **Model Association** - Link emails to any Eloquent model (User, Order, Invoice, etc.)
- 🎯 **Email Categorization** - Classify emails by type (transactional, marketing, notifications, etc.)
- 📊 **Built-in Analytics** - Query delivery rates, open rates, click rates by email type
- 🪝 **Mailgun Webhooks** - Automatic event processing from Mailgun
- 🔒 **Secure Webhooks** - Signature verification for webhook security
- 💾 **Database Storage** - All email data stored in your database
- 🧪 **Fully Tested** - Comprehensive test suite included
- 📱 **Laravel 11+ &amp; 12** - Modern Laravel support with latest features

📋 Requirements
--------------

[](#-requirements)

- PHP 8.2, 8.3, or 8.4 (PHP 8.5 support planned for v7.1.0)
- Laravel 11.0 or higher (Laravel 11 LTS and Laravel 12 supported)
- Mailgun account

📊 Code Coverage
---------------

[](#-code-coverage)

[![Coverage Graph](https://camo.githubusercontent.com/9501b8f4e59102600cb72b25cd1f0f371bc0a4281f7b852e7531ac5f2791a1ff/68747470733a2f2f636f6465636f762e696f2f67682f68656e72796176696c612f656d61696c2d747261636b696e672f6772617068732f696369636c652e7376673f746f6b656e3d31544d4f523330303143)](https://codecov.io/gh/henryavila/email-tracking)

This package maintains high test coverage with comprehensive unit and integration tests. All new features are fully tested before release.

📦 Installation
--------------

[](#-installation)

### 1. Install via Composer

[](#1-install-via-composer)

```
composer require henryavila/email-tracking
```

### 2. Publish and Run Migrations

[](#2-publish-and-run-migrations)

```
php artisan vendor:publish --tag="email-tracking-migrations"
php artisan migrate
```

### 3. Publish Configuration (Optional)

[](#3-publish-configuration-optional)

```
php artisan vendor:publish --tag="email-tracking-config"
```

### 4. Configure Mailgun

[](#4-configure-mailgun)

Setup Laravel Mail with Mailgun driver. See [Laravel Mail Documentation](https://laravel.com/docs/master/mail#mailgun-driver).

Add to your `.env` file:

```
MAIL_MAILER=mailgun
MAILGUN_DOMAIN=yourdomain.com
MAILGUN_SECRET=key-99999999999999999999999999999999
```

### 5. Setup Mailgun Webhook

[](#5-setup-mailgun-webhook)

In your Mailgun dashboard, add a webhook pointing to:

```
https://yourdomain.com/webhooks/mailgun

```

⚙️ Configuration
----------------

[](#️-configuration)

### Register Event Listener

[](#register-event-listener)

The package needs to listen for sent emails to track them.

Add to `AppServiceProvider::boot()`:

```
public function boot(): void
{
    \Illuminate\Support\Facades\Event::listen(
        events: \Illuminate\Mail\Events\MessageSent::class,
        listener: \HenryAvila\EmailTracking\Listeners\LogEmailSentListener::class
    );
}
```

### Configuration File

[](#configuration-file)

The published config file (`config/email-tracking.php`) allows customization:

```
return [
    /**
     * Database connection for Email model (optional)
     * If null, uses default connection
     */
    'email-db-connection' => null,

    /**
     * Save HTML body of sent emails
     */
    'log-body-html' => true,

    /**
     * Save text body of sent emails
     */
    'log-body-txt' => true,
];
```

🚀 Usage
-------

[](#-usage)

### Basic Mailable with Tracking

[](#basic-mailable-with-tracking)

Extend `TrackableMail` instead of Laravel's `Mailable`:

```
use HenryAvila\EmailTracking\Mail\TrackableMail;

class OrderShippedMail extends TrackableMail
{
    public function __construct($order)
    {
        $viewData = [
            'order' => $order,
            'trackingNumber' => $order->tracking_number,
        ];

        parent::__construct($order, 'emails.order-shipped', $viewData);
    }
}
```

Send the email:

```
$order = Order::find(1);
Mail::to($order->customer)->send(new OrderShippedMail($order));
```

The email will be automatically tracked and linked to the `$order` model.

### Trackable Notifications

[](#trackable-notifications)

For notifications, use `TrackableNotificationMailMessage`:

```
use HenryAvila\EmailTracking\Notifications\TrackableNotificationMailMessage;

class OrderShippedNotification extends Notification
{
    public function __construct(protected Order $order)
    {
    }

    public function toMail($notifiable): MailMessage
    {
        return (new TrackableNotificationMailMessage($this->order))
            ->subject('Your order has been shipped!')
            ->line('Your order #' . $this->order->number . ' is on its way.')
            ->action('Track Shipment', url('/orders/' . $this->order->id))
            ->line('Thank you for your purchase!');
    }
}
```

### Email Type Classification (v7.0.0+)

[](#email-type-classification-v700)

Categorize emails for better organization and analytics.

#### 1. Create Email Type Enum

[](#1-create-email-type-enum)

```
