Created
July 2, 2025 08:37
-
-
Save martinbean/be48e0ec37ba59a05e52b936a5721023 to your computer and use it in GitHub Desktop.
Mailchimp subscriber service in Laravel example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Contracts\SubscriberService; | |
use App\Services\Mailchimp\MailchimpSubscriberService; | |
use MailchimpMarketing\ApiClient; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
public function register(): void | |
{ | |
$this->app->singleton(ApiClient::class, function (): ApiClient { | |
$client = new ApiClient(); | |
return $client->setConfig([ | |
'apiKey' => $this->app->make('config')->get('services.mailchimp.api_key'), | |
'server' => $this->app->make('config')->get('services.mailchimp.server_prefix'), | |
]); | |
}); | |
$this->app->singleton(SubscriberService::class, MailchimpSubscriberService::class); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\Mailchimp; | |
use App\Contracts\SubscriberService; | |
class MailchimpSubscriberService implements SubscriberService | |
{ | |
protected ApiClient $client; | |
public function __construct(ApiClient $client) | |
{ | |
$this->client = $client; | |
} | |
public function subscribe(string $listId, string $email): bool | |
{ | |
$hash = md5(strtolower($email)); | |
$this->client->lists->setListMember($listId, $hash, [ | |
'email_address' => $email, | |
'status_if_new' => 'pending', | |
]); | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Contracts; | |
interface SubscriberService | |
{ | |
public function subscribe(string $listId, string $email): bool; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment