I'm using the Spatie Newsletter package with the Mailchimp driver to handle newsletter subscriptions in my Laravel application. However, when I call the subscribe() method, the subscriber is not being added to the corresponding list on Mailchimp, and I'm not getting any error messages.
Here's the code I'm using:
use Spatie\Newsletter\Facades\Newsletter;
//
public function Subscribe(Request $request)
{
try {
$subscription = Newsletter::subscribe("joh.doe@gmail.com", ['FNAME'=>'Rince', 'LNAME'=>'Wind']);
return response()->json([
'message' => 'Newsletter subscription updated successfully',
'data' => $user,
$subscription
]);
} catch (\Exception $e) {
Log::error('Error subscribing to newsletter: ' . $e->getMessage());
return response()->json([
'message' => 'An error occurred while subscribing to the newsletter',
]);
}
/*$user->is_newsletter_subscribed = true;
$user->newsletter_subscription_updated_at = now();
$user->save();*/
}
And this is my config file newsletter.php
<?php
return [
/*
* The driver to use to interact with MailChimp API.
* You may use "log" or "null" to prevent calling the
* API directly from your environment.
*/
'driver' => env('NEWSLETTER_DRIVER', Spatie\Newsletter\Drivers\MailChimpDriver::class),
/**
* These arguments will be given to the driver.
*/
'driver_arguments' => [
'api_key' => env('MAILCHIMP_APIKEY'),
'endpoint' => env('MAILCHIMP_LIST_ID'),
],
/*
* The list name to use when no list name is specified in a method.
*/
/*
* This key is used to identify this list. It can be used
* as the listName parameter provided in the various methods.
*
* You can set it to any string you want, and you can add
* as many lists as you want.
*/
'lists' => [
'subscribers' => [
/*
* When using the Mail coach driver, this should be Email list UUID
* which is displayed in the Mail coach UI
*
* When using the MailChimp driver, this should be a MailChimp list id.
* http://kb.mailchimp.com/lists/managing-subscribers/find-your-list-id.
*/
'id' => env('MAILCHIMP_LIST_ID'),
],
],
/*
* Whether to use SSL when connecting to the MailChimp API.
* Set to false to disable SSL.
*/
'ssl' => false,
];
No exception is thrown, hoewever the function return false, and the audience list in mailchimp is not updated.
I've checked that the Mailchimp API key is correct and that the list ID is also correct. I've also tried using the subscribeOrUpdate() method instead, but it didn't work either.
What could be causing this issue, and how can I debug it? Any help would be appreciated.