StackTips
 1 minutes

How to Add Subscribers to a List Using MailChimp REST Api V3

By Editorial @stacktips, On Sep 17, 2023 PHP 2.37K Views

MailChimp REST API V3 exposes methods that allow you to add, manage members of a specific MailChimp list. The following code snippet helps you to add a new subscriber to MailChimp List.

<?php
class ST_Newsletter_Plugin {
	public function __construct(){ 
	
	} 
	
	function subscribe($user_id, $user_email){
		//get api_key and list id from admin settings
		$api_key = "c8d8c17ae54aui8ujnj43343-us9";
		$list_id = "2d9e113i8hm";
		
		require_once('MailChimp.php');
		$mailChimp = new MailChimp($api_key);
		
		$result = $mailChimp->post("lists/$list_id/members", [
			'email_address' => $user_email,
			'status'=> 'subscribed'
		]);

		if ($mailChimp->success()) {
		    echo "success";
		} else {
		   echo "fail";
		}
	}
}

?>

You can download “MailChimp.php” from GitHub.

stacktips avtar

Editorial

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages.