Skip to content

Instantly share code, notes, and snippets.

@martinbean
Created July 2, 2025 08:37
Show Gist options
  • Save martinbean/be48e0ec37ba59a05e52b936a5721023 to your computer and use it in GitHub Desktop.
Save martinbean/be48e0ec37ba59a05e52b936a5721023 to your computer and use it in GitHub Desktop.
Mailchimp subscriber service in Laravel example
<?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);
}
}
<?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;
}
}
<?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