简体   繁体   中英

How to send newly created campaign with mailchimp api v2?

I am using mailchimp api v2. I am using the mailchimp recommended full api v2 php wrapper. I am able to create a campaign, but not sure how to send it. With the send method, It wants the campaign id, but I am letting mailchimp create the campaign id when creating the campaign.

My create campaign code looks like this:

$api_key = "my_api_key";
require('Mailchimp.php');

//Create Campaign
$Mailchimp = new Mailchimp($api_key);
$result = $Mailchimp->campaigns->create('regular', 
        array('list_id'               => 'my_list_id',
    'subject'                       => 'This is a test subject',
    'from_email'                        => 'test@test.com',
    'from_name'                         => 'From Name'),
    array('html'                    => '<div>test html email</div>',
    'text'                                  => 'This is plain text.')
);

if( $result === false ) {
    // response wasn't even json
    echo 'didnt work';
}
else if( isset($result->status) && $result->status == 'error' ) {
    echo 'Error info: '.$result->status.', '.$result->code.', '.$result->name.', '.$result->error;
} else {
        echo 'worked';
}

This seems to be working for me.

$Mailchimp = new Mailchimp($api_key);
$result = $Mailchimp->campaigns->create('regular', 
        array('list_id'               => 'my_list_id',
    'subject'                           => 'This is a test subjects',
    'from_email'                        => 'test@test.com',
    'from_name'                         => 'From_Name'),
    array('html'                    => '<div>test html email</div>',
    'text'                                  => 'This is plain text.')
);


if( $result === false ) {
    // response wasn't even json
    echo 'sorry';
}
else if( isset($result->status) && $result->status == 'error' ) {
    echo 'Error info: '.$result->status.', '.$result->code.', '.$result->name.', '.$result->error;
} else {
echo 'worked';
$mySend = $Mailchimp->campaigns->send($result['id']);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM