Step by step guide on Mailchimp integration in laravel

Rohit urane
2 min readApr 14, 2022

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:

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

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

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

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

composer require mailchimp/mailchimp

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

MAILCHIMP_APIKEY=XXXXXXXXXXXXXXX 
MAILCHIMP_LIST_ID=XXXXXXXXXXXXXX

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'=>'noreply@devrohit.com',
'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.');
}
}

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 user will get welcome mail when they register to your application. You measure business impact on your email campaigns with a/b testing.

Now your user will get welcome mail when they register for your application. Do you want to check which mail tools 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

Originally published at https://www.devrohit.com on April 14, 2022.

--

--

Rohit urane

enthusiastic full stack developer to build application