PHP code example of myth / courier

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

    

myth / courier example snippets


// app/Config/Modules.php
public $aliases = ['routes'];

$routes->group('my-prefix', ['namespace' => 'Myth\Courier\Controllers'], static function ($routes): void {
    $routes->get('open/(:segment)',        'CourierController::open/$1');
    $routes->get('click/(:segment)',       'CourierController::click/$1');
    $routes->get('unsubscribe/(:segment)', 'CourierController::unsubscribe/$1');
    $routes->post('capture',              'CourierController::capture');
});

$contactService = service('contactService');

// Basic subscribe
$contact = $contactService->subscribe([
    'email'      => '[email protected]',
    'first_name' => 'Jane',
]);

// Subscribe with tags
$contact = $contactService->subscribe($data, tags: ['newsletter', 'vip']);

// Subscribe and immediately enroll in a drip campaign
$contact = $contactService->subscribe($data, tags: ['trial'], dripCampaignId: $drip->id);

$campaignService = service('campaignService');

$campaign = $campaignService->create([
    'name'       => 'May Newsletter',
    'subject'    => 'What\'s new in May',
    'view'       => 'emails/newsletter',
    'from_name'  => 'ACME Co.',
    'from_email' => '[email protected]',
    'tag_filter' => ['newsletter'],  // or 'segment_id' => 3
]);

$campaignService->schedule($campaign->id, new DateTime('+1 hour'));
// Courier CLI will pick it up on the next run

$campaignService = service('campaignService');
$dripService     = service('dripService');

// 1. Create the campaign
$campaign = $campaignService->create([
    'name'    => 'Welcome Drip',
    'subject' => 'Welcome!',
    'type'    => 'drip_sequence',
]);

// 2. Add steps
$campaignService->addDripStep($campaign->id, [
    'view'        => 'emails/welcome_step1',
    'subject'     => 'Welcome to the family',
    'delay_hours' => 0,   // send immediately on enroll
]);
$campaignService->addDripStep($campaign->id, [
    'view'        => 'emails/welcome_step2',
    'subject'     => 'How are you settling in?',
    'delay_hours' => 48,
]);

// 3. Enroll a contact
$dripService->enroll(contactId: $contact->id, campaignId: $campaign->id);

$campaignService->create([
    'name'       => 'Trial Users',
    'tag_filter' => ['trial', 'active'],  // stored as JSON
    // ...
]);

$segmentService = service('segmentService');
$contacts = $segmentService->resolve($segment->id);        // array of ContactDTO
$count    = $segmentService->previewCount($segment->id);   // int

// app/Views/emails/welcome.php
<p>Hi <?= esc($contact->first_name) 

// app/Commands/HandleBounces.php
namespace App\Commands;

use Myth\Courier\Commands\TrackEvents;
use Myth\Courier\Models\ContactModel;
use Myth\Courier\Models\EventModel;

class HandleBounces extends TrackEvents
{
    protected $name        = 'courier:handle-bounces';
    protected $description = 'Process bounce webhooks from email provider.';

    public function run(array $params): void
    {
        // Fetch bounce data from your provider's API or queue…
        foreach ($this->fetchBounces() as $email) {
            $this->processBounce($email);
        }
    }

    protected function processBounce(string $email): void
    {
        $contact = model(ContactModel::class)->where('email', $email)->first();
        if ($contact === null) {
            return;
        }

        model(ContactModel::class)->update($contact->id, ['status' => 'bounced']);
        service('contactService')->cancelAllDrips($contact->id);
        model(EventModel::class)->insert(['send_id' => 0, 'type' => 'bounce']);
    }
}
bash
php spark publish:config Courier