These days more and more clients ask for integrations with external marketing services, like sending email via Mailchimp. Since I have that built in my newsletter service called LinksLetter (it helps to send these emails every Thursday), I want to show you how simple it is.
Usually most external services have their APIs, and those have their wrappers for different languages or frameworks. In this case, we’re talking about drewm/mailchimp-api – MailChimp API v3 wrapper for PHP.
We just use it in composer.json, like this:
Then – usual stuff for packages: config/app.php file Service and Facade:
App\Providers\MailchimpApiServiceProvider::class, // ... 'Mailchimp' => App\Facades\MailchimpApi\Facade\MailchimpApiFacade::class,
And finally we can use is as Mailchimp::. In my case, I needed a list of Mailchimp lists, where I choose one of them and pass list ID to send() function. Like this:
use Mailchimp; // ... $lists = Mailchimp::getLists(); // ... choosing $list_id $result = Mailchimp::send($list_id, $newsletter_subject, $newsletter_html); if ($result['errors']) return back()->withErrors($result['errors']);
Also you can schedule the sending of your campaign – just pass in the time as parameter, keep in mind you need it in specific Mailchimp format:
$dateTime = Carbon::createFromFormat('Y m d - H:i', $request->date_time) ->format('Y-m-d\TH:i:s+00:00'); $result = Mailchimp::schedule($list_id, $subject, $html, $dateTime);
So this is a simple example of working with Mailchimp, but I wanted to illustrate how simple it is to use external popular APIs and services – just read the documentation, if there is one.