Changing email doesn't sync with Mailchimp

Created on 16 April 2019, about 6 years ago
Updated 3 February 2023, over 2 years 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 over 1 year 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 over 1 year 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 over 1 year ago
    17 pass
  • Open on Drupal.org →
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    Not currently mergeable.
  • Open on Drupal.org →
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update about 1 year ago
    Not currently mergeable.
  • Open on Drupal.org →
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update about 1 year 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.

  • 🇦🇺Australia HappyPanda

    Is there any chance we could get a fix for this?

    Even if it only addresses the most common use case (users have email fields, if the email field is updated currently mailchimp ends up with two users for that UUID - one with the old email, one with the new).

    I understand from the comments above that there are some very awkward cases possible, but I'm hoping you could apply the simple fix, which would probably help 80% of installs who have the problem and then proceed to the complex use case in the future.

  • 🇺🇸United States mrweiner

    @happypand If you or anybody needs to address this in their site without worrying about this merge request, I recommend looking at my earlier comment #6. In isolation, in the context of your own site, this is likely an easy fix.

  • 🇦🇺Australia HappyPanda

    Cheers @mrweiner appreciate the suggestion.

    Looks to work for me - had to tweak it slightly to:

    use Drupal\user\Entity\User;

    function hp_mailchimp_api_patch_user_presave(User $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);
    }
    }
    }

    But looks good so far!

Production build 0.71.5 2024