/** * @param string $email * @param string $firstName * @param string $lastName * @param array|object $interests Properties/Keys are interest ids and values are boolean * @param array $extras Additional fields to be passed to the Mailchimp API * * @throws \Exception */ public function subscribe(string $email, string $firstName = null, string $lastName = null, $interests = null, array $extras = []) : bool { $mergeFields = []; if ($firstName !== null) { $mergeFields['FNAME'] = $firstName; } if ($lastName !== null) { $mergeFields['LNAME'] = $lastName; } $data = array_merge(['email_address' => $email, 'status' => 'subscribed', 'merge_fields' => (object) $mergeFields], $extras); if ($interests !== null) { $data['interests'] = (object) $interests; } $result = $this->api->post('lists/' . $this->id() . '/members', $data); return $result->has('id') && strlen($result->get('id')) > 0; }
/** * @param Subscriber $subscriber * @param string $listId * * @throws UnableToAddSubscriberException */ public function addSubscriberToList(Subscriber $subscriber, $listId) { try { $getUri = sprintf("/lists/%s/members/%s", $listId, md5(strtolower($subscriber->getEmail()->toNative()))); $this->mc->get($getUri); // User already exists, therefore don't do anything return; } catch (\Exception $e) { } try { $postURI = sprintf("/lists/%s/members", $listId); $this->mc->post($postURI, $subscriber->toArray()); } catch (\Exception $e) { throw new UnableToAddSubscriberException(sprintf('Unable to add subscriber %s to the Mailchimp list (%s)', $subscriber->getEmailString(), $listId)); } }