Hisham Muneer
Hisham Muneer

Reputation: 8742

how to handle email already exists error 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 added my email id first time it worked correctly but if the same email is added again, it shows this error : [email protected] is already subscribed to list XXX Newsletter. Click here to update your profile.

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

Also I know what error i am getting 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 have tried this also 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 highly appreciated. I have wasted my complete day in this. :(

Upvotes: 4

Views: 7026

Answers (3)

Marcello Kad
Marcello Kad

Reputation: 198

Have you tried using:

'update_existing' => true

In your parameters?

Upvotes: 1

shensw
shensw

Reputation: 233

I agree with Hisham, as for a noob like me, googled many times and still can't find the actual helpful solution until I see Hisham's own answer hints me. Here's a more detail example in case someone needs it in the future

// 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";

    }

Upvotes: 3

Hisham Muneer
Hisham Muneer

Reputation: 8742

Found the answer and wanted to share with the guys facing the same problem. In previous Mailchimp versions it was easy to handle but from 2.0 you need to use try catch block for this.

Upvotes: 2

Related Questions