Laravel: How to Add New Registered User to Mailchimp List
Founder of QuickAdminPanel
Quite often after registration of new user we want to add them to our newsletter list. This article will show you how to do it with Mailchimp.
For this example, we will use QuickAdminPanel-generated registration form, but you can do absolutely same thing with default Laravel Auth form.
Step 1. Add checkbox to the form.
First, let’s be ethical and not break the law – we need to ask user if they agree to be added to the list.
So in default Laravel register form we add this checkbox, default unchecked.
Here’s register.blade.php file code:
<form method="POST" action="{{ route('register') }}"> {{ csrf_field() }} {{-- ... more fields ... --}} <div class="input-group mb-4"> <div class="input-group-prepend"> <span class="input-group-text"> <i class="fa fa-lock fa-fw"></i> </span> </div> <input type="password" name="password_confirmation" class="form-control" required placeholder="{{ trans('global.login_password_confirmation') }}"> </div> <div class="input-group mb-4"> <input type="checkbox" name="subscribed" value="1" /> Subscribe to the newsletter </div> <button class="btn btn-block btn-primary"> {{ trans('global.register') }} </button> </form>
Step 2. Save the choice into the database
Let’s save that information in the database, so new field is needed.
php artisan make:migration add_subscribed_to_users_table
Here’s the code of migration file:
public function up() { Schema::table('users', function (Blueprint $table) { $table->boolean('subscribed')->default(false); }); }
Next, add that to fillables array of app/User.php model:
class User extends Authenticatable { protected $fillable = [ 'name', 'email', 'password', 'created_at', 'updated_at', 'deleted_at', 'remember_token', 'email_verified_at', 'subscribed', ];
Finally, we need to change default create() method for app/Http/Controllers/Auth/RegisterController.php, with one change – adding the subscribed value.
class RegisterController extends Controller { // ... other methods protected function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), 'subscribed' => $data['subscribed'] ?? false, ]); } }
For more “tricks” about Laravel registration customization, see this article – 8 Things You Can Customize in Laravel Registration.
Step 3. Adding to Mailchimp list
This step will be different for every email provider, so you are using something different than Mailchimp then read API docs for your provider.
First, we need to get two variables from Mailchimp:
– API Key
– Audience ID (they used to be called “Lists”, now renamed to “Audiences”)
Here’s how you get your API Key – in “Account” section:
And here’s how you get Audience ID, after you create an audience:
Now, we will install a package called Laravel Newsletter by Spatie, that will help us to add the subscriber:
composer require spatie/laravel-newsletter
Next, we follow installation instruction of the package, and publish the configuration:
php artisan vendor:publish --provider="Spatie\Newsletter\NewsletterServiceProvider"
Final step of the configuration – go to our .env file and add the values of API Key and Audience ID.
MAILCHIMP_APIKEY=8ec0cde9f1acc8cxxxxxxxxxxxxxx MAILCHIMP_LIST_ID=fc4aafxxxxx
I also advise to add these variables with empty values to .env.example file – see this article: How to add new .env variable, so teammates would notice?
Finally, we will edit the same create() method of RegisterController, here’s it’s final version:
use Newsletter; class RegisterController extends Controller { // ... other methods protected function create(array $data) { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), 'subscribed' => $data['subscribed'] ?? false, ]); if ($user->subscribed) { Newsletter::subscribe($user->email); } return $user; } }
As you can see, we still need to return User object, but in the meantime we can perform some operations with it.
And that’s it! Now you can send email updates about your tool to users who subscribed.