Laravel + Mailchimp API: Send Email Code Examples in 2023

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

In this article, we will discuss what Mailchimp is and what it is used for. You will also learn how to integrate MailChimp API into your Laravel. We will explain all the steps required for integrating Mailchimp API in Laravel.

In this article, we will discuss the following sections.

Table of Contents

  1. What is Mailchimp?
  2. What is Mailchimp used for?
  3. Send an Email in Laravel through Mailchimp API. 
  4. Sending Emails in Laravel through Mailchimp API Explored.

What is Mailchimp?

Mailchimp is an email marketing cloud software provider. They are one of the oldest & largest email services around. Ben Chestnut and Mark Armstrong founded the Mailchimp email service in 2001.

Mailchimp is one of the world’s most popular email marketing solutions, with more than 14 million users, and they deliver 1 billion emails daily. While most of Mailchimp’s customers are small businesses, some medium-sized businesses use its Premium Plan.

Best for an all-in-one integrated email delivery branding solution. Mailchimp is a powerful marketing API service that provides you with intelligent marketing tools. It works with your favorite apps and web services and helps you do more with your email marketing. It can also find the tools you use or discover new ways for email marketing.

MailChimp allows you to create and send emails and offers other features since it is a more comprehensive email marketing tool. Some examples are the ability to manage and import contact lists, send emails for abandoned carts, use a drag & drop editor, and schedule emails.

With MailChimp email service, you will be in good company. When you build integrations with Mailchimp, you create tools for a trusted and reliable platform that powers your marketing, e-commerce, and more for millions of small businesses.

Mailchimp’s API client libraries and mobile SDKs make it so easy to work with Mailchimp Marketing and Transactional APIs with your language or platform of choice.

Some of the key features of MailChimp email service:

  • Drag and Drop Custom Email Builder
  • Email Automation
  • Store content like images to use later in the campaign.
  • Send transactional emails
  • Real-time analytics

What is Mailchimp used for?

MailChimp provides features like managing subscribers, sending emails using campaigns, tracking email results, etc. Using MailChimp, you can also track how many subscribers open the email and read. Mailchimp is used for getting leads into your email database and then sending a mass message to many people at the same time via email and social media. 

Mailchimp offers its solutions to customers according to the principle of Software as a Service (Saas). Customers need no additional software on their computer or server but can use a browser-based user interface to access the tool.

How to Send an Email in Laravel through Mailchimp API

You can send emails in Laravel via Mailchimp API by the following steps.

Step 1: Get Mailchimp API key.

Click here to create a Mailchimp account. If you have an account, follow these steps to get API credentials from your account.

  1. First, log in to your Mailchimp account. 
  2. Go to the profile dropdown.
  3. Click on account and go to the extras tab.
  4. Click on API keys and get your API key.
laravel mailchimp
api key

Step 2: Get Audience ID

You will also need to get an Audience ID for mail configuration. To get this, follow these steps.

  1. Go to the Audience menu.
  2. Click to Manage Audience and then settings.
  3. Click Audience name and defaults. 
  4. You will get an audience ID.
laravel mailchimp
settings
laravel mailchimp

Step 3: Install the Mailchimp Composer Package.

After getting the Mailchimp API key and an Audience ID from your account, you now need to install the Mailchimp library. 

In this step, we will install the skovmand/mailchimp-laravel package to use the MailChimp API methods. Run the following composer command to install the skovmand/mailchimp-laravel package.

composer require skovmand/mailchimp-laravel

Now we need to add the provider path and alias path in the config/app.php file, so open the file and add the below code.

config/app.php

return [
	......
	$provides => [
		......
		......,
		Skovmand\Mailchimp\MailchimpServiceProvider::class,
	],
	.....
]

You also need to add Mailchimp credentials in the .env file.

MAILCHIMP_APIKEY=XXXXXXXXXXXXXXX
MAILCHIMP_LIST_ID=XXXXXXXXXXXXXX

Step 4: Add Laravel Route to test the integration.

In this step, we add three new routes for your better understanding. One for layout, the second one for subscription, and the last one for send campaign. So first, add the below routes in your routes.php file.

app/Http/routes.php

Route::post('send', 'MailChimpController@sendCompaign');

Step 5: Add a Laravel Controller for the route.

In this step, we will add a MailChimpController file for our example. You can copy the code and paste it.

app/Http/Controllers/MailChimpController.php

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Auth;
use Config;


class MailChimpController extends Controller
{


    public $mailchimp;

    // a listID coming from Mailchimp
    public $listId = 'test';


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




