PHP code example of trycourier / courier

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

    

trycourier / courier example snippets


use Courier\CourierClient;
use Courier\Requests\SendMessageRequest;
use Courier\Send\Types\ContentMessage;
use Courier\Send\Types\ElementalContentSugar;
use Courier\Send\Types\UserRecipient;

$courier = new CourierClient();
$response = $courier->send(
    request: new SendMessageRequest([
        'message' => new ContentMessage([
            'to' => [
                new UserRecipient([
                    'email' => '[email protected]',
                    'data' => [
                        'name' => 'Marty',
                    ],
                ]),
            ],
            'content' => new ElementalContentSugar([
                'title' => 'Back to the Future',
                'body' => 'Oh my {{name}}, we need 1.21 Gigawatts!',
            ]),
        ]),
    ])
);

use Courier\CourierClient;

$courier = new CourierClient("COURIER_AUTH_TOKEN");

use Courier\CourierClient;

$courier = new CourierClient(); // Token is read from the COURIER_AUTH_TOKEN environment variable

use Courier\CourierClient;
use Courier\Environments;

$courier = new CourierClient(options: [
    'baseUrl' => Environments::Production->value // Used by default
]);

use Courier\CourierClient;

$courier = new CourierClient(options: [
    'baseUrl' => 'https://custom-staging.com'
]);

use Courier\Messages\Types\MessageDetails;
use Courier\Messages\Types\MessageStatus;

$messageDetails = new MessageDetails([
    'status' => MessageStatus::Delivered->value,
]);

/**
 * @var value-of<MessageStatus> $status The current status of the message.
 */

use Courier\Exceptions\CourierApiException;
use Courier\Exceptions\CourierException;

try {
    $courier->lists->get(...);
} catch (CourierApiException $e) {
    echo 'Courier API Exception occurred: ' . $e->getMessage() . "\n";
    echo 'Status Code: ' . $e->getCode() . "\n";
    echo 'Response Body: ' . $e->getBody() . "\n";
    // Optionally, rethrow the exception or handle accordingly
}

use Courier\Lists\Requests\GetAllListsRequest;

$items = $courier->lists->list(
    request: new GetAllListsRequest([
        'cursor' => 'abc123',
        'pageSize' => 10,
    ])
)->items;

foreach ($items as $list) {
    echo "Found list with ID: " . $list->id . "\n";
}

use GuzzleHttp\Client;
use Courier\CourierClient;

// Create a custom Guzzle client with specific configuration.
$client = new Client([
    'timeout' => 5.0,
]);

// Pass the custom client when creating an instance of the class.
$courier = new CourierClient(options: [
    'client' => $client
]);