drewm/mailchimp-api
Simple PHP wrapper for the Mailchimp API. Send campaigns, manage lists/audiences, subscribers and members, view reports, and handle API calls with minimal setup. Lightweight, Composer-friendly, and easy to integrate into existing PHP/Laravel apps.
Installation
composer require drewm/mailchimp-api
Add to composer.json if using a monorepo or custom package structure.
First Use Case: Sending a Campaign
use Drewm\MailChimp\MailChimp;
$mailchimp = new MailChimp('your-dc-mailchimp-api-key', 'us12'); // Replace with your DC and API key
$response = $mailchimp->campaigns->create([
'type' => 'regular',
'recipients' => ['list_id' => 'your-list-id'],
'settings' => [
'subject_line' => 'Hello, world!',
'from_name' => 'Your Name',
'reply_to' => 'reply@example.com',
'template_id' => 12345,
],
]);
Where to Look First
src/MailChimp.php for core logic.tests folder for real-world usage patterns.$lists = $mailchimp->lists->getAll();
$mailchimp->lists->subscribe('list-id', [
'email_address' => 'user@example.com',
'merge_fields' => ['FNAME' => 'John', 'LNAME' => 'Doe'],
]);
$campaign = $mailchimp->campaigns->create([...]);
$mailchimp->campaigns->send($campaign['id']);
$mailchimp->campaigns->schedule($campaign['id'], '2023-12-31T12:00:00');
$mailchimp->automations->trigger('journey-id', 'user@example.com');
$webhook = $mailchimp->webhooks->create([
'url' => 'https://your-app.com/webhook',
'events' => ['subscribe', 'unsubscribe'],
]);
Drewm\MailChimp\Exceptions\RateLimitException gracefully.try {
$response = $mailchimp->lists->get('list-id');
} catch (\Drewm\MailChimp\Exceptions\ApiException $e) {
// Log or notify (e.g., Slack, Sentry)
logger()->error($e->getMessage());
}
MailChimpTestCase from the tests folder as a template for unit/integration tests. Mock the API responses with Guzzle’s MockHandler.API Key and DC Misconfiguration
datacenter (e.g., us12 vs. eu1) or API key causes 401 Unauthorized errors.$mailchimp = new MailChimp('us12_your-api-key', 'us12'); // Correct format
Deprecated Endpoints
transactional or ecommerce).Webhook Verification
Route::post('/mailchimp-webhook', function (Request $request) {
$signature = $request->header('X-Mailchimp-Signature');
$payload = $request->getContent();
// Verify using MailChimp's webhook signature logic
// https://developer.mailchimp.com/documentation/mailchimp/guides/webhook-signature-verification/
});
Merge Fields and Segmentation
FNAME, LNAME) must match MailChimp’s exact naming.$mergeFields = $mailchimp->lists->mergeFields('list-id');
$mailchimp = new MailChimp('key', 'dc', [
'debug' => true, // Logs requests/responses to `storage/logs/mailchimp.log`
]);
$response = $mailchimp->lists->get('list-id');
logger()->debug($response->getBody()->getContents());
Custom Endpoints
Add support for unsupported endpoints by extending the MailChimp class:
class CustomMailChimp extends \Drewm\MailChimp\MailChimp {
public function customEndpoint($method, $path, $data = []) {
return $this->request($method, $path, $data);
}
}
Middleware for Requests
Override the request method to add headers or logging:
$mailchimp->setClient(new \GuzzleHttp\Client([
'headers' => [
'User-Agent' => 'YourApp/1.0',
],
]));
Batch Operations The package doesn’t natively support batch operations (e.g., bulk unsubscribes). Use the MailChimp API’s batch endpoints and wrap them in a custom service:
$mailchimp->request('POST', '/lists/list-id/actions/unsubscribe', [
'batch_size' => 1000,
'data' => ['emails' => ['user1@example.com', 'user2@example.com']],
]);
10s. Increase for large operations:
$mailchimp->setClient(new \GuzzleHttp\Client(['timeout' => 30]));
$mailchimp->setClient(new \GuzzleHttp\Client(['verify' => false]));
How can I help you explore Laravel packages today?