Integrating Mailchimp Subscription with Laravel

The goal is, with each registration, website will add the email to the Mailchimp list, so the marketing guys can make, change and play with a welcome mails or create additional campaigns.

There is an official MailChimp client for PHP. Unfortunately this package is working with old Mailchimp API version 2.0, and current one is 3 (library version is 2.0.6).

I assume you already have a running project, so I will go through with how to:

  1. prepare your project and trigger the event
  2. add the library
  3. call the MailChimp API endpoint.

Preparing

Make the event and listener classes. Event UserRegistered will be the event triggered when (obviously) user is registered, and SubscribeToMailchimp listener will interact with Mailchimp services.

Open app/Providers/EventServiceProvider.php and add

protected $listen = [
    'App\Events\UserRegistered' => [
        'App\Listeners\SubscribeToMailchimp',
    ],
];

Type in console php artisan event:generate to generate those classes.

In the UserRegistered event we will store the currently registered user.

<?php

namespace App\Events;

use App\User;
use Illuminate\Queue\SerializesModels;

class UserRegistered extends Event
{
    use SerializesModels;

    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }
}

Listener will accept this event as a parameter.

Only thing left is to trigger the event at the right moment. If you are using default auth generator, open app/Http/Controllers/Auth/AuthController.php and change the method create.

// use App\Events\UserRegistered;

$user = User::create([
    'name' => $data['name'],
    'email' => $data['email'],
    'password' => bcrypt($data['password']),
]);

event(new UserRegistered($user));

return $user; 

Install & configure the the package.

composer require mailchimp/mailchimp 

You need to get API key

By looking into the code of the Mailchimp class, you can see that passing api key can be done through environment MAILCHIMP_APIKEY.

Put that in my .env file.

MAILCHIMP_APIKEY=<api_key>

Now find the list id and add it again in the .env as a MAILCHIMP_LIST_ID.

Mailchimp List ID

MAILCHIMP_LIST_ID=65a7e21335

Using Mailchimp client library

Listener app/Listeners/SubscribeToMailchimp.php will reacted on the event. Mailchimp class instance will be automatically injected in to the constructor by the Framework.

In the documentation there is an instruction how to subscribe user to the particular list.

private $mailchimp;

public function __construct(\Mailchimp $mailchimp)
{
  $this->mailchimp = $mailchimp;
}

public function handle(UserRegistered $event)
{

  $this->mailchimp->lists->subscribe(
      env('MAILCHIMP_LIST_ID'),
      ['email' => $event->user->email],
      null,
      null,
      false
  );

}

Fifth parameter in the subscribe method is double opt in. It is saying to Mailchimp to ask user for additional confirmation before subscribing to the list. Turn this off. It is not good to ask someone that is just registered "Wold you like to subscribe".

Third parameter can be useful, sending additional, merge, parameters (usually FNAME and LNAME). Check this link.

After that you will have the users email in your list.

New Subscriber

Author

I plan to write more articles about common laravel components. If you are interested let’s stay in touch.
comments powered by Disqus