composer require coderbyheart/mailchimp-bundle
config/bundles.php (Laravel 5.4+):
Coderbyheart\MailChimpBundle\CoderbyheartMailChimpBundle::class => true,
.env (or config/services.php for Laravel):
MAILCHIMP_API_KEY=123-us1
MAILCHIMP_RETURN_TYPE=object # or 'array'
use Coderbyheart\MailChimpBundle\MailChimp;
public function __construct(MailChimp $mailchimp) {
$this->mailchimp = $mailchimp;
}
public function getLists() {
return $this->mailchimp->listsList();
}
List Management:
// Create a list
$list = $this->mailchimp->listsCreate([
'name' => 'Newsletter Subscribers',
'contact' => ['company' => 'Acme Corp']
]);
// Add a subscriber
$this->mailchimp->listsSubscribe('list-id', [
'email_address' => 'user@example.com',
'merge_fields' => ['FNAME' => 'John']
]);
Campaigns:
// Create a campaign
$campaign = $this->mailchimp->campaignsCreate([
'type' => 'regular',
'settings' => ['subject_line' => 'Hello!']
]);
// Send a campaign
$this->mailchimp->campaignsSend($campaign['id']);
Batch Operations:
// Sync users from DB to MailChimp
foreach ($users as $user) {
$this->mailchimp->listsUpsert('list-id', [
'email_address' => $user->email,
'merge_fields' => $user->toMailchimpMergeFields()
]);
}
// app/Listeners/UserRegistered.php
public function handle(UserRegistered $event) {
$this->mailchimp->listsSubscribe('newsletter', [
'email_address' => $event->user->email,
'merge_fields' => ['FNAME' => $event->user->name]
]);
}
// app/Console/Commands/SyncMailchimp.php
public function handle() {
$this->mailchimp->listsSync('list-id', $this->getUsers());
}
429 Too Many Requests by implementing retry logic:
try {
$response = $this->mailchimp->listsList();
} catch (\GuzzleHttp\Exception\RequestException $e) {
if ($e->getCode() === 429) {
sleep(30); // Wait 30 seconds
return $this->mailchimp->listsList();
}
throw $e;
}
Deprecated API:
listsUpsert) may behave differently than expected. Prefer listsBatchSubscribe or listsBatchUpsert for bulk operations.$client = $this->mailchimp->getClient();
$response = $client->post('lists/{list-id}/members', [
'json' => ['email_address' => 'test@example.com']
]);
Authentication:
.env and restrict file permissions.Response Handling:
return_type config (object/array) applies globally. Override per-call if needed:
$this->mailchimp->setReturnType('array');
$lists = $this->mailchimp->listsList();
Idempotency:
listsSubscribe may fail if the email already exists. Use listsUpsert or check first:
$members = $this->mailchimp->listsMembersList('list-id', ['email_address' => 'test@example.com']);
if (empty($members)) {
$this->mailchimp->listsSubscribe('list-id', [...]);
}
$this->mailchimp->getClient()->getConfig()->set('debug', true);
try {
$this->mailchimp->ping();
} catch (\Exception $e) {
throw new \RuntimeException('MailChimp API key invalid or server unreachable.');
}
// app/Services/ExtendedMailChimp.php
public function listsBatchUpsert($listId, array $members) {
$client = $this->mailchimp->getClient();
return $client->post("lists/{$listId}/members", [
'json' => ['members' => $members]
]);
}
subscribe, unsubscribe) via Laravel events:
// app/Providers/EventServiceProvider.php
protected $listen = [
'mailchimp.subscribed' => [MailchimpSubscriberHandler::class, 'handle'],
];
$mailchimp = Mockery::mock(MailChimp::class);
$mailchimp->shouldReceive('listsSubscribe')->once();
$this->app->instance(MailChimp::class, $mailchimp);
How can I help you explore Laravel packages today?