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:

  1. POST /campaigns/send: Sends a campaign to a list of subscribers.
  2. 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:

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:

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.