diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a2fb56e..bd10243 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,9 +2,9 @@ name: CI on: push: - branches: [ master ] + branches: [ main ] pull_request: - branches: [ master ] + branches: [ main ] workflow_dispatch: schedule: - cron: '*/8 * * * *' diff --git a/src/MailosaurClient.php b/src/MailosaurClient.php index 77e94f5..1442c7c 100644 --- a/src/MailosaurClient.php +++ b/src/MailosaurClient.php @@ -9,6 +9,7 @@ use Mailosaur\Operations\Servers; use Mailosaur\Operations\Usage; use Mailosaur\Operations\Devices; +use Mailosaur\Operations\Previews; class MailosaurClient { @@ -36,6 +37,9 @@ class MailosaurClient /** @var Operations\Devices */ public $devices; + /** @var Operations\Previews */ + public $previews; + public function __construct($apiKey, $baseUri = 'https://mailosaur.com/') { @@ -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); } /** diff --git a/src/Models/Preview.php b/src/Models/Preview.php new file mode 100644 index 0000000..156d715 --- /dev/null +++ b/src/Models/Preview.php @@ -0,0 +1,50 @@ +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()); + } +} diff --git a/src/Models/PreviewEmailClient.php b/src/Models/PreviewEmailClient.php new file mode 100644 index 0000000..fe4f0b1 --- /dev/null +++ b/src/Models/PreviewEmailClient.php @@ -0,0 +1,82 @@ +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()); + } +} diff --git a/src/Models/PreviewEmailClientListResult.php b/src/Models/PreviewEmailClientListResult.php new file mode 100644 index 0000000..6d1eba8 --- /dev/null +++ b/src/Models/PreviewEmailClientListResult.php @@ -0,0 +1,21 @@ +items)) { + foreach ($data->items as $item) { + $this->items[] = new PreviewEmailClient($item); + } + } + } +} diff --git a/src/Models/PreviewListResult.php b/src/Models/PreviewListResult.php new file mode 100644 index 0000000..7641000 --- /dev/null +++ b/src/Models/PreviewListResult.php @@ -0,0 +1,21 @@ +items)) { + foreach ($data->items as $item) { + $this->items[] = new Preview($item); + } + } + } +} diff --git a/src/Models/PreviewRequest.php b/src/Models/PreviewRequest.php new file mode 100644 index 0000000..08d53bb --- /dev/null +++ b/src/Models/PreviewRequest.php @@ -0,0 +1,37 @@ +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()); + } +} diff --git a/src/Models/PreviewRequestOptions.php b/src/Models/PreviewRequestOptions.php new file mode 100644 index 0000000..15c9a0a --- /dev/null +++ b/src/Models/PreviewRequestOptions.php @@ -0,0 +1,36 @@ +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()); + } +} diff --git a/src/Operations/Files.php b/src/Operations/Files.php index 965f2bd..9d27fa4 100644 --- a/src/Operations/Files.php +++ b/src/Operations/Files.php @@ -36,4 +36,17 @@ public function getEmail($id) { return $this->request('api/files/email/' . urlencode($id)); } -} \ No newline at end of file + + /** + * Download an email preview + * + * @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)); + } +} diff --git a/src/Operations/Messages.php b/src/Operations/Messages.php index ffc3e0c..4445688 100644 --- a/src/Operations/Messages.php +++ b/src/Operations/Messages.php @@ -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 { @@ -309,4 +311,32 @@ public function reply($id, MessageReplyOptions $messageReplyOptions) return new Message($response); } + + /** + * Generate email previews + *

Generates screenshots of an email rendered in the specified email clients.

+ * + * @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); + } } diff --git a/src/Operations/Previews.php b/src/Operations/Previews.php new file mode 100644 index 0000000..39eb978 --- /dev/null +++ b/src/Operations/Previews.php @@ -0,0 +1,25 @@ +List all email preview clients + * + * @return PreviewEmailClientListResult + * @throws \Mailosaur\Models\MailosaurException + */ + public function allEmailClients() + { + $response = $this->request('api/previews/clients'); + + $response = json_decode($response); + + return new PreviewEmailClientListResult($response); + } +} diff --git a/tests/PreviewsTests.php b/tests/PreviewsTests.php new file mode 100644 index 0000000..7ec989d --- /dev/null +++ b/tests/PreviewsTests.php @@ -0,0 +1,65 @@ +previews->allEmailClients(); + + $this->assertTrue(count($result->items) > 1); + } + + public function testGeneratePreviews() + { + if (empty(self::$server)) { $this->markTestSkipped(); } + + $randomString = Servers::randomString(10); + $host = ($h = getenv('MAILOSAUR_SMTP_HOST')) ? $h : 'mailosaur.net'; + $testEmailAddress = $randomString . '@' . self::$server . '.' . $host; + + Mailer::sendEmail(self::$client, self::$server, $testEmailAddress); + + $criteria = new SearchCriteria(); + $criteria->sentTo = $testEmailAddress; + + $email = self::$client->messages->get(self::$server, $criteria); + + $request = new PreviewRequest('OL2021'); + $options = new PreviewRequestOptions(array($request)); + + $result = self::$client->messages->generatePreviews($email->id, $options); + $this->assertTrue(count($result->items) > 0); + + $file = self::$client->files->getPreview($result->items[0]->id); + $this->assertNotNull($file); + } + +}