Mailchimp send mail api
Mailchimp's Send Mail API allows you to send emails programmatically using their API. Here's an overview of the API and how to use it:
API Endpoints
The Send Mail API has two main endpoints:
POST /campaigns/send
: Sends a campaign to a list of subscribers.POST /campaigns/send-test
: Sends a test campaign to a list of subscribers.
Request Body
The request body for both endpoints is similar and contains the following properties:
campaign_id
: The ID of the campaign you want to send.recipient_list_id
: The ID of the list you want to send to.recipient_list
: An array of recipient email addresses.merge_vars
: An object containing merge variables to replace in the email template.template_id
: The ID of the email template to use.subject
: The subject line of the email.from_name
: The name of the sender.from_email
: The email address of the sender.reply_to
: The email address to reply to.headers
: An object containing custom headers to add to the email.
Example Request
Here's an example request to send a campaign using the Send Mail API:
{
"campaign_id": "123456",
"recipient_list_id": "789012",
"recipient_list": [
{"email": "[email protected]"},
{"email": "[email protected]"}
],
"merge_vars": {
"FNAME": "John",
"LNAME": "Doe"
},
"template_id": "123456",
"subject": "Hello from Mailchimp!",
"from_name": "John Doe",
"from_email": "[email protected]",
"reply_to": "[email protected]",
"headers": {
"X-Mailchimp-Campaign": "Hello from Mailchimp!"
}
}
Response
The response from the API will contain the following properties:
campaign_id
: The ID of the campaign that was sent.recipient_count
: The number of recipients who received the email.sent_count
: The number of emails that were successfully sent.failed_count
: The number of emails that failed to send.errors
: An array of error objects containing information about failed emails.
Error Handling
The Send Mail API returns a 200 OK response even if some emails fail to send. To handle errors, you can check the failed_count
property in the response and loop through the errors
array to get more information about the failed emails.
Authentication
To use the Send Mail API, you need to authenticate with Mailchimp using an API key or an access token. You can obtain an API key by creating a Mailchimp account and going to the "Account" > "API keys" page. You can also obtain an access token by creating a Mailchimp account and going to the "Account" > "API keys" page, then clicking on the "Create access token" button.
Here's an example of how to authenticate with the Send Mail API using an API key:
curl -X POST \
https://us12.api.mailchimp.com/3.0/campaigns/send \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"campaign_id": "123456",
"recipient_list_id": "789012",
"recipient_list": [
{"email": "[email protected]"},
{"email": "[email protected]"}
],
"merge_vars": {
"FNAME": "John",
"LNAME": "Doe"
},
"template_id": "123456",
"subject": "Hello from Mailchimp!",
"from_name": "John Doe",
"from_email": "[email protected]",
"reply_to": "[email protected]",
"headers": {
"X-Mailchimp-Campaign": "Hello from Mailchimp!"
}
}'
Note that you should replace YOUR_API_KEY
with your actual Mailchimp API key.