dcsg/mailchimp-api-connector-bundle
Install the Bundle
composer require dcsg/mailchimp-api-connector-bundle:~2.0
Enable the bundle in config/bundles.php:
return [
// ...
Dcsg\MailchimpApiConnectorBundle\DcsgMailchimpApiConnectorBundle::class => ['all' => true],
];
Configure Mailchimp API Key
Add your Mailchimp API key to config/packages/dcsg_mailchimp_api_connector.yaml:
dcsg_mailchimp_api_connector:
api_key: 'your-mailchimp-api-key-us12' # Replace with your key (e.g., 'us12' for US region)
First Use Case: Fetching Lists
Inject the MailchimpApiConnector service and fetch a list of Mailchimp lists:
use Dcsg\MailchimpApiConnectorBundle\Service\MailchimpApiConnector;
class MailchimpService
{
public function __construct(private MailchimpApiConnector $mailchimp)
{
}
public function getLists()
{
return $this->mailchimp->getLists();
}
}
Subscribing Users to a List
$this->mailchimp->subscribe('list-id', [
'email' => 'user@example.com',
'merge_fields' => [
'FNAME' => 'John',
'LNAME' => 'Doe',
],
]);
Fetching Subscribers
$subscribers = $this->mailchimp->getSubscribers('list-id', ['status' => 'subscribed']);
Sending Campaigns
$campaign = $this->mailchimp->createCampaign([
'type' => 'regular',
'recipients' => ['list_id' => 'list-id'],
'settings' => [
'subject_line' => 'Hello from Laravel!',
'from_name' => 'Your App',
'reply_to' => 'noreply@example.com',
],
]);
Handling Webhooks (Legacy) Use Symfony’s event system to process Mailchimp webhooks (e.g., subscriber updates):
# config/packages/dcsg_mailchimp_api_connector.yaml
dcsg_mailchimp_api_connector:
webhook:
enabled: true
secret: 'your-webhook-secret'
events: ['subscribe', 'unsubscribe']
ContainerInterface to access the service in Laravel:
$mailchimp = app('mailchimp_api_connector');
try {
$this->mailchimp->getLists();
} catch (\Dcsg\MailchimpApiConnector\Exception\MailchimpException $e) {
Log::error('Mailchimp API error: ' . $e->getMessage());
}
$subscribers = [];
$offset = 0;
do {
$batch = $this->mailchimp->getSubscribers('list-id', ['offset' => $offset, 'limit' => 100]);
$subscribers = array_merge($subscribers, $batch);
$offset += 100;
} while (!empty($batch));
Deprecated Bundle: The last release was in 2014. Ensure compatibility with:
/ecommerce/stores). Use the official Mailchimp PHP SDK for v3+.Webhook Limitations:
Http facade or a queue worker to poll for updates instead.API Key Format:
us12, eu1). Omitting this will cause 401 Unauthorized errors.Rate Limiting:
use Symfony\Component\HttpKernel\Exception\RateLimitExceededHttpException;
try {
$this->mailchimp->getLists();
} catch (RateLimitExceededHttpException $e) {
sleep(1); // Wait before retrying
retry();
}
Enable Verbose Logging:
# config/packages/monolog.yaml
handlers:
mailchimp:
type: stream
path: "%kernel.logs_dir%/mailchimp.log"
level: debug
Log raw API responses to debug payloads or errors.
Test with a Sandbox Account:
Check HTTP Headers:
Authorization: Basic header is correctly formatted:
// Example of manual auth (if bundle fails)
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://us12.admin.mailchimp.com/3.0/',
'headers' => [
'Authorization' => 'Basic ' . base64_encode('api_key:'),
],
]);
Custom API Endpoints:
Override the MailchimpApiConnector service to extend functionality:
# config/services.yaml
services:
App\Service\CustomMailchimpService:
arguments:
$connector: '@mailchimp_api_connector'
class CustomMailchimpService
{
public function __construct(private MailchimpApiConnector $connector)
{
}
public function customEndpoint()
{
return $this->connector->makeRequest('GET', '/custom/endpoint');
}
}
Event Listeners:
Subscribe to Mailchimp events (e.g., mailchimp.subscriber.created) via Symfony’s event dispatcher:
use Dcsg\MailchimpApiConnectorBundle\Event\SubscriberEvent;
class MailchimpSubscriberListener
{
public function onSubscriberCreated(SubscriberEvent $event)
{
// Handle new subscriber
}
}
Register the listener in services.yaml:
services:
App\EventListener\MailchimpSubscriberListener:
tags:
- { name: kernel.event_listener, event: mailchimp.subscriber.created, method: onSubscriberCreated }
Mocking for Tests:
Use a mock MailchimpApiConnector in PHPUnit:
$mock = $this->createMock(MailchimpApiConnector::class);
$mock->method('getLists')->willReturn(['list1', 'list2']);
$this->container->set('mailchimp_api_connector', $mock);
How can I help you explore Laravel packages today?