cleaniquecoders / mailhistory
Keep track all the emails sent in the your Laravel application.
Installs: 1 396
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 0
Forks: 0
Open Issues: 1
Requires
- php: ^8.1 | ^8.2 | ^8.3
- illuminate/contracts: ^9.0 | ^10.0 | ^11.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.1|^7.9
- orchestra/testbench: ^8.0
- pestphp/pest: ^1.21|^2.0
- pestphp/pest-plugin-laravel: ^1.4|^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
README
This package will allow you to capture any mail send out either using Mail or Notification features in Laravel.
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.
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.