Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
arzzen committed Jan 31, 2022
0 parents commit eb43a79
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/nbproject/*
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# SmartEmailing

Easy way to interact with SmartEmailing API from PHP

## Installation

The best way to install this component is using [Composer](http://getcomposer.org/):

```sh
$ composer require belenka/smartemailing
```

Then it is required to add the following lines to config.neon:

```
parameters:
smartemailing:
username: <smartemailing_username>
token: <smartemailing_api_token>
services:
- BeLenka\SmartEmailing(%smartemailing.username%, %smartemailing.token%)
```

## Usage

Insert a new contact into SmartEmailing lists:

```
$this->smartEmailing->importContact($email, $name, $surname, $language, $contactLists, $properties, $customFields, $purposes, $settings);
```

Insert a order data into SmartEmailing lists:

```
$this->smartEmailing->importOrders($data);
```

Get an exisitng contact by email:

```
$this->smartEmailing->getContactsByEmail($email);
```

Get all contacts:

```
$this->smartEmailing->getContacts();
```

Get an exisitng contact by user's ID:

```
$this->smartEmailing->getContact($id);
```




28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "belenka/smartemailing",
"type": "library",
"description": "Easy way to interact with SmartEmailing API from PHP",
"license": ["MIT"],
"authors": [
{
"name": "Apps Dev Team",
"email": "[email protected]",
"homepage": "https://www.appsdevteam.com"
},
{
"name": "BeLenka Dev Team",
"email": "[email protected]",
"homepage": "https://www.belenka.com"
}
],
"require": {
"guzzlehttp/guzzle": "^6.3",
"nette/utils": "^2.2"
},
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"belenka\\": "src/"
}
}
}
294 changes: 294 additions & 0 deletions src/SmartEmailing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
<?php

namespace belenka;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Nette\Utils\Json;

/**
* SmartEmailing API v3
* http://docs.smartemailing.apiary.io/
*/
class SmartEmailing
{

const NODE_PING = 'ping';
const NODE_CHECK_CREDENTIALS = 'check-credentials';
const NODE_CONTACTLISTS = 'contactlists';
const NODE_CUSTOMFIELDS = 'customfields';
const NODE_CUSTOMFIELDS_OPTIONS = 'customfield-options';
const NODE_CONTACT_CUSTOMFIELDS = 'contact-customfields';
const NODE_CHANGE_EMAILADDRESS = 'change-emailaddress';
const NODE_CONTACT_FORGET = 'contacts/forget';
const NODE_CONTACTS = 'contacts';
const NODE_IMPORT = 'import';
const NODE_PURPOSES = 'purposes';
const NODE_PURPOSE_CONNECTIONS = 'purpose-connections';
const NODE_ORDERS = 'orders';
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_DELETE = 'DELETE';

protected $url = 'https://app.smartemailing.cz/api/v3';
protected $username;
protected $token;

/**
* SmartEmailing constructor.
*
* @param string $username
* @param string $token
*/
public function __construct($username, $token)
{
$this->username = $username;
$this->token = $token;
}

/**
* Aliveness test
*
* @return array
* @throws SmartEmailingException
*/
public function ping()
{
return $this->call(self::METHOD_GET, self::NODE_PING);
}

/**
* Login test
*
* @return array
* @throws SmartEmailingException
*/
public function checkCredentials()
{
return $this->call(self::METHOD_GET, self::NODE_CHECK_CREDENTIALS);
}

/**
* @param string $name
* @param string $senderName
* @param string $senderEmail email
* @param string $replyTo email
* @param string|null $publicName
* @throws SmartEmailingException
*/
public function createContactlist($name, $senderName, $senderEmail, $replyTo, $publicName = NULL)
{
$data = [
'name' => $name,
'sendername' => $senderName,
'senderemail' => $senderEmail,
'replyto' => $replyTo,
];
if (is_string($publicName)) {
$data['publicname'] = $publicName;
}
return $this->call(self::METHOD_POST, self::NODE_CONTACTLISTS, $data);
}

/**
* @return array
* @throws SmartEmailingException
*/
public function getContactlists()
{
return $this->call(self::METHOD_GET, self::NODE_CONTACTLISTS);
}

/**
* @param $id
* @return array
* @throws SmartEmailingException
*/
public function getContactlist($id)
{
return $this->call(self::METHOD_GET, self::NODE_CONTACTLISTS . "/" . $id);
}

/**
* @param $id
* @param string|null $name
* @param string|null $senderName
* @param string|null $senderEmail
* @param string|null $replyTo
* @param string|null $publicName
* @return array
* @throws SmartEmailingException
*/
public function updateContactlist($id, $name = NULL, $senderName = NULL, $senderEmail = NULL, $replyTo = NULL, $publicName = NULL)
{
$data = [];
if ($name !== NULL)
$data['name'] = $name;
if ($senderName !== NULL)
$data['sendername'] = $senderName;
if ($senderEmail !== NULL)
$data['senderemail'] = $senderEmail;
if ($replyTo !== NULL)
$data['replyto'] = $replyTo;
if ($publicName !== NULL)
$data['publicname'] = $publicName;
if ($publicName !== NULL)
$data['publicname'] = $publicName;

return $this->call(self::METHOD_PUT, self::NODE_CONTACTLISTS . "/" . $id, $data);
}

/**
* @param string $from
* @param string $to
* @return array
* @throws SmartEmailingException
*/
public function changeContactEmail($from, $to)
{
return $this->call(self::METHOD_POST, self::NODE_CHANGE_EMAILADDRESS, [
'from' => $from,
'to' => $to,
]);
}

/**
* Deletes contact and anonymizes all his leftover data. This action cannot be undone.
* This is GDPR complaint method to secure contact's right to be forgotten.
*
* @param $id
* @return array
* @throws SmartEmailingException
*/
public function deleteContact($id)
{
return $this->call(self::METHOD_DELETE, self::NODE_CONTACT_FORGET . "/" . $id);
}

/**
* @return array
* @throws SmartEmailingException
*/
public function getContacts($query = [])
{
return $this->call(self::METHOD_GET, self::NODE_CONTACTS, null, $query);
}

/**
* @return array
* @throws SmartEmailingException
*/
public function getContactsByEmail($email)
{
$query = ['emailaddress' => $email];
return $this->call(self::METHOD_GET, self::NODE_CONTACTS, null, $query);
}

/**
* @return array
* @throws SmartEmailingException
*/
public function getContact($id)
{
return $this->call(self::METHOD_GET, self::NODE_CONTACTS . "/" . $id);
}

/**
* https://app.smartemailing.cz/docs/api/v3/index.html#api-Import-Import_contacts
*
* @param $email
* @param array|NULL $contactLists
* @param array|NULL $properties
* @param array|NULL $customFields
* @param array|NULL $purposes
* @param array|NULL $settings
*/
public function importContact($email, $name = NULL, $surname = NULL, $language = 'cz_CZ', array $contactLists = NULL, array $properties = NULL, array $customFields = NULL, array $purposes = NULL, array $settings = NULL)
{
$contact = [
'emailaddress' => $email,
];

if (is_array($contactLists)) {
$contact['contactlists'] = [];
foreach ($contactLists as $id => $status) {
$contact['contactlists'][] = [
'id' => $id,
'status' => $status,
'name' => $name,
'surname' => $surname,
'language' => $language
];
}
}

if (is_array($properties)) {
foreach ($properties as $name => $value) {
$contact[$name] = $value;
// $contact['data'][$name] = $value;
}
}

if (is_array($customFields)) {
$contact['customfields'] = $customFields;
}

if (is_array($purposes)) {
$contact['purposes'] = $purposes;
}

$data = [
'data' => [
$contact,
],
];

if (is_array($settings)) {
$data['settings'] = $settings;
}

return $this->call(self::METHOD_POST, self::NODE_IMPORT, $data);
}

public function importOrders(array $data)
{
return $this->call(self::METHOD_POST, self::NODE_ORDERS, $data);
}

/**
* connect to Smartemailing API v3
*
* @param string $method
* @param string $node
* @param array|null $data
* @param array|null $query
* @return array
* @throws SmartEmailingException
*/
protected function call($method, $node, $data = NULL, $query = NULL)
{
$options = [
'auth' => [$this->username, $this->token],
];
if (is_array($data)) {
$options['json'] = $data;
}
if (is_array($query)) {
$options['query'] = $query;
}

$client = new Client();

try {
$response = $client->request($method, $this->url . "/" . $node, $options);
} catch (ClientException $e) {
$response = $e->getResponse();
$body = Json::decode((string) $response->getBody(), Json::FORCE_ARRAY);
throw new SmartEmailingException($body['message'], $response->getStatusCode());
}

return Json::decode((string) $response->getBody(), Json::FORCE_ARRAY);
}

}
8 changes: 8 additions & 0 deletions src/SmartEmailingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace belenka;

class SmartEmailingException extends \Exception
{

}

0 comments on commit eb43a79

Please sign in to comment.