Good morning all of you,
I’m currently working on using spatie/newsletter in my laravel 5.6 Blog to work with MailChimp to create a newsletter where users can subscribe.
When I try to subscribe, I get this response:
"message": "Invalid MailChimp API key `` supplied.", "exception": "Exception", "file": "C:\...\vendor\drewm\mailchimp-api\src\MailChimp.php", "line": 49,
My Controller
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use Newsletter; class SubscribeController { public function subscribe(Request $request) { if (!filter_var($request->email, FILTER_VALIDATE_EMAIL)) { return "Die eingegebene Email ist nicht gültig."; } else { if (Newsletter::isSubscribed($request->email)) { return "Du bist bereits mit dieser Mail in meiner Mailingliste eingetragen."; } else { if (!(Newsletter::subscribeOrUpdate($request->email))) { return "Leider gab es ein Problem, bitte versuche es später noch einmal."; } else { return "Ihre Email " . $request->email . " wurde zu meiner Mailingliste hinzugefügt."; } } } } }
Env File
MAILCHIMP_APIKEY=123456-us19 MAILCHIMP_LIST_ID=1234567
newsletter.php (after running php artisan vendor:publish -provider=”SpatieNewsletterNewsletterServiceProvider”)
<?php return [ 'apiKey' => env('MAILCHIMP_APIKEY'), 'defaultListName' => 'nameOfListInMailChimp', 'lists' => [ 'subscribers' => [ 'id' => env('MAILCHIMP_LIST_ID'), ], ], 'ssl' => true, ];
I have also added the
SpatieNewsletterNewsletterServiceProvider::class,
into the app/config
providers aswell as
'Newsletter' => SpatieNewsletterNewsletterServiceProvider::class,
to the aliases in the app/config
.
I really don’t know why it is not working at this point.
I tried to give all necessary information. Api Key is valid and Status is ok. List Id is valid aswell.
Thank you in advance! Domi
Advertisement
Answer
I found the solution myself, it took me a while to figure it out.
First: In the newsletter.php
class, when I configured my lists, I forgot to change the name of the list from 'subsribers'
to 'nameOfListInMailChimp'
.
Second thing I needed to do was:
php artisan config:cache
Because every time you make a change in the .env file, you have to clear the cache to make the changes work.
I hope this is helpful for other people, this basically could be used as a complete How- to-configure Spatie Newsletter 😉