public function process() { $errorMessage = $this->lexicon->getMsg('mc.subscription_failed'); if (!$this->getCFGDef('apiKey')) { $this->addMessage($errorMessage); return false; } $MailChimp = new \DrewM\MailChimp\MailChimp($this->getCFGDef('apiKey')); $list_id = $this->getCFGDef('listId'); if (!$list_id) { $this->addMessage($errorMessage); return false; } $MailChimp->post("lists/{$list_id}/members", array('email_address' => $this->getField('email'), 'merge_fields' => array('NAME' => $this->getField('name')), 'status' => 'pending')); if (!$MailChimp->getLastError()) { $this->addMessage($errorMessage); } else { $this->setFormStatus(true); $this->renderTpl = $this->getCFGDef('successTpl', $this->lexicon->getMsg('mc.default_successTpl')); return true; } }
/** * listUnsubscribe * Unsubscribe the user from the mailing list. Does not remove the user, just sets status to unsubscribed. * * @param listid is the id for the list * @param email_address is the email address subscribed in mailchimp * @return false in case of an error */ public static function listUnsubscribe($listid, $email_address, $externaluserargs = null, $email_type = 'html', $memberlist = null) { global $CFG; require_once $CFG->dirroot . '/blocks/mailchimp/classes/MailChimp.php'; if (!isset($CFG->block_mailchimp_apicode)) { return false; } $memberid = helper::getMemberID($listid, $email_address, $memberlist); if (!$memberid) { //If member is not already present in the list, add them to the list with an unsubscribed status. $method = "lists/" . $listid . "/members"; if (!($api = new \DrewM\MailChimp\MailChimp($CFG->block_mailchimp_apicode))) { debugging("ERROR: Unable to create mailchimp wrapper object \\DrewM\\MailChimp\\MailChimp."); return false; } $args = array('email_address' => strtolower($email_address), 'email_type' => $email_type, 'status' => 'unsubscribed', 'VIP' => false, 'merge_fields' => array('FNAME' => $externaluserargs['FNAME'], 'LNAME' => $externaluserargs['LNAME'])); if (!$api->post($method, $args)) { debugging("ERROR: Unable to add unsubscribed user " . $email_address . " to mailchimp, method: " . $method); return false; } return true; } // Member is present in the list, update the status to unsubscribed. $method = "lists/" . $listid . "/members/" . $memberid; if (!($api = new \DrewM\MailChimp\MailChimp($CFG->block_mailchimp_apicode))) { debugging("ERROR: Unable to create mailchimp wrapper object \\DrewM\\MailChimp\\MailChimp."); return false; } $args = array('email_address' => strtolower($email_address), 'status' => 'unsubscribed'); if (!$api->patch($method, $args)) { debugging("ERROR: Unable to unsubscribe mailchimp user " . $email_address . ", method: " . $method); return false; } return true; }