cleaniquecoders/mailhistory

Keep track all the emails sent in the your Laravel application.

2.1.0 2024-03-21 04:14 UTC

README

This package will allow you to capture any mail send out either using Mail or Notification features in Laravel.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Installation

You can install the package via composer:

composer require cleaniquecoders/mailhistory

You can publish and run the migrations with:

php artisan vendor:publish --tag="mailhistory-migrations"
php artisan migrate

If you need to configure more, do publish the config file and update the config file as neccessary.

php artisan vendor:publish --tag="mailhistory-config"

Usage

We need to configure two parts - Mail & Notification.

Mail

All mails are required to use mail metadata trait.

<?php

namespace App\Mail;

use CleaniqueCoders\MailHistory\Concerns\InteractsWithMailMetadata;

class DefaultMail extends Mailable
{
    use InteractsWithMailMetadata, SerializesModels;

And in your mail constructor, do call the following method:

public function __construct()
{
    $this->configureMetadataHash();
}

With this setup, we can track which email has been sent or still sending.

Notification

Do configure you mail prior to this step. At the moment, it only works with Mailable, not Mail Message class.

For notifications classes, you will need to add the following trait:

use CleaniqueCoders\MailHistory\Concerns\InteractsWithMail;

class DefaultNotification extends Notification
{
    use InteractsWithMail;

Then in your notification constructor, you need to initialise the mail object.

public function __construct()
{
    $this->setMail(
        new \App\Mails\DefaultMail
    );
}

And update the toMail() as following:

/**
 * Get the mail representation of the notification.
 */
public function toMail(object $notifiable): Mailable
{
    return $this->getMail()->to($notifiable->email);
}

Artisan Commands

If you need to clean up your mail history table, you can run:

php artisan mailhistory:clear

If you need to test the mail or notification:

php artisan mailhistory:test you-email@app.com --mail
php artisan mailhistory:test you-email@app.com --notification

Do take note, for mail testing, you need a user record that have the email address as we are using Notifiable trait.

You may run the test using specified queue:

php artisan mailhistory:test you-email@app.com --mail --queue=mail

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.