Sipan
Sipan

Reputation: 103

mailchimp api delete user from list

when user unsubscribes from list he can't subscribe again with the same email address. How can allow user to resubscribe to list?

 $unsubscribe = $mailChimp->call('lists/unsubscribe',array(
                    'id'        => $list_id,
                    'email'    =>  array('email' => $email),
                    true, 
                    true
                ));

Upvotes: 2

Views: 6608

Answers (3)

blissweb
blissweb

Reputation: 3855

You can do it by updating the user to a status of "pending"

Unfortunately my code is the Python api, but you should be able get the idea.

def mcResendSubscriptionSignup(self,email,audienceId):

    # calculate the hash of the email address
    emailHash = hashlib.md5(email.lower().encode()).hexdigest()

    # get existing user, and all their data
    data = self.mcClient.lists.members.get(list_id=audienceId, subscriber_hash=emailHash)

    # set the user status to pending to resend subscription email
    data['status'] = 'pending'

    # update the data back to the user record in the audience
    data = self.mcClient.lists.members.update(list_id=audienceId, subscriber_hash=emailHash, data=data)

    print(f'Sent a resubscription email to {email}')

This function will resend a confirmation email to the user which he has to click on to resubscribe. Note, you need to also find your audienceId.

This is the only way that Mailchimp will allow you to re-add the user to an audience as of 2020, after an unsubscribe.

Yes, this is a pain when testing, which is why I worked on this function. Tiny bit better than doing it in the GUI interface.

Upvotes: 0

Adam
Adam

Reputation: 29009

If you want to resubscribe the user later, you need to delete him, see the documentation:

When you need to remove a few subscribers, decide whether you want to delete them yourself or unsubscribe them. Deleted subscribers can be added back to your list, so if you need to make sure a subscriber isn't accidentally re-added, unsubscribe them instead.

If you are using the latest mailchimp-api then you can delete the user as follows:

include 'Mailchimp.php';
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('your**api***key');


function deleteUser($email){
    global $MailChimp;
    //your list_id from Mailchimp
    $list_id = 'your***list**id';

    $subscriber_hash = $MailChimp->subscriberHash($email);
    $MailChimp->delete("lists/$list_id/members/$subscriber_hash"); 
}

If no user with that email exists, then $MailChimp->delete() will return an array like this:

Array ( [type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/ [title] => Resource Not Found [status] => 404 [detail] => The requested resource could not be found. [instance] => ) 

If the user was found, then the method will not return anything. Note that this does not imply that the user has been deleted, because if the user has been unsubscribed previously, then its not possible to delete him.

If you do not want to use the api, then you can also write your own custom curl command using the delete verb.

Upvotes: 3

Try deleting the email from list and re-subscribe the same. You can delete the member by setting delete_member property to true in unsubscribe method

Upvotes: 0

Related Questions