PHP code example of apps-inteligentes / email-tracking
1. Go to this page and download the library: Download apps-inteligentes/email-tracking library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
apps-inteligentes / email-tracking example snippets
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,
];
public function boot(): void
{
// ...
\Illuminate\Support\Facades\Event::listen(
events: \Illuminate\Mail\Events\MessageSent::class,
listener: \HenryAvila\EmailTracking\Listeners\LogEmailSentListener::class
);
}
class SampleMail extends \Illuminate\Mail\Mailable
{
public function __construct()
{
//
}
public function content(): Content
{
return new Content(
view: 'view.name',
);
}
}
class SampleMail extends \HenryAvila\EmailTracking\Mail\TrackableMail
{
public function __construct($modelSender)
{
$viewData = [];
parent::__construct($modelSender, 'view.name', $viewData]);
}
}
// example: Send the Sample Mail to User with id 1
$user = User::find(1);
Mail::to($user)->send(new App\Mail\SampleMail($user));
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!');
}
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!');
}
// User with id 1 send the sample notification to multiple $clientes
$user = User::find(1);
Notification::send($clientes, new SampleNotification($user));
class EmailWebhookProcessed
{
public function __construct(public readonly AbstractEmailEvent $emailEvent)
{
}
}
declare(strict_types=1);
namespace App\Listeners;
use HenryAvila\EmailTracking\Events\Email\AbstractEmailEvent;
use HenryAvila\EmailTracking\Events\Email\AcceptedEmailEvent;
use HenryAvila\EmailTracking\Events\Email\ClickedEmailEvent;
use HenryAvila\EmailTracking\Events\Email\DeliveredEmailEvent;
use HenryAvila\EmailTracking\Events\Email\OpenedEmailEvent;
use HenryAvila\EmailTracking\Events\Email\PermanentFailureEmailEvent;
use HenryAvila\EmailTracking\Events\Email\SpamComplaintsEmailEvent;
use HenryAvila\EmailTracking\Events\Email\TemporaryFailureEmailEvent;
use HenryAvila\EmailTracking\Events\Email\UnsubscribeEmailEvent;
use HenryAvila\EmailTracking\Events\EmailWebhookProcessed;
class MailgunWebhookProcessedListener
{
public function handle(EmailWebhookProcessed $event): void
{
match ($event->emailEvent::class) {
AcceptedEmailEvent::class => $this->handleAcceptedEmailEvent($event->emailEvent),
ClickedEmailEvent::class => $this->handleClickedEmailEvent($event->emailEvent),
DeliveredEmailEvent::class => $this->handleDeliveredEmailEvent($event->emailEvent),
OpenedEmailEvent::class => $this->handleOpenedEmailEvent($event->emailEvent),
PermanentFailureEmailEvent::class => $this->handlePermanentFailureEmailEvent($event->emailEvent),
TemporaryFailureEmailEvent::class => $this->handleTemporaryFailureEmailEvent($event->emailEvent),
SpamComplaintsEmailEvent::class => $this->handleSpamComplaintsEmailEvent($event->emailEvent),
UnsubscribeEmailEvent::class => $this->handleUnsubscribeEmailEvent($event->emailEvent),
};
}
private function handleAcceptedEmailEvent(AcceptedEmailEvent $emailEvent)
{
// Implement your logic for handling accepted email events here
}
private function handleClickedEmailEvent(ClickedEmailEvent $emailEvent)
{
// Implement your logic for handling clicked email events here
}
private function handleDeliveredEmailEvent(DeliveredEmailEvent $emailEvent)
{
// Implement your logic for handling delivered email events here
}
private function handleOpenedEmailEvent(OpenedEmailEvent $emailEvent)
{
// Implement your logic for handling opened email events here
}
private function handlePermanentFailureEmailEvent(PermanentFailureEmailEvent $emailEvent)
{
// Implement your logic for handling permanent failure email events here
}
private function handleTemporaryFailureEmailEvent(TemporaryFailureEmailEvent $emailEvent)
{
// Implement your logic for handling temporary failure email events here
}
private function handleSpamComplaintsEmailEvent(SpamComplaintsEmailEvent $emailEvent)
{
// Implement your logic for handling spam complaints email events here
}
private function handleUnsubscribeEmailEvent(UnsubscribeEmailEvent $emailEvent)
{
// Implement your logic for handling unsubscribe email events here
}
}