rkingbishnoi/brevo-laravel-sdk

A Laravel package that provides an easy-to-use interface for interacting with Brevo's email, SMS, and marketing automation services, enabling seamless API integration for messaging and campaign management.

v1.0.0 2025-03-19 20:14 UTC

This package is not auto-updated.

Last update: 2025-06-12 14:22:58 UTC


README

Packagist License Laravel PHP

A Laravel package providing an easy-to-use interface for interacting with Brevo's email, SMS, and marketing automation services. This SDK enables seamless API integration for messaging and campaign management.

📌 Features

Built on the Official Brevo PHP SDK

Easy Brevo API Integration

Support for Email, SMS, WhatsApp, and Campaign APIs

Simple Laravel Facade for Quick Usage

Built-in Exception Handling & API Validation

Secure API Key Configuration

📦 Installation

Step 1: Install via Composer

You can install the package via Composer:

composer require rkingbishnoi/brevo-laravel-sdk

Step 2: Publish the Configuration

Publish the package configuration file to your application: bash Copy Edit

php artisan vendor:publish --tag=config

This will create a config/brevo.php file where you can set your Brevo API key.

Step 3: Set the API Key

Add your Brevo API key in your .env file:

BREVO_API_KEY=your-brevo-api-key

🚀 Usage Guide

Once you have installed and configured the package, you can start using it in your Laravel application.

This package provides a facade (Brevo) for interacting with various Brevo API services such as Transactional Emails, SMS, Contacts, and Campaigns.

📝 Quick Cheat Sheet

ActionAPI MethodExample Usage 📌
Send Transactional EmailBrevo::transactionalEmailsApi()->sendTransacEmail()📌 View Code
Send Transactional SMSBrevo::transactionalSmsApi()->sendTransacSms()📌 View Code
Fetch Account DetailsBrevo::accountApi()->getAccount()📌 View Code
Fetch Contact ListsBrevo::listsApi()->getLists()📌 View Code
Send WhatsApp MessageBrevo::transactionalWhatsAppApi()->sendWhatsAppTemplateMessage()📌 View Code

📧 Sending an Email via Brevo

use Rkingbishnoi\BrevoLaravelSdk\Facades\Brevo;

try {
    $emailApi = Brevo::transactionalEmailsApi();
    $response = $emailApi->sendTransacEmail([
        'sender' => ['name' => 'Your Company', 'email' => 'your-email@yourdomain.com'],
        'to' => [['email' => 'recipient@example.com', 'name' => 'Recipient Name']],
        'subject' => 'Welcome to our platform!',
        'htmlContent' => '<p>Hello, thank you for signing up!</p>',
    ]);

    return response()->json(['success' => true, 'response' => $response]);
} catch (Exception $e) {
    return response()->json(['error' => $e->getMessage()], 500);
}

🔥 Error Handling

This package comes with built-in exception handling for API-related issues.

Handling Missing API Key If the API key is missing, the package will throw:

throw new MissingApiKeyException('The Brevo API key is missing. Please set the [BREVO_API_KEY] in your .env file or config/brevo.php.');```

How to Handle?

use Rkingbishnoi\BrevoLaravelSdk\Facades\Brevo;
use Rkingbishnoi\BrevoLaravelSdk\Exceptions\MissingApiKeyException;

try {
    $account = Brevo::accountApi()->getAccount();
} catch (MissingApiKeyException $e) {
    return response()->json(['error' => $e->getMessage()], 400);
} catch (Exception $e) {
    return response()->json(['error' => 'Something went wrong.'], 500);
}

Testing To run tests, use:

vendor/bin/phpunit

If you haven't installed Orchestra Testbench, install it with:

composer require --dev orchestra/testbench

Then run :

vendor/bin/phpunit

📄 License

This package is open-source software licensed under the MIT License.

📢 Contributing

We encourage and appreciate contributions! If you have any suggestions, improvements, or bug fixes, feel free to submit a pull request or start a discussion via issues.

📌 How to Contribute

  1. Fork the repository
  2. Create a new branch (git checkout -b feature-branch)
  3. Make your changes and ensure they follow best practices
  4. Commit your changes (git commit -m "Describe your changes")
  5. Push to your branch (git push origin feature-branch)
  6. Submit a pull request for review

🚀 Let's build something great together!