Skip to content

Commit

Permalink
Add email previews functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-mailosaur committed Sep 15, 2023
1 parent 4f46bba commit 77a427f
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: CI

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]
workflow_dispatch:
schedule:
- cron: '*/8 * * * *'
Expand Down
6 changes: 6 additions & 0 deletions src/MailosaurClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Mailosaur\Operations\Servers;
use Mailosaur\Operations\Usage;
use Mailosaur\Operations\Devices;
use Mailosaur\Operations\Previews;

class MailosaurClient
{
Expand Down Expand Up @@ -36,6 +37,9 @@ class MailosaurClient
/** @var Operations\Devices */
public $devices;

/** @var Operations\Previews */
public $previews;


public function __construct($apiKey, $baseUri = 'https://mailosaur.com/')
{
Expand All @@ -54,6 +58,8 @@ public function __construct($apiKey, $baseUri = 'https://mailosaur.com/')
$this->usage = new Usage($this);

$this->devices = new Devices($this);

$this->previews = new Previews($this);
}

/**
Expand Down
50 changes: 50 additions & 0 deletions src/Models/Preview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Mailosaur\Models;


class Preview
{
/** @var string The unique identifier for the email preview. */
public $id;

/** @var string The email client the preview was generated with. */
public $emailClient;

/** @var bool Whether images were disabled in the preview. */
public $disableImages;

public function __construct(\stdClass $data)
{
if (property_exists($data, 'id')) {
$this->id = $data->id;
}

if (property_exists($data, 'emailClient')) {
$this->emailClient = $data->emailClient;
}

if (property_exists($data, 'disableImages')) {
$this->disableImages = $data->disableImages;
}
}

/**
* @return array
*/
public function __toArray()
{
$model = array(
'id' => $this->id,
'emailClient' => $this->emailClient,
'disableImages' => $this->disableImages
);

return $model;
}

public function toJsonString()
{
return json_encode($this->__toArray());
}
}
82 changes: 82 additions & 0 deletions src/Models/PreviewEmailClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Mailosaur\Models;


class PreviewEmailClient
{
/** @var string The unique identifier for the email preview. */
public $id;

/** @var string The display name of the email client. */
public $name;

/** @var string Whether the platform is desktop, mobile, or web-based. */
public $platformGroup;

/** @var string The type of platform on which the email client is running. */
public $platformType;

/** @var string The platform version number. */
public $platformVersion;

/** @var bool Whether images can be disabled when generating previews. */
public $canDisableImages;

/** @var string The current status of the email client. */
public $status;

public function __construct(\stdClass $data)
{
if (property_exists($data, 'id')) {
$this->id = $data->id;
}

if (property_exists($data, 'name')) {
$this->name = $data->name;
}

if (property_exists($data, 'platformGroup')) {
$this->platformGroup = $data->platformGroup;
}

if (property_exists($data, 'platformType')) {
$this->platformType = $data->platformType;
}

if (property_exists($data, 'platformVersion')) {
$this->platformVersion = $data->platformVersion;
}

if (property_exists($data, 'canDisableImages')) {
$this->canDisableImages = $data->canDisableImages;
}

if (property_exists($data, 'status')) {
$this->status = $data->status;
}
}

/**
* @return array
*/
public function __toArray()
{
$model = array(
'id' => $this->id,
'name' => $this->name,
'platformGroup' => $this->platformGroup,
'platformType' => $this->platformType,
'platformVersion' => $this->platformVersion,
'canDisableImages' => $this->canDisableImages,
'status' => $this->status
);

return $model;
}

public function toJsonString()
{
return json_encode($this->__toArray());
}
}
21 changes: 21 additions & 0 deletions src/Models/PreviewEmailClientListResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Mailosaur\Models;


