Changing email doesn't sync with Mailchimp

Created on 16 April 2019, over 5 years ago
Updated 24 July 2024, 5 months ago

Hi!
There was similar issue for 7.x version of module https://www.drupal.org/project/mailchimp/issues/2822362
Does anyone have solution for 8.x?
Thanks!

🐛 Bug report
Status

Needs work

Version

2.0

Component

Lists Module

Created by

🇺🇦Ukraine khiminrm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India sorabh.v6 Indore

    I think unsubscribing is not enough and we it delete/archive old entry as well.

  • 🇺🇸United States mrweiner

    That committed D7 patch references http://stackoverflow.com/questions/32224697/mailchimp-api-v3-0-change-su.... One of the solutions there mentions that you can update the contact email via API. Here is a basic way to do this for users specifically.

    function mymodule_user_presave(UserInterface $user): void {
      if (!$user->isNew()
        && $user->getEmail() !== $user->original->getEmail()) {
        $original_email = $user->original->getEmail();
        $new_email = $user->getEmail();
    
        $lists = mailchimp_get_lists();
        foreach ($lists as $list_id => $list) {
          $info = mailchimp_get_memberinfo($list_id, $original_email);
    
          if (empty((array) $info)) {
            continue;
          }
    
          $tokens = [
            'list_id' => $list_id,
            'subscriber_hash' => md5(strtolower($original_email)),
          ];
    
          $parameters = [
            'email_address' => $new_email,
          ];
    
          /* @var \Mailchimp\MailchimpLists $mc_lists */
          $mc_lists = mailchimp_get_api_object('MailchimpLists');
          $mc_lists->request('PUT', '/lists/{list_id}/members/{subscriber_hash}', $tokens, $parameters);
        }
      }
    }
    

    It loops over the lists looking for contacts matching the user's original ID. If one is found, it makes a PUT request to update that contact's email. Since this is done in hook_entity_presave(), it completes before functions like mailchimp_lists_process_subscribe_form_choices(), which will then update the contact instead of adding a new one.

    I feel like there must be a cleaner way to do this in the MC module proper, but it's been a long couple of weeks and I just need this working.

  • Status changed to RTBC 10 months ago
  • 🇬🇧United Kingdom matt b

    I've been running in production with this patch for some time now. Let's just get this committed please!

  • 🇺🇸United States bdimaggio Boston, MA

    Hear you @Matt B! We've got this review (and hopefully commit/release) on the schedule for this coming week.

  • 🇺🇸United States xenophyle

    I'm thinking this won't work when the email address is on a referenced entity. So you have a content type with the mailchimp subscription field and maybe a reference to a user. The subscription field uses the user's email field. If the user entity gets updated, this module won't notice and be able to unsubscribe the old email.

    Or does anyone have that working?

  • Status changed to Needs work 10 months ago
  • 🇺🇸United States xenophyle

    I've been working on this and hope to have a fix soon.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update 10 months ago
    17 pass
  • Open on Drupal.org →
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update 9 months ago
    Not currently mergeable.
  • Open on Drupal.org →
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update 8 months ago
    Not currently mergeable.
  • Open on Drupal.org →
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update 8 months ago
    Not currently mergeable.
  • 🇬🇧United Kingdom matt b

    Any progress on this please?

  • 🇺🇸United States xenophyle

    @Matt B The scope has crept to include other merge data besides email, which has drawn this out into a major project. There is an MR in progress that needs testing and review.

  • 🇬🇧United Kingdom matt b

    Thanks for the update. I no longer use mailchimp due to poor service provision so migrated away last week.

Production build 0.71.5 2024