Step by step guide on Mailchimp integration in laravel

Step by step guide on Mailchimp integration in laravel

Hi guys,

Mailchimp is the most popular email marketing service and can manage the subscribers to your websites and get a newsletter about new content or deals. You can automate the email service using the MailChimp email tool. In this article, we integrate the Mailchimp service into the laravel application.

Step-by-step guide on Mailchimp integration in laravel:

  1. Get Mailchimp API key and audience ID:

Click here to create an account on the Mailchimp platform. If you have an account, follow the steps to get API credentials from your account.

Goto profile dropdown, Click Account and Goto Extras tab and click API keys

Mailchimp screen
Mailchimp

After, we need an Audience ID for mail configuration. Goto Audience menu, Click on Manage Audience, and then settings. Click Audience name and defaults. You will get an audience ID.

Mailchimp
Mailchimp
  1. Install Mailchimp library

Mailchimp provides a library for PHP language. You need to run the composer command.

composer require mailchimp/mailchimp
  1. Add a credential to the laravel project.

You need to add a Mailchimp credential in the .env file

MAILCHIMP_APIKEY=XXXXXXXXXXXXXXX
MAILCHIMP_LIST_ID=XXXXXXXXXXXXXX
  1. Implement on Controller

Create a method inside a controller class. You should call this method with an instance of the class.

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mailchimp;

class MailController extends Controller
{
	public function sendNewsletter(Request $request)
	{
		$listId = env('MAILCHIMP_LIST_ID');

		$mailchimp = new MailChimp(env('MAILCHIMP_APIKEY'));

		$campaign = $mailchimp->campaigns->create('regular',[
			'list_id'=>$list_id,
			'subject'=>'Welcome Mail',
			'from_email'=>'[email protected]',
			'from_name'=>'Devrohit',
			'to_name'=>'Devrohit Subscriber',		
		],[
			'html'=> $request->input('body'),
			'text'=>strip_tags($request->input('body'))
		]);

		$mailchimp->campaigns->send($campaign['id']);

		dd('Campaign send successfully.');
	}
}
  1. Register route

you can access the above method using the URL.

Route::get('/send-welcome-mail','MailController@sendNewsletter');

You can add the above code to the register function. Now your users will get welcome mail when they register for your application. You measure business impact on your email campaigns with a/b testing.

Now your users will get welcome mail when they register for your application. Do you want to check which mail tools are best for your application? Click Here. I hope that this post (step-by-step guide on Mailchimp integration in laravel) has clarified how to integrate MailChimp in laravel. If you have any questions, please leave a comment and I will respond as soon as possible.

Thank you for reading this article. Please share this article with your friend circle. That’s it for the day. Stay Connected!
Cheers

Loading

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top