Search code examples
phpmailchimpmailchimp-api-v3.0

Mailchimp API (PHP) - Your Campaign is not ready to send


I am having a problem usign the Mailchimp API with PHP.

When I'm running the code, this is the error I get:

{
    "type": "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
    "title": "Bad Request",
    "status": 400,
    "detail": "Your Campaign is not ready to send.",
    "instance": "46235d29-6a67-4d55-800d-a95e2dc7273f"
}

I know why the campaign is not ready to send, as you can see in this image, the details are filled, but not saved:

Here is the code I'm trying to run:

<?php
require '../vendor/autoload.php';
$apiKey = 'API ';

$mailchimp = new \MailchimpAPI\Mailchimp($apiKey);

$list_created = $mailchimp->lists()->post([
  'name' => 'Lista Monday',
  'contact' => [
    'company' => 'Test Company Monday',
    'address1' => 'Monday address 1',
    'city' => 'Monday City',
    'state' => 'Monday State',
    'zip' => '227560',
    'country' => 'Romania'
  ],
  'permission_reminder' => 'test',
  'campaign_defaults' => [
    'from_name' => 'Anjus Parsay',
    'from_email' => 'FROM EMAIL',
    'subject' => 'Monday Email',
    'language' => 'English'
  ],
  'email_type_option' => false
]);

$list_id = json_decode($list_created->getBody(), true)['id'];

$email = "EMAIL ";

$addedUser = $mailchimp->lists($list_id)->members()->post([
    'email_address' => $email,
    'status' => 'subscribed'
]);

//1. Create the template
$template_content = file_get_contents("http://widevisiondesign.com/anjus/mailchimp/src/testTemplate.html");

$template = $mailchimp->templates()->post([
'name' => 'testTemplateHTML',
'html' => $template_content
]);

$template_id = json_decode($template->getBody(), true)['id'];

//2. Create a campaign
$campaign = $mailchimp->campaigns()->post([
'type' => 'regular',
'recipients' => [
'list_id' => $list_id
],
'settings' => [
'title' => 'Monday',
'subject_line' => 'Monday - Sunday',
'from_name' => 'NAME NAME',
'to_name' => 'test',
'preview_text' => 'Preview text - Monday',
'template_id' => $template_id
]
]);

$campaign_id = json_decode($campaign->getBody(), true)['id'];

$sent = $mailchimp->campaigns($campaign_id)->send([$email], 'plaintext')->getBody();

print_r($sent);

?>

Thank you!


Solution

  • From the code and screenshot you presented, here are some things that came to my mind:

    1. Have you verified and authenticated your email address? If no, you need to authenticate the domain of your email address. Go to your Mailchimp dashboard, click Account > Settings > Domains > Verify a Domain.
    2. Then add your domain, follow the instruction to verify and authenticate.
    3. If you prefer, you can make an API call to get a checklist before actually sending the email. The checklist gives a success and error notes on what needs to be done before actually sending the email.
    4. I'm sorry if this seems somehow but did you really provide your email address in place of the 'FROM EMAIL' placeholder? From the code, it seems that you set the name of the from_name but did not set the from_email.