PHP code example of waynebrummer / mail-telemetry

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

    

waynebrummer / mail-telemetry example snippets


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

\Mail::send('email.test', [], function ($message) {
    // ... other settings here
    $message->getHeaders()->addTextHeader('X-Email-Notification-ID',$this->id);
});

/**
 * Mailable function returned.
 *
 * @param mixed $notifiable
 *
 * @return \App\Mail\AssignedToMail
 */
public function toMail($notifiable) : \App\Mail\AssignedToMail
{
    return new AssignedToMail($notifiable, $this->model, $this->id);
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    $this->withSwiftMessage(function ($message) {
        $message->getHeaders()->addTextHeader('X-Email-Notification-ID', $this->notification);
    });
    return $this->to($this->user->email)
        ->subject("Assignment: {$this->user->first_name} {$this->user->last_name},")
        ->markdown('vendor.notifications.email', $this->message->data());
}




namespace App\Listeners;

use Pace\MailTelemetry\Events\EmailEvent;

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

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

/**
 * 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-ID',$model->id);
});

public function handle(EmailSentEvent $event)
{
    $tracker = $event->sent_email;
    $model_id = $event->sent_email->getHeader('X-Model-ID');
    $model = Model::find($model_id);
    // Perform your tracking/linking tasks on $model knowing the SentEmail object
}

...
'auth-route' => [
    'enabled'     => true,
    'prefix'      => 'api/email',
    'middleware'  => ['auth:api', 'can:emails-list-telemetry'],
],
...
bash
php artisan vendor:publish --provider="Pace\MailTelemetry\ServiceProvider"
bash
php artisan migrate
bash
php artisan make:listener EmailViewed