class PreviewEmailClientListResult
{
/**
* @var PreviewEmailClient[] A list of available email clients with which to generate email previews.
*/
public $items = array();

public function __construct(\stdClass $data)
{
if (property_exists($data, 'items') && is_array($data->items)) {
foreach ($data->items as $item) {
$this->items[] = new PreviewEmailClient($item);
}
}
}
}
21 changes: 21 additions & 0 deletions src/Models/PreviewListResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Mailosaur\Models;


class PreviewListResult
{
/**
* @var Preview[] A list of requested email previews.
*/
public $items = array();

public function __construct(\stdClass $data)
{
if (property_exists($data, 'items') && is_array($data->items)) {
foreach ($data->items as $item) {
$this->items[] = new Preview($item);
}
}
}
}
37 changes: 37 additions & 0 deletions src/Models/PreviewRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Mailosaur\Models;


class PreviewRequest
{
/** @var string The email client you wish to generate a preview for. */
public $emailClient;

/** @var bool Whether images will be disabled (only if supported by the client). */
public $disableImages;

public function __construct($emailClient, $disableImages = false)
{
$this->emailClient = $emailClient;
$this->disableImages = $disableImages;
}

/**
* @return array
*/
public function __toArray()
{
$model = array(
'emailClient' => $this->emailClient,
'disableImages' => $this->disableImages
);

return $model;
}

public function toJsonString()
{
return json_encode($this->__toArray());
}
}
36 changes: 36 additions & 0 deletions src/Models/PreviewRequestOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Mailosaur\Models;


class PreviewRequestOptions
{
/**
* @var PreviewRequest[] The list of email preview requests.
*/
public $previews = array();

public function __construct($previews)
{
$this->previews = $previews;
}

public function __toArray()
{
$model = array(
'previews' => $this->previews
);

return $model;
}

/**
* Prepare json-serialized string
*
* @return string
*/
public function toJsonString()
{
return json_encode($this->__toArray());
}
}
15 changes: 14 additions & 1 deletion src/Operations/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,17 @@ public function getEmail($id)
{
return $this->request('api/files/email/' . urlencode($id));
}
}

/**
* <strong>Download an email preview</strong>
*
* @param string $id The identifier of the preview to be downloaded.
*
* @return string
* @throws \Mailosaur\Models\MailosaurException
*/
public function getPreview($id)
{
return $this->request('api/files/previews/' . urlencode($id));
}
}
30 changes: 30 additions & 0 deletions src/Operations/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Mailosaur\Models\MessageCreateOptions;
use Mailosaur\Models\MessageForwardOptions;
use Mailosaur\Models\MessageReplyOptions;
use Mailosaur\Models\PreviewRequestOptions;
use Mailosaur\Models\PreviewListResult;

class Messages extends AOperation
{
Expand Down Expand Up @@ -309,4 +311,32 @@ public function reply($id, MessageReplyOptions $messageReplyOptions)

return new Message($response);
}

/**
* <strong>Generate email previews</strong>
* <p>Generates screenshots of an email rendered in the specified email clients.</p>
*
* @param $id
* @param $options
*
* @return \Mailosaur\Models\PreviewListResult
* @throws \Mailosaur\Models\MailosaurException
*/
public function generatePreviews($id, PreviewRequestOptions $options)
{
$payload = $options->toJsonString();

$response = $this->request(
'api/messages/' . $id . '/previews',
array(
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => array('Content-Type:application/json', 'Content-Length: ' . strlen($payload))
)
);

$response = json_decode($response);

return new PreviewListResult($response);
}
}
25 changes: 25 additions & 0 deletions src/Operations/Previews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Mailosaur\Operations;


use Mailosaur\Models\PreviewEmailClientListResult;

class Previews extends AOperation
{

/**
* <strong>List all email preview clients</strong>
*
* @return PreviewEmailClientListResult
* @throws \Mailosaur\Models\MailosaurException
*/
public function allEmailClients()
{
$response = $this->request('api/previews/clients');

$response = json_decode($response);

return new PreviewEmailClientListResult($response);
}
}
Loading

0 comments on commit 77a427f

Please sign in to comment.