'Resubscribe a user to a MailChimp list after unsubscribe

My site allows users to subscribe to MailChimp lists using the API via Drupal MailChimp module. But if a user unsubscribes by following the link in the email, and subsequently decides to re-subscribe by visiting my website and checking the "subscribe" box, MailChimp responds with

[email protected] is in a compliance state due to unsubscribe, bounce, or compliance review and cannot be subscribed.

What is the solution assuming the user really wants to re-subscribe?



Solution 1:[1]

Set the member's status to pending. That should resend the opt-in email.

EDIT:

Apparently this does not work anymore if the address was not unsubscribed through the API.

Also, don't be a jerk and downvote what was obviously previously a useful answer just because the API changed. Please inform the person who took the time to help you that the API has changed using a comment, not a downvote.

Solution 2:[2]

If we need to resubscribe an email which has been unsubscribed,

We need to make a put call with either of the following options:

  1. {"status" : "subscribed"} Will resubscribe the email
  2. {"status" : "pending"} Will send a confirmation email for resubscription.

The API endpoint must consist of md5 hash like(/lists/list_id/members/md5hash)

Solution 3:[3]

First get the status from response, if it is unsubscribed then update the list.This will do the trick ;)

const mailchimp = require("@mailchimp/mailchimp_marketing");
const md5 = require("md5");

router.post("/newsletter-subscribe", asyncWrapper(async (req, res) => {

mailchimp.setConfig({
apiKey: "e4ef******62c481-us17",
server: "us17",
});

const subscriber_hash = md5(email.toLowerCase());
const list_id = '44b****47';
let response = await mailchimp.lists.setListMember(
  list_id,
  subscriber_hash,
  {
    email_address: email,
    status_if_new: 'subscribed',
  }
);
if(response.status=='unsubscribed'){
    response = await mailchimp.lists.updateListMember(
        list_id,
        subscriber_hash,
        {status: 'subscribed'}
      );
}
 return res.json({'subscribed': response.status});


}));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 TheViralGriffin
Solution 3 mlhazan