A thin wrapper module for the Mailchimp Marketing API v3 that provides transparent access to all Mailchimp endpoints for Craft CMS 4+.
- Transparent API Access: Direct pass-through to Mailchimp's API with no abstraction
- Enhanced JavaScript API: Comprehensive client-side wrapper with helper methods
- Webhook Support: Handle Mailchimp webhooks with event dispatching
- Rate Limiting: Built-in rate limiting (30 requests/minute)
- CSRF Protection: Automatic CSRF validation for API endpoints
- Examples: Complete templates demonstrating common use cases
- Craft CMS 4.0+
- PHP 8.0.2+
- Mailchimp account with API key
After adding the submodule, you can use the installation helper script:
# Add the submodule
git submodule add https://github.com/mach1media/craft-mailchimp.git modules/craft-mailchimp
# Run the installation helper
bash modules/craft-mailchimp/install.shThe script will:
- Prompt you for your layout template path
- Copy and update template files automatically
- Set up JavaScript dependencies in the correct locations
- Provide environment variable configuration reminders
- Add the module as a git submodule:
git submodule add https://github.com/mach1media/craft-mailchimp.git modules/craft-mailchimp- Update your
composer.jsonautoload configuration:
{
"autoload": {
"psr-4": {
"rcg\\mailchimp\\": "modules/craft-mailchimp/src/"
}
}
}- Run composer dump-autoload:
composer dump-autoload- Add your Mailchimp credentials to
.env:
MAILCHIMP_API_KEY="your-api-key-here"
MAILCHIMP_LIST_ID="your-list-id"
MAILCHIMP_SIGNUP_URL="https://mailchi.mp/yourdomain/signup"
MAILCHIMP_SERVER_PREFIX="" # Optional: e.g., "us19" (auto-detected if not set)
- Register the module in
config/app.php:
return [
'modules' => [
'mailchimp' => \rcg\mailchimp\MailchimpModule::class,
],
'bootstrap' => ['mailchimp'],
];- Copy and adapt the template files:
# Create mailchimp templates directory
mkdir -p templates/mailchimp
# Copy example templates
cp -r modules/craft-mailchimp/templates/* templates/mailchimp/
# Update the templates to extend your project's layout
# Edit each template to replace the default layout with your project's layout:
# Change: {% extends "_layout" %}
# To: {% extends "_layout/default" %} (or your project's actual layout path)- Copy JavaScript dependencies to your web root:
For standard installations:
# Copy the mailchimp.js file to your public directory
cp modules/craft-mailchimp/resources/js/mailchimp.js public/js/For DDEV installations:
# Copy to your source directory if using a build process
cp modules/craft-mailchimp/resources/js/mailchimp.js src/js/vendor/
# Or copy directly to the public directory
ddev exec cp modules/craft-mailchimp/resources/js/mailchimp.js public/js/If using Laravel Mix or Webpack:
// In your webpack.mix.js or webpack config
mix.copy('modules/craft-mailchimp/resources/js/mailchimp.js', 'public/js/vendor/');Required environment variables in your .env file:
MAILCHIMP_API_KEY- Your Mailchimp API key (required)MAILCHIMP_LIST_ID- Default list ID for operations (optional)MAILCHIMP_SIGNUP_URL- Fallback signup URL (optional)MAILCHIMP_SERVER_PREFIX- API server prefix, e.g., "us19" (auto-detected from API key if not set)
Create config/mailchimp.php for advanced settings:
return [
'enableWebhooks' => true,
'webhookSecret' => 'your-webhook-secret',
'rateLimit' => 30, // requests per minute
];Include the JavaScript wrapper and CryptoJS for MD5 hashing:
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
<script src="/js/mailchimp.js"></script> <!-- Or /js/vendor/mailchimp.js depending on your setup -->Initialize and use the API:
// Initialize the API
const mailchimp = new MailchimpAPI({
csrfTokenName: '{{ craft.app.config.general.csrfTokenName }}',
csrfTokenValue: '{{ craft.app.request.csrfToken }}',
listId: '{{ getenv("MAILCHIMP_LIST_ID") }}'
});
// Check subscription status
const status = await mailchimp.getSubscriptionStatus('user@example.com');
// Subscribe a new member
await mailchimp.subscribeMember('user@example.com', {
FNAME: 'John',
LNAME: 'Doe'
});
// Batch operations
await mailchimp.batchSubscribe(['email1@example.com', 'email2@example.com']);
// Tags
await mailchimp.addTags('user@example.com', ['customer', 'newsletter']);
// Search members
const results = await mailchimp.searchMembers('john');// In PHP
$module = \rcg\mailchimp\MailchimpModule::getInstance();
$response = $module->api->get('/lists');
// Subscribe a member
$response = $module->api->put("/lists/{$listId}/members/" . md5($email), [
'email_address' => $email,
'status' => 'subscribed'
]);{# In Twig #}
{% set module = craft.app.modules.mailchimp %}
{% set response = module.api.get('/lists/' ~ listId) %}
{% if response.success %}
<p>List name: {{ response.data.name }}</p>
{% endif %}The module provides a single endpoint that accepts any Mailchimp API request:
Endpoint: /actions/mailchimp/api/request
Parameters:
method- HTTP method (GET, POST, PATCH, PUT, DELETE)endpoint- Mailchimp API endpoint (e.g.,/lists,/lists/{list_id}/members)params- Request parameters (query params for GET, body for others)
See docs/javascript-api.md for complete documentation of all available methods.
See docs/webhooks.md for webhook configuration and handling.
The module includes example templates in the templates/examples/ directory:
check-subscription.twig- Check if an email is subscribedget-subscriber.twig- Get full subscriber detailslist-info.twig- Display list information and statssignup-url.twig- Get the hosted signup form URL
Access the test interface at /mailchimp/test after installation.
The example templates use a generic layout structure. When integrating into your project:
-
Update the layout inheritance:
{# Change from: #} {% extends "_layout" %} {# To your project's layout: #} {% extends "_layout/default" %}
-
Adjust block names to match your layout:
{# If your layout uses different block names: #} {% block main %} {# instead of 'content' #} <!-- Your content here --> {% endblock %}
-
Update asset paths:
{# Update JavaScript path based on your setup: #} <script src="{{ siteUrl }}js/vendor/mailchimp.js"></script> {# or #} <script src="{{ alias('@web/dist/js/mailchimp.js') }}"></script>
If using a build process, update your configuration to include the Mailchimp JavaScript:
// webpack.mix.js example
mix.js('src/js/app.js', 'public/dist/js')
.copy('modules/craft-mailchimp/resources/js/mailchimp.js', 'public/dist/js/vendor/')
.sass('src/css/app.scss', 'public/dist/css');Important: If you're using GitHub Actions for deployment, you must configure your workflow to properly handle git submodules. Without this, the module won't be available in production.
Update your deployment workflow (.github/workflows/deploy.yml) with these changes:
- Update the checkout action:
- name: Checkout Code
uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0- Add submodule commands to your server deployment:
- name: Deploy to Server
run: |
ssh user@server << 'EOF'
cd /path/to/your/app
git pull origin main
echo "Initializing and updating git submodules..."
git submodule update --init --recursive
git submodule update --remote
echo "Installing composer dependencies..."
composer install --no-interaction --no-dev --optimize-autoloader
composer dump-autoload --optimize
# Your other deployment commands...
EOFGit submodules are not automatically included when you clone or pull a repository. The deployment process must explicitly:
- Initialize the submodule directories
- Fetch the submodule content
- Update the autoloader to recognize the new classes
Without these steps, you'll encounter errors like:
Error: Failed to instantiate component or class "rcg\mailchimp\MailchimpModule"
For other deployment systems (Forge, Ploi, etc.), ensure your deployment script includes:
git submodule update --init --recursive
git submodule update --remote
composer dump-autoload --optimizeOn your droplet, generate an SSH key for the ploi user:
sudo -u ploi ssh-keygen -t ed25519 -f /home/ploi/.ssh/id_ed25519 -N ""
Get the public key:
sudo cat /home/ploi/.ssh/id_ed25519.pub
In GitHub, go to your craft-mailchimp repository → Settings → Deploy keys → Add deploy key
- Paste the public key
- Give it a name like "ploi-servername"
- Check "Allow write access" if needed (probably not for a submodule)
Then run:
git submodule sync
git submodule update --init --recursive
If setting up manually on a server:
# Clone the repository
git clone --recurse-submodules https://github.com/your-username/your-project.git
# Or if already cloned without submodules
git submodule update --init --recursive
# Install dependencies
composer install --no-dev --optimize-autoloaderIntegration test examples are provided in tests/integration/. These demonstrate how to test your Mailchimp integration.
This module is licensed under the MIT License. See LICENSE.md for details.
For issues and feature requests, please use the GitHub issue tracker.
Developed by RCG for use in Craft CMS projects.