henryavila / email-tracking
Track e-mail delivery with Mailgun Hooks. All data are stored in the database on Email model
Installs: 1 997
Dependents: 1
Suggesters: 0
Security: 0
Stars: 6
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1.0
- illuminate/contracts: ^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.9.2
- spatie/laravel-permission: ^3.0|^4.0|^5.0|^6.0
Requires (Dev)
- laravel/pint: ^1.17
- nunomaduro/collision: ^6.0|^7.0|^8.1
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0|^8.0|^9.0
- pestphp/pest: ^v2.13.0
- pestphp/pest-plugin-laravel: ^v2.3.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5|^10.0
- dev-main
- v5.3.0
- v5.2.0
- v5.1.1
- v5.1.0
- v5.0.1
- v5.0.0
- v4.0.2
- v4.0.1
- 3.0.0
- 2.2.0
- 2.1.0
- 2.0.0
- 1.2.2
- 1.2.0
- 1.1.0
- 1.0.2.x-dev
- 1.0.2
- v1.0.1.x-dev
- 1.0.1
- 1.0
- dev-blank_line_if
- dev-line_break
- dev-fix-publish-config-file
- dev-fix-middleware
- dev-fix-middlware
- dev-remove-laravel-nova
- dev-henryavila-patch-1
- dev-laravel-11
- dev-log-email-body
- dev-fix#1
- dev-model-connection
This package is auto-updated.
Last update: 2024-10-06 15:55:12 UTC
README
Rastreamento de e-mail com Laravel
Abandon Laravel Nova
Since I've abandoned Laravel Nova in favor of Filament, This package will no longer add support to Laravel Nova. The exact content of this package with Laravel Nova has been moved to a new package https://packagist.org/packages/henryavila/laravel-nova-email-tracking If you are using Laravel Nova, please use this new package.
Mailgun configuration
On mailgun interface, add a webhook
to the url <APP_URL>/webhooks/mailgun
Installation
Setup Laravel Mail with mailgun at https://laravel.com/docs/master/mail#mailgun-driver
Define the environments variable in your .env
file
MAIL_MAILER=mailgun
MAILGUN_DOMAIN=yourdomain.com
MAILGUN_SECRET=key-99999999999999999999999999999999
Install the package via composer:
composer require henryavila/email-tracking
Publish and run the migrations with:
php artisan vendor:publish --tag="email-tracking-migrations"
php artisan migrate
Publish the config file with:
php artisan vendor:publish --tag="email-tracking-config"
This is the contents of the published config file:
return [ /** * if defined, the Email model will use this database connection. * This connection name must be defined in database.connections config file */ 'email-db-connection' => null, /** * Save the HTML Body of all sent messages */ 'log-body-html' => true, /** * Save the TXT Body of all sent messages */ 'log-body-txt' => true, ];
Configuration
On all models that can send e-mail, add the trait ModelWithEmailsSenderTrait
For Laravel 10, add this conde in EventServiceProvider.php
file
protected $listen = [ \Illuminate\Mail\Events\MessageSent::class => [ \HenryAvila\EmailTracking\Listeners\LogEmailSentListener::class, ], ];
For Laravel 11, Add this code inside the boot()
method of AppServiceProvider.php
public function boot(): void { // ... \Illuminate\Support\Facades\Event::listen( events: \Illuminate\Mail\Events\MessageSent::class, listener: \HenryAvila\EmailTracking\Listeners\LogEmailSentListener::class ); }
At this point, all e-mail sent from app, will be logged on the app, but the sender will not be saved
Save the Email sender
To be able to track the e-mail sender, you must create a custom Mailable
or Notification
.
Mailable
When creating a new Mailable, it must extend the Class with HenryAvila\EmailTracking\Mail\TrackableMail
Also, You must change the constructor and content function.
This is the default mail class:
class SampleMail extends \Illuminate\Mail\Mailable { public function __construct() { // } public function content(): Content { return new Content( view: 'view.name', ); } }
Change the class to this:
class SampleMail extends \HenryAvila\EmailTracking\Mail\TrackableMail { public function __construct($modelSender) { $viewData = []; parent::__construct($modelSender, 'view.name', $viewData]); } }
To send the Mailable, just pass the model in the mailable constructor
// example: Send the Sample Mail to User with id 1 $user = User::find(1); Mail::to($user)->send(new App\Mail\SampleMail($user));
Notification
When creating a notification, all you have to do is to change the toMail()
method.
Replace the default code:
public function toMail($notifiable): MailMessage { return (new MailMessage) ->line('The introduction to the notification.') ->action('Notification Action', url('/')) ->line('Thank you for using our application!'); }
with this code:
public function __construct(protected \Illuminate\Database\Eloquent\Model $model) { // } public function toMail($notifiable): MailMessage { return (new \HenryAvila\EmailTracking\Notifications\TrackableNotificationMailMessage($this->model)) ->line('The introduction to the notification.') ->blankLine() ->line('Another line, after a blank line') ->blankLineIf($condition) ->action('Notification Action', url('/')) ->line('Thank you for using our application!'); }
To send the notification
// User with id 1 send the sample notification to multiple $clientes $user = User::find(1); Notification::send($clientes, new SampleNotification($user));
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.