nztim/mailchimp

subscribe method can't update existing subscriber?

Closed this issue · 5 comments

The docs say that Mailchimp::subscribe(); will add a new subscriber or update an existing one. However when using it today like so:

    Mailchimp::subscribe(
        $this->listId,
        $contact->email,
        $mergeTags,
        false
    );

for an existing list subscriber, the API call threw an MailchimpBadRequestException with the message "Mailchimp API error (400): array (\n 'title' => 'Member Exists',\n 'status' => 400..."

Am I missing something? Thank you!

nztim commented

Hi Chris, I don't see any problem in the code but can you provide the rest of the error (without user data)? There may be an 'errors' property with more details.

You might also want to check for required merge fields and that merge field names match exactly.

After looking at it again it does seem like the subscriber may have been updated despite the error, but I can't confirm 100%.

I'm pasting this from a Slack messages so apologies for the formatting and truncation:

Mailchimp API error (400): array (
 'title' => 'Member Exists',
 'status' => 400,
 'detail' => 'email@example.com is already a list member. Use PUT to insert or update list members.',
 'instance' => '<redacted>',
)
Level
ERROR
Exception
```{
   "class": "NZTim\\Mailchimp\\Exception\\MailchimpBadRequestException",
   "message": "Mailchimp API error (400): array (\n  'title' => 'Member Exists',\n  'status' => 400,\n  'detail' => 'email@example.com is already a list member. Use PUT to insert or update list members.',\n  'instance' => '<redacted>',\n)",
   "code": 400,
   "file": "/var/www/myproject/vendor/nztim/mailchimp/src/MailchimpApi.php:106",
   "trace": [
       "/var/www/myproject/vendor/nztim/mailchimp/src/MailchimpApi.php:96",
       "/var/www/myproject/vendor/nztim/mailchimp/src/MailchimpApi.php:52",
       "/var/www/myproject/vendor/nztim/mailchimp/src/Mailchimp.php:62",
       "/var/www/myproject/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:338",
       "/var/www/myproject/app/Helpers/MailchimpHelper.php:50",
       "/var/www/myproject/app/Actions/Newsletter/CreateOrUpdateNewsletterSubscriberAction.php:15",
       "/var/www/myproject/vendor/spatie/laravel-queueable-action/src/ActionJob.php:92",
       "/var/www/myproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36",
       "/var/www/myproject/vendor/laravel/framework/src/Illuminate/Container/Util.php:41",
       "/var/www/myproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93",
       "/var/www/myproject/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37",
       "/var/www/myproject/vendor/laravel/framework/src/Illuminate/Container/Container.php:661",
       "/var/www/myproject/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:128",

Here's how the merge tags array is constructed:

        $mergeTags = [
            'FNAME' => $contact->first_name ?? '',
            'LNAME' => $contact->last_name ?? '',
            'ORGNAME' => $contact->org_name ?? '',
            'WWNID' => $contact->id,
        ];

Thank you,
Chris

nztim commented

Thanks Chris. I've had a look and found two reported causes for that error: making the subscriber hash from a mixed case email address, and the use of untrimmed email addresses.

I was able to reproduce the error by those means, and in both cases the real problem appears to be multiple subscriber hashes pointing to the same email address, hence the "Member Exists" error.

This package lowercases the email address but doesn't trim them, so my guess is that's the problem you are experiencing,

I will update the package to trim the email but you might like to check that this is in fact the issue.

nztim commented

I've made some changes to the package which include ensuring emails are trimmed. I'll run it myself for a while and then tag a release.

You are welcome to try it out but it may be easier to just add your own trim() in your subscribe command as that's the only important improvement.

nztim commented

I've tagged an update that trims email addresses. If you find that the problem in your case is not as described above then please let me know and I'll reopen.