From 4c92145257845113183c6eeee2d588a3f87d61a6 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Tue, 14 Mar 2017 11:59:30 -0400 Subject: [PATCH] switch from mailgun lists to sendy --- controller/action/MailActions.class.php | 10 +- lib/thirdparty/Sendy.class.php | 132 ++++++++++++++++++ .../template/email_templates/_confirmHash.php | 2 +- 3 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 lib/thirdparty/Sendy.class.php diff --git a/controller/action/MailActions.class.php b/controller/action/MailActions.class.php index 4b2c541e..ef564daa 100644 --- a/controller/action/MailActions.class.php +++ b/controller/action/MailActions.class.php @@ -41,10 +41,14 @@ class MailActions extends Actions return ['mail/subscribe', ['error' => __('email.invalid_confirm_hash')]]; } - $outcome = Mailgun::addToMailingList(Mailgun::LIST_GENERAL, $email); - if ($outcome !== true) + $sendy = new Sendy(Config::get('sendy_api_key'), Config::get('sendy_install_url'), Sendy::LIST_GENERAL); + try { - return ['mail/subscribe', ['error' => $outcome]]; + $sendy->subscribe($email); + } + catch (SendyException $e) + { + return ['mail/subscribe', ['error' => $e->getMessage()]]; } return ['mail/subscribe', ['confirmSuccess' => true, 'learnFooter' => true]]; diff --git a/lib/thirdparty/Sendy.class.php b/lib/thirdparty/Sendy.class.php new file mode 100644 index 00000000..afe0dddf --- /dev/null +++ b/lib/thirdparty/Sendy.class.php @@ -0,0 +1,132 @@ +apiKey = $apiKey; + $this->installationUrl = rtrim($installationUrl, '/'); + $this->listId = $listId; + } + + public static function getAllSubscriptionStatuses() + { + $r = new ReflectionClass(get_called_class()); + + $constants = []; + foreach ($r->getConstants() as $constant => $value) + { + if (preg_match('/^STATUS_/', $constant)) + { + $constants[] = $value; + } + } + + return $constants; + } + + public function subscribe($email, $name = null, array $extraVars = []) + { + $result = $this->call('subscribe', array_merge($extraVars, [ + 'email' => $email, + 'name' => $name, + 'list' => $this->listId, + 'boolean' => 'true', + ])); + + switch ($result) + { + case '1': + return true; + + case 'Already subscribed.': + return 'Already subscribed.'; + + default: + throw new SendyException($result); + } + } + + public function unsubscribe($email) + { + $result = $this->call('subscribe', [ + 'email' => $email, + 'list' => $this->listId, + 'boolean' => 'true', + ]); + + if ($result == 1) + { + return true; + } + + throw new SendyException($result); + } + + public function getSubscriptionStatus($email) + { + $result = $this->callAuth('api/subscribers/subscription-status.php', ['email' => $email, 'list_id' => $this->listId]); + + if (in_array($result, static::getAllSubscriptionStatuses())) + { + return $result; + } + + throw new SendyException($result); + } + + public function getActiveSubscriberCount() + { + $result = $this->callAuth('api/subscribers/active-subscriber-count.php', ['list_id' => $this->listId]); + + if (!is_numeric($result)) + { + throw new SendyException($result); + } + + return (int)$result; + } + + public function createCampaign(array $values) + { + $result = $this->callAuth('api/campaigns/create.php', $values); + + switch ($result) + { + case 'Campaign created': + case 'Campaign created and now sending': + return $result; + + default: + throw new SendyException($result); + } + } + + protected function call($endpoint, array $values = []) + { + return Curl::post($this->installationUrl . '/' . $endpoint, $values); + } + + protected function callAuth($endpoint, array $values = []) + { + return $this->call($endpoint, array_merge($values, ['api_key' => $this->apiKey])); + } +} diff --git a/view/template/email_templates/_confirmHash.php b/view/template/email_templates/_confirmHash.php index fff28d04..1750b9ea 100644 --- a/view/template/email_templates/_confirmHash.php +++ b/view/template/email_templates/_confirmHash.php @@ -1,4 +1,4 @@ - +