|

|  How to Integrate Sendinblue API for Email Campaigns in PHP

How to Integrate Sendinblue API for Email Campaigns in PHP

October 31, 2024

Learn to effortlessly integrate Sendinblue API in PHP and streamline your email campaigns with this step-by-step guide. Perfect for developers looking to optimize communication.

How to Integrate Sendinblue API for Email Campaigns in PHP

 

Install Sendinblue PHP Library

 

  • First, install the API client library using Composer to manage dependencies easily and ensure compatibility.
  •  

  • In your project directory, run the following command:

 

composer require sendinblue/api-v3-sdk

 

Configure API Client

 

  • Initialize the API client with your API key. You must provide the correct API key to authenticate requests.

 

require 'vendor/autoload.php';

$config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', 'YOUR_SENDINBLUE_API_KEY');
$apiInstance = new SendinBlue\Client\Api\TransactionalEmailsApi(new GuzzleHttp\Client(), $config);

 

Create the Email Campaign

 

  • Define the campaign details such as sender, subject, and content. This involves creating a new instance of the model representing the email campaign.
  • Ensure that you provide valid and appropriately formatted data for each required field.

 

$emailCampaignsApi = new SendinBlue\Client\Api\EmailCampaignsApi(new GuzzleHttp\Client(), $config);

$emailCampaign = new \SendinBlue\Client\Model\CreateEmailCampaign();
$emailCampaign->setTag('Campaign Tag');
$emailCampaign->setSender(['name' => 'Your Name', 'email' => 'your.email@example.com']);
$emailCampaign->setName('My First Campaign');
$emailCampaign->setSubject('See the latest updates');
$emailCampaign->setHtmlContent('<html><body><p>Hello, this is your newsletter!</p></body></html>');
$emailCampaign->setRecipients(['listIds' => [12, 34]]);
$emailCampaign->setScheduledAt('2023-12-01T00:00:00+00:00');

// Create the campaign
try {
    $result = $emailCampaignsApi->createEmailCampaign($emailCampaign);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling EmailCampaignsApi->createEmailCampaign: ', $e->getMessage(), PHP_EOL;
}

 

Send a Transactional Email

 

  • Use the API to send individual transactional emails, if needed. This is different from campaigns and doesn't require setting a predefined list.

 

$emailData = new \SendinBlue\Client\Model\SendSmtpEmail();
$emailData->setSubject('Hello from Sendinblue');
$emailData->setSender(['name' => 'Your Name', 'email' => 'your.email@example.com']);
$emailData->setTo([['email' => 'recipient@example.com', 'name' => 'Recipient Name']]);
$emailData->setHtmlContent('<html><body><p>This is a test email.</p></body></html>');

try {
    $result = $apiInstance->sendTransacEmail($emailData);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling TransactionalEmailsApi->sendTransacEmail: ', $e->getMessage(), PHP_EOL;
}

 

Handle API Responses

 

  • Manage the responses from the API to confirm whether emails were sent successfully or handle errors appropriately.
  • Incorporate logging or user notifications based on the result of your API requests to improve debugging and user interaction.

 

// Example of handling the API response
try {
    $result = $emailCampaignsApi->getCampaignStats('CAMPAIGN_ID');
    echo 'Campaign stats: ', json_encode($result), PHP_EOL;
} catch (Exception $e) {
    echo 'Error while retrieving campaign stats: ', $e->getMessage(), PHP_EOL;
}