2016-05-10 13:04:43 +02:00

127 lines
2.6 KiB
PHP
Executable File

<?php
require_once('php-mailjet-v3-simple.class.php');
class MailjetSyncApiClientV3 implements IMailjetSyncApiClient
{
var $api_key;
var $secret_key;
var $client;
public function __construct($api_key, $secret_key)
{
$this->api_key = $api_key;
$this->secret_key = $secret_key;
$this->client = new MailjetSyncApiV3($this->api_key, $this->secret_key);
}
public static function testAPI($api_key, $secret_key)
{
$client = new MailjetSyncApiV3($api_key, $secret_key);
$client->myprofile();
return ($client->_response_code == 200);
}
public static function checkResponseCode($code)
{
if (in_array($code, array(200, 201, 204, 304)))
return true;
else
return false;
}
public function isConfigured()
{
return MailjetSyncApiClientV3::testAPI($this->api_key, $this->secret_key);
}
public function getLists()
{
if (!$this->isConfigured())
return array();
$response = $this->client->contactslist();
if ($response === false)
return array();
$data = array();
foreach ($response->Data as $l)
{
$data[] = array(
'id' => $l->ID,
'name' => $l->Name,
'subscribers' => $l->SubscriberCount,
);
}
return $data;
}
public function addContactToList($email, $list_id)
{
if (!$this->isConfigured())
return false;
$params = array(
'method' => 'POST',
'Action' => 'Add',
'Force' => true,
'Addresses' => array($email),
'ListID' => $list_id,
);
$this->client->manycontacts($params);
if ($this->client->_response_code == 201)
return true;
return false;
}
public function addContactsToList($emails, $list_id)
{
if (!$this->isConfigured())
return false;
$params = array(
'method' => 'POST',
'Action' => 'Add',
'Force' => true,
'Addresses' => $emails,
'ListID' => $list_id,
);
$this->client->manycontacts($params);
if ($this->client->_response_code == 201)
return true;
return false;
}
public function addDetailedContactToList ($contacts, $lists)
{
$params = array(
"method" => "POST",
"ContactsLists" => $lists,
"Contacts" => $contacts
);
$result = $this->client->managemanycontacts($params);
if ($this->client->_response_code == 201) {
return true;
}
return false;
}
public function deleteContactFromList($email, $list_id, $delete = false)
{
if (!$this->isConfigured())
return false;
$params = array(
'method' => 'POST',
'Force' => true,
'Addresses' => array($email),
'ListID' => $list_id,
'Action' => $delete ? 'Remove' : 'Unsubscribe'
);
$this->client->manycontacts($params);
if ($this->client->_response_code == 201)
return true;
return false;
}
}