How to handle email error already exists in mailchimp api code php

I have this code:

    App::import('Vendor', 'mailchimp', array('file' => 'mailchimp/Mailchimp.php'));
    $key = Configure::read("MAILCHIMP_KEY");
    $list_id = 'xxxxxxxx';
    $email=array('email' => $email_id);
    $merge_vars = array(
        'FNAME' => $fname, 
        'LNAME' => $lname,             
        'groupings' => 
            array(
                'name' => array('TestGroupTitle'),
                'groups' => array('TestGroup')
            )
        );

    $double_optin='false';
    $mailchimp = new Mailchimp($key);

    // sample to take help
    // subscribe(string apikey, string id, struct email, struct merge_vars, string email_type, bool double_optin, bool update_existing, bool replace_interests, bool send_welcome)
    $result = $mailchimp->lists->subscribe($list_id, $email, $merge_vars, 'html', $double_optin, false, true, false);

When I first added my email id when it worked correctly, but if the same email address is added again, it shows this error: xxxx@xx.com is already subscribed to the XXX Newsletter list. Click here to update your profile.

I am not repeating anything, but still this message is coming.

I also know what error I get after some debugging:

array(
'status' => 'error',
'code' => (int) 214,
'name' => 'List_AlreadySubscribed',
'error' => 'xxxx is already subscribed to list xxxx Newsletter. Click here to update your profile.'
)

I also tried this, but nothing happened.

  if ($mailchimp->errorCode) {
        $error['Code'] = $this->_mailChimp->errorCode;
        $error['Message'] = $this->_mailChimp->errorMessage;
        pr($error);
        pr($error['Code']);
    } 

I am using PHP and Mailchimp api 2.0. Any help would be greatly appreciated. I spent my full day on this. :(

+4
source share
3 answers

Hisham, noob, , googled , , Hisham . , -

// Your Mailchimp API Key 
$apikey = 'xxxxxxxxxxxxxx'; 
// List ID
$list_id = 'xxxxxxxxxx';
// Initializing the $MailChimp object
$mc = new Mailchimp($apikey);

    try {
            //MAILCHIMP API call subscribe() ---- email only 
            $mc->lists->subscribe($list_id,  array(
                "email"  => $email
            ));

    } catch (Exception $e) {

        echo 'Caught exception: ',  $e->getMessage(), "\n";

    }
+3

, . Mailchimp , 2.0 catch try .

+2

You tried to use:

'update_existing' => true

In your options?

+1
source

All Articles