    public function send(Request $request)
    {
        $this->validate($request, [
            'subject' => 'required',
            'to_email' => 'required',
            'from_email' => 'required',
            'message' => 'required',
        ]);


        try {
            $options = [
            'list_id'   => $this->listId,
            'subject' => $request->input('subject'),            'from_name' => $request->input('from_email'),
            'from_email' => 'hardik@itsolutionstuff.com',
            'to_name' => $request->input('to_email')
            ];


            $content = [
            'html' => $request->input('message'),
            'text' => strip_tags($request->input('message'))
            ];


            $campaign = $this->mailchimp->campaigns->create('regular', $options, $content);
            $this->mailchimp->campaigns->send($campaign['id']);


            return redirect()->back()->with('success','send campaign successfully');
           
        } catch (Exception $e) {
            return redirect()->back()->with('error','Error from MailChimp');
        }
    }

}

Step 6: Add Blade File.

In the step, you just need to create a new blade file mailchimp.blade.php, and put the below code on that file.

@extends('layouts.app')

@section('content')
<h2 class="text-center">MailChimp API Example</h2>
<div class="container">


@if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
    <button type="button" class="close" data-dismiss="alert">×</button>
        <strong>{{ $message }}</strong>
</div>
@endif


@if ($message = Session::get('error'))
<div class="alert alert-danger alert-block">
    <button type="button" class="close" data-dismiss="alert">×</button>
        <strong>{{ $message }}</strong>
</div>
@endif


    <div class="row">
        <div class="col-md-5">
            <div class="well">
                 {!! Form::open(array('route' => 'subscribe')) !!}
                  <div>
                    <h3 class="text-center">Subscribe Your Email</h3>
                     <input class="form-control" name="email" id="email" type="email" placeholder="Your Email" required>
                     <br/>
                     <div class="text-center">
                        <button class="btn btn-info btn-lg" type="submit">Subscribe</button>
                     </div>
                  </div>
                 {!! Form::close() !!}
             </div>
        </div>
        <div class="col-md-7">
            <div class="well well-sm">
          {!! Form::open(array('route' => 'send','class'=>'form-horizontal')) !!}
          <fieldset>
            <legend class="text-center">Send</legend>

   
            <!-- Name input-->
            <div class="form-group">
              <label class="col-md-3 control-label" for="name">Subject</label>
              <div class="col-md-9">
                <input id="name" name="subject" type="text" placeholder="Your Subject" class="form-control">
              </div>
            </div>
   
            <!-- Email input-->
            <div class="form-group">
              <label class="col-md-3 control-label" for="email">To</label>
              <div class="col-md-9">
                <input id="email" name="to_email" type="text" placeholder="To " class="form-control">
              </div>
            </div>


            <!-- From Email input-->
            <div class="form-group">
              <label class="col-md-3 control-label" for="email">From</label>
              <div class="col-md-9">
                <input id="email" name="from_email" type="text" placeholder="From " class="form-control">
              </div>
            </div>

   
            <!-- Message body -->
            <div class="form-group">
              <label class="col-md-3 control-label" for="message">Your message</label>
              <div class="col-md-9">
                <textarea class="form-control" id="message" name="message" placeholder="Please enter your message here..." rows="5"></textarea>
              </div>
            </div>

   
            <!-- Form actions -->
            <div class="form-group">
              <div class="col-md-12 text-right">
                <button type="submit" class="btn btn-primary btn-lg">Send Campaign</button>
              </div>
            </div>
          </fieldset>
          {!! Form::close() !!}
        </div>
        </div>
    </div>
</div>
@endsection

Sending Emails in Laravel through Mailchimp API

Mailchimp is one of the world’s most popular email marketing solutions, with over 14 million users. The Mailchimp email service core competency is email marketing, and the platform includes several valuable tools that help small businesses with online marketing. This consists of a content library, landing page builder, paid ads campaign manager, and a recommendations engine. Also, the Mailchimp email service does not require advanced technical expertise or coding. 

Mailchimp is best for small businesses that prefer an all-in-one platform that supports the key components of online marketing and does not require advanced technical expertise. If you are a small to medium-sized business that requires sophisticated automation tools, consider testing ActiveCampaign. 

Learn more about What is MailChimp Used For? Email API Guide + 2023 Examples

This article explains what Mailchimp is and what it is used for. It also describes all the steps required for integrating Mailchimp API in Laravel.

I hope you’ve enjoyed the article. Stay tuned for more at FuelingPHP

Dig Deeper with Our Email in PHP Examples and Guides