Skip to content

Commit

Permalink
Add transactional emails support (#8)
Browse files Browse the repository at this point in the history
* Transactional emails
* Send bulk custom emails
  • Loading branch information
kubec authored Dec 15, 2020
1 parent 64eb47f commit f420b79
Show file tree
Hide file tree
Showing 21 changed files with 1,314 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"minimum-stability": "stable",
"require": {
"php": ">=7.3",
"guzzlehttp/guzzle": ">=6.2"
"guzzlehttp/guzzle": ">=6.2",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.5",
Expand Down
108 changes: 108 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,114 @@ if ($customField = $api->customFields()->exists('name')) {
}
```

### Send / Transactional emails
The implementation of API call ``send/transactional-emails-bulk``: https://app.smartemailing.cz/docs/api/v3/index.html#api-Custom_campaigns-Send_transactional_emails
## Full transactional email example
```php
$transactionEmail = new TransactionalEmails($api);

$credentials = new SenderCredentials();
$credentials->setFrom('[email protected]');
$credentials->setReplyTo('[email protected]');
$credentials->setSenderName('Jean-Luc Picard');

$recipient = new Recipient();
$recipient->setEmailAddress('[email protected]');

$replace1 = new Replace();
$replace1->setKey('key1');
$replace1->setContent('content1');

$replace2 = new Replace();
$replace2->setKey('key2');
$replace2->setContent('content2');

$templateVariable = new TemplateVariable();
$templateVariable->setCustomData([
'foo' => 'bar',
'products' => [
['name' => 'prod1', 'desc' => 'desc1'],
['name' => 'prod1', 'desc' => 'desc2']
]
]);

$attachment1 = new Attachment();
$attachment1->setContentType('image/png');
$attachment1->setFileName('picture.png');
$attachment1->setDataBase64('data1');

$attachment2 = new Attachment();
$attachment2->setContentType('image/gif');
$attachment2->setFileName('sun.gif');
$attachment2->setDataBase64('data2');

$task = new Task();
$task->setRecipient($recipient);
$task->addReplace($replace1);
$task->addReplace($replace2);
$task->setTemplateVariables($templateVariable);
$task->addAttachment($attachment1);
$task->addAttachment($attachment2);

$messageContents = new MessageContents();
$messageContents->setTextBody('text_body');
$messageContents->setHtmlBody('html_body');
$messageContents->setSubject('subject');

$transactionEmail->setTag('tag_tag');
$transactionEmail->setEmailId(5);
$transactionEmail->setSenderCredentials($credentials);
$transactionEmail->addTask($task);
$transactionEmail->setMessageContents($messageContents);

$transactionEmail->send();
```

### Send / Bulk custom emails
The implementation of API call ``send/custom-emails-bulk``: https://app.smartemailing.cz/docs/api/v3/index.html#api-Custom_campaigns-Send_bulk_custom_emails
## Full transactional email example
```php
$transactionEmail = new BulkCustomEmails($api);

$credentials = new SenderCredentials();
$credentials->setFrom('[email protected]');
$credentials->setReplyTo('[email protected]');
$credentials->setSenderName('Jean-Luc Picard');

$recipient = new Recipient();
$recipient->setEmailAddress('[email protected]');

$replace1 = new Replace();
$replace1->setKey('key1');
$replace1->setContent('content1');

$replace2 = new Replace();
$replace2->setKey('key2');
$replace2->setContent('content2');

$templateVariable = new TemplateVariable();
$templateVariable->setCustomData([
'foo' => 'bar',
'products' => [
['name' => 'prod1', 'desc' => 'desc1'],
['name' => 'prod1', 'desc' => 'desc2']
]
]);

$task = new Task();
$task->setRecipient($recipient);
$task->addReplace($replace1);
$task->addReplace($replace2);
$task->setTemplateVariables($templateVariable);

$transactionEmail->setTag('tag_tag');
$transactionEmail->setEmailId(5);
$transactionEmail->setSenderCredentials($credentials);
$transactionEmail->addTask($task);

$transactionEmail->send();
```

## Changelog

### 0.1.8
Expand Down
107 changes: 107 additions & 0 deletions src/Request/Send/AbstractSend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php declare(strict_types=1);

namespace SmartEmailing\v3\Request\Send;

use JsonSerializable;
use SmartEmailing\v3\Request\AbstractRequest;

/**
* Class TransactionEmails
*
* @link https://app.smartemailing.cz/docs/api/v3/index.html#api-Custom_campaigns-Send_transactional_emails
*/
abstract class AbstractSend extends AbstractRequest implements JsonSerializable
{
/** @var SenderCredentials */
protected $senderCredentials;
/** @var int */
protected $emailId;
/** @var string */
protected $tag;
/** @var Task[] */
protected $tasks = [];

/**
* @return int
*/
public function getEmailId(): ?int
{
return $this->emailId;
}

/**
* @param int $emailId
*/
public function setEmailId(int $emailId): void
{
$this->emailId = $emailId;
}

/**
* @return string
*/
public function getTag(): ?String
{
return $this->tag;
}

/**
* @param string $tag
*/
public function setTag(string $tag): void
{
$this->tag = $tag;
}

/**
* @return SenderCredentials
*/
public function getSenderCredentials(): ?SenderCredentials
{
return $this->senderCredentials;
}

/**
* @param SenderCredentials $senderCredentials
*/
public function setSenderCredentials(SenderCredentials $senderCredentials): void
{
$this->senderCredentials = $senderCredentials;
}

/**
* @param Task $task
*/
public function addTask(Task $task): void
{
$this->tasks[] = $task;
}

/**
* @return Task[]
*/
public function getTasks(): array
{
return $this->tasks;
}

/**
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}

protected function options()
{
return [
'json' => $this->jsonSerialize(),
];
}

protected function method()
{
return 'POST';
}
}
67 changes: 67 additions & 0 deletions src/Request/Send/Attachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);

namespace SmartEmailing\v3\Request\Send;

use SmartEmailing\v3\Exceptions\PropertyRequiredException;
use SmartEmailing\v3\Models\Model;

class Attachment extends Model
{
/** @var string */
private $fileName;

/** @var string */
private $contentType;

/** @var string */
private $dataBase64;


public function getFileName(): ?String
{
return $this->fileName;
}

public function setFileName(String $fileName): void
{
$this->fileName = $fileName;
}

public function getContentType(): ?String
{
return $this->contentType;
}

public function setContentType(String $contentType): void
{
$this->contentType = $contentType;
}

public function getDataBase64(): ?String
{
return $this->dataBase64;
}

public function setDataBase64(String $dataBase64): void
{
$this->dataBase64 = $dataBase64;
}

public function toArray(): array
{
PropertyRequiredException::throwIf('file_name', !empty($this->getFileName()), 'You must set file_name - missing file_name');
PropertyRequiredException::throwIf('content_type', !empty($this->getContentType()), 'You must set content_type - missing content_type');
PropertyRequiredException::throwIf('data_base64', !empty($this->getDataBase64()), 'You must set data_base64 - missing data_base64');

return [
'file_name' => $this->getFileName(),
'content_type' => $this->getContentType(),
'data_base64' => $this->getDataBase64(),
];
}

public function jsonSerialize(): array
{
return $this->toArray();
}
}
34 changes: 34 additions & 0 deletions src/Request/Send/BulkCustomEmails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace SmartEmailing\v3\Request\Send;

/**
* Class BulkCustomEmails
*
* @link https://app.smartemailing.cz/docs/api/v3/index.html#api-Custom_campaigns-Send_bulk_custom_emails
*/
class BulkCustomEmails extends AbstractSend
{
/**
* Converts data to array
*
* @return array
*/
public function toArray(): array
{
return [
'sender_credentials' => $this->getSenderCredentials(),
'tag' => $this->getTag(),
'email_id' => $this->getEmailId(),
'tasks' => $this->getTasks(),
];
}

/**
* @return string
*/
protected function endpoint()
{
return 'send/custom-emails-bulk';
}
}
Loading

0 comments on commit f420b79

Please sign in to comment.