The LaravelBrevo package is a Laravel wrapper for integrating with the Brevo API (formerly Sendinblue). It simplifies interactions with Brevo's email marketing and contact management features, allowing you to manage contacts, subscribe/unsubscribe users, and retrieve contact information seamlessly within your Laravel application.
This version of the documentation demonstrates how to use the package via the Facade for cleaner and more expressive code.
This package is ideal for:
-
Email Marketing:
- Subscribe users to mailing lists.
- Unsubscribe users from mailing lists.
- Update user attributes (e.g., name, preferences).
-
Contact Management:
- Retrieve contact details.
- Create or update contacts in Brevo.
-
Automation:
- Automatically add new users to Brevo lists during registration.
- Sync user data between your application and Brevo.
You can install the package via composer:
composer require designbycode/laravel-brevo
You can publish the config file with:
php artisan vendor:publish --tag="brevo"
Add your Brevo API key to the .env file:
BREVO_API_KEY=your-api-key
return [
'api_key' => env('BREVO_API_KEY', ''),
];
api_key: Your Brevo API key. This is required to authenticate API requests.
To retrieve details for a specific contact by email:
use Designbycode\LaravelBrevo\Facades\Brevo;
if ($contact = Brevo::getContactInfo('[email protected]')) {
echo "Contact Name: " . $contact->getAttributes()->name;
} else {
echo "Contact not found.";
}
To subscribe a contact to a mailing list:
use Designbycode\LaravelBrevo\Facades\Brevo;
if ($success = Brevo::subscribe('[email protected]', $listId)) {
echo "Contact subscribed successfully!";
} else {
echo "Failed to subscribe contact.";
}
To unsubscribe a contact from a mailing list:
use Designbycode\LaravelBrevo\Facades\Brevo;
if ($success = Brevo::unsubscribe('[email protected]', $listId)) {
echo "Contact unsubscribed successfully!";
} else {
echo "Failed to unsubscribe contact.";
}
Brevo::getContactInfo(string $email): ?GetExtendedContactDetails
- Retrieves contact details for the specified email.
- Returns
null
if the contact is not found.
Brevo::subscribe(string $email, int $listId, array $attributes = []): bool
- Subscribes a contact to a mailing list.
- Creates a new contact if they don't exist, or updates an existing contact.
- Returns
true
on success,false
on failure.
Brevo::unsubscribe(string $email, int $listId): bool
- Unsubscribes a contact from a mailing list.
- Returns
true
on success,false
on failure.
The package handles API errors gracefully:
- 404 Not Found: Logs a warning and returns
null
orfalse
. - 500 Server Error: Logs an error and returns
false
. - Other exceptions are logged and handled appropriately.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.