switch from mailgun lists to sendy

This commit is contained in:
Alex Grintsvayg 2017-03-14 11:59:30 -04:00
parent c62e7849b7
commit 4c92145257
3 changed files with 140 additions and 4 deletions

View file

@ -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]];

132
lib/thirdparty/Sendy.class.php vendored Normal file
View file

@ -0,0 +1,132 @@
<?php
class SendyException extends Exception
{
}
class Sendy
{
const STATUS_SUBSCRIBED = 'Subscribed';
const STATUS_UNSUBSCRIBED = 'Unsubscribed';
const STATUS_UNCONFIRMED = 'Unconfirmed';
const STATUS_BOUNCED = 'Bounced';
const STATUS_SOFT_BOUNCED = 'Soft bounced';
const STATUS_COMPLAINED = 'Complained';
const LIST_TEST = 'ko9LyWC4SQMI6763xTYCXucA';
const LIST_GENERAL = '5Fax4vQ1c9lku763BKUNb4aQ';
protected $installationUrl;
protected $apiKey;
protected $listId;
public function __construct($apiKey, $installationUrl, $listId)
{
$this->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]));
}
}

View file

@ -1,4 +1,4 @@
<?php $url = 'https://' . Request::getHost() . '/list/confirm/' . $confirmHash ?>
<?php $url = Request::getHostAndProto() . '/list/confirm/' . $confirmHash ?>
<?php ob_start() ?>
<meta itemprop="name" content="Confirm Subscription"/>
<table width="100%" cellpadding="0" cellspacing="0">