PHP code example of getbrevo / brevo-php

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

    

getbrevo / brevo-php example snippets


// v4
$client->event->createBatchEvents($eventsArray);

// v5
use Brevo\Event\Requests\CreateBatchEventsRequest;

$client->event->createBatchEvents(
    new CreateBatchEventsRequest(['events' => $eventsArray])
);

use Brevo\Client\Configuration;
use Brevo\Client\Api\TransactionalEmailsApi;
use Brevo\Client\Model\SendSmtpEmail;

$config = Configuration::getDefaultConfiguration()->setApiKey('api-key', 'xkeysib-xxx');
$api = new TransactionalEmailsApi(new \GuzzleHttp\Client(), $config);

$message = new SendSmtpEmail();
$message->setSubject('First email');
$message->setTextContent('Hello world!');
$message->setSender(['name' => 'Bob Wilson', 'email' => '[email protected]']);
$message->setTo([['email' => '[email protected]', 'name' => 'Sarah Davis']]);

$api->sendTransacEmail($message);

use Brevo\Brevo;
use Brevo\TransactionalEmails\Requests\SendTransacEmailRequest;
use Brevo\TransactionalEmails\Types\SendTransacEmailRequestSender;
use Brevo\TransactionalEmails\Types\SendTransacEmailRequestToItem;

$brevo = new Brevo('xkeysib-xxx');

$brevo->transactionalEmails->sendTransacEmail(
    new SendTransacEmailRequest([
        'subject' => 'First email',
        'textContent' => 'Hello world!',
        'sender' => new SendTransacEmailRequestSender([
            'name' => 'Bob Wilson',
            'email' => '[email protected]',
        ]),
        'to' => [
            new SendTransacEmailRequestToItem([
                'email' => '[email protected]',
                'name' => 'Sarah Davis',
            ]),
        ],
    ])
);



namespace Example;

use Brevo\Brevo;
use Brevo\TransactionalEmails\Requests\SendTransacEmailRequest;
use Brevo\TransactionalEmails\Types\SendTransacEmailRequestSender;
use Brevo\TransactionalEmails\Types\SendTransacEmailRequestToItem;

$client = new Brevo(
    apiKey: '<value>',
);
$client->transactionalEmails->sendTransacEmail(
    new SendTransacEmailRequest([
        'htmlContent' => '<html><head></head><body><p>Hello,</p>This is my first transactional email sent from Brevo.</p></body></html>',
        'sender' => new SendTransacEmailRequestSender([
            'email' => '[email protected]',
            'name' => 'Alex from Brevo',
        ]),
        'subject' => 'Hello from Brevo!',
        'to' => [
            new SendTransacEmailRequestToItem([
                'email' => '[email protected]',
                'name' => 'John Doe',
            ]),
        ],
    ]),
);


use Brevo\Exceptions\BrevoApiException;
use Brevo\Exceptions\BrevoException;

try {
    $response = $client->transactionalEmails->sendTransacEmail(...);
} catch (BrevoApiException $e) {
    echo '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 Brevo\Brevo;

// Pass any PSR-18 compatible HTTP client implementation.
// For example, using Guzzle:
$customClient = new \GuzzleHttp\Client([
    'timeout' => 5.0,
]);

$client = new Brevo(
    apiKey: 'xkeysib-xxx',
    options: ['client' => $customClient],
);

// Or using Symfony HttpClient:
// $customClient = (new \Symfony\Component\HttpClient\Psr18Client())
//     ->withOptions(['timeout' => 5.0]);
//
// $client = new Brevo(
//     apiKey: 'xkeysib-xxx',
//     options: ['client' => $customClient],
// );

$response = $client->transactionalEmails->sendTransacEmail(
    ...,
    options: [
        'maxRetries' => 0 // Override maxRetries at the request level
    ]
);

// Client-level
$client = new Brevo('xkeysib-xxx', ['timeout' => 30.0]);

// Request-level (overrides client setting)
$response = $client->transactionalEmails->sendTransacEmail(
    ...,
    options: [
        'timeout' => 3.0
    ]
);

use Brevo\Brevo;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\MessageFormatter;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logger = new Logger('brevo');
$logger->pushHandler(new StreamHandler('php://stderr', Logger::DEBUG));

$stack = HandlerStack::create();
$stack->push(Middleware::log(
    $logger,
    new MessageFormatter('{method} {uri} → {code}')
));

$client = new Brevo('xkeysib-xxx', [
    'client' => new Client(['handler' => $stack, 'timeout' => 5.0]),
]);
bash
composer