PHP code example of sendboo / sendboo-php

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

    

sendboo / sendboo-php example snippets


use Sendboo\Sendboo;

$sendboo = new Sendboo(
    token:          '123|sb_...',
    organizationId: 'org-uuid',
    storeId:        'store-uuid', // optional; 

$sendboo->track('purchase_completed', [
    'anonymous_id' => $visitorId,
    'properties'   => ['order_id' => 1234, 'total' => 79.90],
]);

// Build the list however you fetch subscribers — here from a plain PDO query.
$rows = [];

foreach ($pdo->query('SELECT email, first_name, plan FROM users WHERE subscribed = 1') as $user) {
    $rows[] = [
        'email'      => $user['email'],
        'first_name' => $user['first_name'],
        'tags'       => $user['plan'] === 'pro' ? ['pro'] : [],
    ];
}

$result = $sendboo->subscribers()->sync('email-list-uuid', $rows, unsubscribeMissing: true);
// ['upserted' => 1200, 'unsubscribed' => 7]

$rows = User::where('subscribed', true)->get()
    ->map(fn (User $user) => [
        'email'      => $user->email,
        'first_name' => $user->first_name,
        'tags'       => $user->plan === 'pro' ? ['pro'] : [],
    ])
    ->all();

$sendboo->subscribers()->sync('email-list-uuid', $rows, unsubscribeMissing: true);



namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Sendboo\Sendboo;

class SyncSendboo extends Command
{
    protected $signature = 'sendboo:sync';

    protected $description = 'Sync subscribed users to the Sendboo email list';

    public function handle(): int
    {
        $sendboo = new Sendboo(
            token:          config('services.sendboo.token'),
            organizationId: config('services.sendboo.organization_id'),
            storeId:        config('services.sendboo.store_id'),
        );

        $rows = User::where('subscribed', true)->get()
            ->map(fn (User $user) => [
                'email'      => $user->email,
                'first_name' => $user->first_name,
                'tags'       => $user->plan === 'pro' ? ['pro'] : [],
            ])
            ->all();

        $result = $sendboo->subscribers()->sync(
            config('services.sendboo.list_uuid'),
            $rows,
            unsubscribeMissing: true,
        );

        $this->info("Synced {$result['upserted']}, unsubscribed {$result['unsubscribed']}.");

        return self::SUCCESS;
    }
}

'sendboo' => [
    'token'           => env('SENDBOO_TOKEN'),
    'organization_id' => env('SENDBOO_ORG_ID'),
    'store_id'        => env('SENDBOO_STORE_ID'),
    'list_uuid'       => env('SENDBOO_LIST_UUID'),
],

use Illuminate\Support\Facades\Schedule;

Schedule::command('sendboo:sync')->dailyAt('03:00');

$subscribers = $sendboo->subscribers();

$subscribers->upsert('email-list-uuid', [
    'email'            => '[email protected]',
    'first_name'       => 'Jane',
    'tags'             => ['vip'],
    'extra_attributes' => ['plan' => 'pro'],
    'skip_confirmation' => true,
]);

$subscribers->unsubscribe('email-list-uuid', '[email protected]');

foreach ($subscribers->all('email-list-uuid') as $subscriber) {
    // ...
}
bash
composer