PHP code example of zanysoft / mail-tracker

1. Go to this page and download the library: Download zanysoft/mail-tracker 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/ */

    

zanysoft / mail-tracker example snippets


\Mail::send('email.test', [], function ($message) {
    // ... other settings here
    $message->getHeaders()->addTextHeader('X-No-Track',Str::random(10));
});



namespace App\Listeners;

use ZanySoft\MailTracker\Events\ViewEmailEvent;

class EmailViewed
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  ViewEmailEvent  $event
     * @return void
     */
    public function handle(ViewEmailEvent $event)
    {
        // Access the model using $event->sent_email...
    }
}



namespace App\Listeners;

use ZanySoft\MailTracker\Events\PermanentBouncedMessageEvent;

class BouncedEmail
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  PermanentBouncedMessageEvent  $event
     * @return void
     */
    public function handle(PermanentBouncedMessageEvent $event)
    {
        // Access the email address using $event->email_address...
    }
}

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'ZanySoft\MailTracker\Events\EmailSentEvent' => [
        'App\Listeners\EmailSent',
    ],
    'ZanySoft\MailTracker\Events\ViewEmailEvent' => [
        'App\Listeners\EmailViewed',
    ],
    'ZanySoft\MailTracker\Events\LinkClickedEvent' => [
        'App\Listeners\EmailLinkClicked',
    ],
    'ZanySoft\MailTracker\Events\EmailDeliveredEvent' => [
        'App\Listeners\EmailDelivered',
    ],
    'ZanySoft\MailTracker\Events\ComplaintMessageEvent' => [
        'App\Listeners\EmailComplaint',
    ],
    'ZanySoft\MailTracker\Events\PermanentBouncedMessageEvent' => [
        'App\Listeners\BouncedEmail',
    ],
];

/**
 * Send an email and do processing on a model with the email
 */
\Mail::send('email.test', [], function ($message) use($email, $subject, $name, $model) {
    $message->from('[email protected]', 'From Name');
    $message->sender('[email protected]', 'Sender Name');
    $message->to($email, $name);
    $message->subject($subject);

    // Create a custom header that we can later retrieve
    $message->getHeaders()->addTextHeader("X-MODEL", "Model Name");
    $message->getHeaders()->addTextHeader('X-Model-ID',$model->id);
});

public function handle(EmailSentEvent $event)
{
    $tracker = $event->sent_email;
    $model_id = $event->sent_email->getHeader('X-Model-ID');
    // or
    $model_id = $event->sent_email->getModelId();

    $model_name = $event->sent_email->getHeader('X-Model');
    // or
    $model_name = $event->sent_email->getModel();

    $model = app("App\\$model_name")->find($model_id);
    // Perform your tracking/linking tasks on $model knowing the SentEmail object
}
bash
$ php artisan vendor:publish --provider="ZanySoft\MailTracker\MailTrackerServiceProvider"
bash
$ php artisan migrate