Email Newsletter Integration (Mailchimp)
Mailchimp is an international leader in email marketing with a powerful template editor, automations, and a mature API. Suitable for projects targeting Western markets.
Adding Subscriber via Marketing API
// composer require mailchimp/marketing
$mailchimp = new \MailchimpMarketing\ApiClient();
$mailchimp->setConfig([
'apiKey' => env('MAILCHIMP_API_KEY'),
'server' => env('MAILCHIMP_SERVER_PREFIX') // 'us1', 'eu2', etc.
]);
try {
$mailchimp->lists->addListMember(env('MAILCHIMP_LIST_ID'), [
'email_address' => $email,
'status' => 'subscribed', // or 'pending' for double opt-in
'merge_fields' => [
'FNAME' => $firstName,
'LNAME' => $lastName
],
'tags' => ['website', 'organic']
]);
} catch (\GuzzleHttp\Exception\ClientException $e) {
// 400 — if email already exists, check status
}
Upsert Subscriber
If contact already exists — use MD5 hash of email:
$subscriberHash = md5(strtolower($email));
$mailchimp->lists->setListMember(env('MAILCHIMP_LIST_ID'), $subscriberHash, [
'email_address' => $email,
'status_if_new' => 'subscribed',
'merge_fields' => ['FNAME' => $firstName]
]);
Transactional Emails via Mandrill (Mailchimp Transactional)
Mandrill is Mailchimp's transactional email service. Templates are stored in Mandrill, emails are sent via API:
$mandrill = new \Mandrill(env('MANDRILL_API_KEY'));
$mandrill->messages->sendTemplate('order-confirmation', [
['name' => 'ORDER_ID', 'content' => $orderId],
['name' => 'ORDER_TOTAL','content' => $orderTotal]
], [
'to' => [['email' => $to]],
'subject' => "Order #{$orderId} Confirmed"
]);
Events and Automations
Mailchimp Customer Journeys are triggered via API events:
$mailchimp->lists->createListMemberEvent(
env('MAILCHIMP_LIST_ID'),
md5(strtolower($email)),
['name' => 'purchase', 'properties' => ['amount' => 1500]]
);
Integration timeline: 1 business day for basic subscription + transactional emails.







