From 9c832db70b4c23777cfb59aa8ed6e9ca2765e743 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Tue, 30 Apr 2024 14:12:00 +0200 Subject: [PATCH 1/3] Refactoring of examples --- CHANGELOG.md | 6 + README.md | 3 +- examples/bulkSending/emails.php | 113 ------------------ examples/sending/emails.php | 100 ++++++++++++++++ examples/{sandbox => testing}/attachments.php | 0 examples/{sandbox => testing}/emails.php | 0 examples/{sandbox => testing}/inboxes.php | 0 examples/{sandbox => testing}/messages.php | 0 examples/{sandbox => testing}/projects.php | 0 9 files changed, 107 insertions(+), 115 deletions(-) delete mode 100644 examples/bulkSending/emails.php rename examples/{sandbox => testing}/attachments.php (100%) rename examples/{sandbox => testing}/emails.php (100%) rename examples/{sandbox => testing}/inboxes.php (100%) rename examples/{sandbox => testing}/messages.php (100%) rename examples/{sandbox => testing}/projects.php (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc5a339..9d0628e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [1.9.0] - 2024-05-06 + +- Refactoring of examples + - sandbox -> [testing](examples/testing) + - bulkSending -> [sending](examples/sending) + ## [1.8.1] - 2024-04-25 - Use real value for template headers (should not be encoded as it is not a real header) diff --git a/README.md b/README.md index c3cacb4..4b8a9f3 100644 --- a/README.md +++ b/README.md @@ -111,9 +111,8 @@ try { You can find more examples [here](examples). * [General examples](examples/general) +* [Testing examples](examples/testing) * [Sending examples](examples/sending) -* [Bulk Sending examples](examples/bulkSending) -* [Sandbox examples](examples/sandbox) ## Framework integration diff --git a/examples/bulkSending/emails.php b/examples/bulkSending/emails.php deleted file mode 100644 index 97ca514..0000000 --- a/examples/bulkSending/emails.php +++ /dev/null @@ -1,113 +0,0 @@ -from(new Address('example@YOUR-DOMAIN-HERE.com', 'Mailtrap Test')) // <--- you should use your domain here that you installed in the mailtrap.io admin area (otherwise you will get 401) - ->replyTo(new Address('reply@YOUR-DOMAIN-HERE.com')) - ->to(new Address('email@example.com', 'Jon')) - ->priority(Email::PRIORITY_HIGH) - ->cc('mailtrapqa@example.com') - ->addCc('staging@example.com') - ->bcc('mailtrapdev@example.com') - ->subject('Best practices of building HTML emails') - ->text('Hey! Learn the best practices of building HTML emails and play with ready-to-go templates. Mailtrap’s Guide on How to Build HTML Email is live on our blog') - ->html( - ' - -


Hey
- Learn the best practices of building HTML emails and play with ready-to-go templates.

-

Mailtrap’s Guide on How to Build HTML Email is live on our blog

- - - ' - ) - ->embed(fopen('https://mailtrap.io/wp-content/uploads/2021/04/mailtrap-new-logo.svg', 'r'), 'logo', 'image/svg+xml') - ->attachFromPath('README.md') - ; - - // Headers - $email->getHeaders() - ->addTextHeader('X-Message-Source', '1alf.com') - ->add(new UnstructuredHeader('X-Mailer', 'Mailtrap PHP Client')) - ; - - // Custom Variables - $email->getHeaders() - ->add(new CustomVariableHeader('user_id', '45982')) - ->add(new CustomVariableHeader('batch_id', 'PSJ-12')) - ; - - // Category (should be only one) - $email->getHeaders() - ->add(new CategoryHeader('Integration Test')) - ; - - $response = $mailtrap->bulkSending()->emails()->send($email); - - var_dump(ResponseHelper::toArray($response)); // body (array) -} catch (Exception $e) { - echo 'Caught exception: ', $e->getMessage(), "\n"; -} - - -/** - * Email Bulk Sending WITH TEMPLATE - * - * WARNING! If a template is provided, then subject, text, html, category and other params are forbidden. - * - * UUID of email template. Subject, text and html will be generated from template using optional template_variables. - * Optional template variables that will be used to generate actual subject, text and html from email template - */ -try { - // your API token from here https://mailtrap.io/api-tokens - $apiKey = getenv('MAILTRAP_API_KEY'); - $mailtrap = new MailtrapClient(new Config($apiKey)); - - $email = (new Email()) - ->from(new Address('example@YOUR-DOMAIN-HERE.com', 'Mailtrap Test')) // <--- you should use your domain here that you installed in the mailtrap.io admin area (otherwise you will get 401) - ->replyTo(new Address('reply@YOUR-DOMAIN-HERE.com')) - ->to(new Address('example@gmail.com', 'Jon')) - ; - - // Template UUID and Variables - $email->getHeaders() - ->add(new TemplateUuidHeader('bfa432fd-0000-0000-0000-8493da283a69')) - ->add(new TemplateVariableHeader('user_name', 'Jon Bush')) - ->add(new TemplateVariableHeader('next_step_link', 'https://mailtrap.io/')) - ->add(new TemplateVariableHeader('get_started_link', 'https://mailtrap.io/')) - ->add(new TemplateVariableHeader('onboarding_video_link', 'some_video_link')) - ; - - $response = $mailtrap->bulkSending()->emails()->send($email); - - var_dump(ResponseHelper::toArray($response)); // body (array) -} catch (Exception $e) { - echo 'Caught exception: ', $e->getMessage(), "\n"; -} diff --git a/examples/sending/emails.php b/examples/sending/emails.php index 7c4b5f2..2902063 100644 --- a/examples/sending/emails.php +++ b/examples/sending/emails.php @@ -112,3 +112,103 @@ } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } + + +/********************************************************************************************************************** + ******************************************* EMAIL BULK SENDING ******************************************************* + ********************************************************************************************************************** + */ + +/** + * Email Bulk Sending API + * + * POST https://bulk.api.mailtrap.io/api/send + */ +try { + // your API token from here https://mailtrap.io/api-tokens + $apiKey = getenv('MAILTRAP_API_KEY'); + $mailtrap = new MailtrapClient(new Config($apiKey)); + + $email = (new Email()) + ->from(new Address('example@YOUR-DOMAIN-HERE.com', 'Mailtrap Test')) // <--- you should use your domain here that you installed in the mailtrap.io admin area (otherwise you will get 401) + ->replyTo(new Address('reply@YOUR-DOMAIN-HERE.com')) + ->to(new Address('email@example.com', 'Jon')) + ->priority(Email::PRIORITY_HIGH) + ->cc('mailtrapqa@example.com') + ->addCc('staging@example.com') + ->bcc('mailtrapdev@example.com') + ->subject('Best practices of building HTML emails') + ->text('Hey! Learn the best practices of building HTML emails and play with ready-to-go templates. Mailtrap’s Guide on How to Build HTML Email is live on our blog') + ->html( + ' + +


Hey
+ Learn the best practices of building HTML emails and play with ready-to-go templates.

+

Mailtrap’s Guide on How to Build HTML Email is live on our blog

+ + + ' + ) + ->embed(fopen('https://mailtrap.io/wp-content/uploads/2021/04/mailtrap-new-logo.svg', 'r'), 'logo', 'image/svg+xml') + ->attachFromPath('README.md') + ; + + // Headers + $email->getHeaders() + ->addTextHeader('X-Message-Source', '1alf.com') + ->add(new UnstructuredHeader('X-Mailer', 'Mailtrap PHP Client')) + ; + + // Custom Variables + $email->getHeaders() + ->add(new CustomVariableHeader('user_id', '45982')) + ->add(new CustomVariableHeader('batch_id', 'PSJ-12')) + ; + + // Category (should be only one) + $email->getHeaders() + ->add(new CategoryHeader('Integration Test')) + ; + + $response = $mailtrap->bulkSending()->emails()->send($email); + + var_dump(ResponseHelper::toArray($response)); // body (array) +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} + + +/** + * Email Bulk Sending WITH TEMPLATE + * + * WARNING! If a template is provided, then subject, text, html, category and other params are forbidden. + * + * UUID of email template. Subject, text and html will be generated from template using optional template_variables. + * Optional template variables that will be used to generate actual subject, text and html from email template + */ +try { + // your API token from here https://mailtrap.io/api-tokens + $apiKey = getenv('MAILTRAP_API_KEY'); + $mailtrap = new MailtrapClient(new Config($apiKey)); + + $email = (new Email()) + ->from(new Address('example@YOUR-DOMAIN-HERE.com', 'Mailtrap Test')) // <--- you should use your domain here that you installed in the mailtrap.io admin area (otherwise you will get 401) + ->replyTo(new Address('reply@YOUR-DOMAIN-HERE.com')) + ->to(new Address('example@gmail.com', 'Jon')) + ; + + // Template UUID and Variables + $email->getHeaders() + ->add(new TemplateUuidHeader('bfa432fd-0000-0000-0000-8493da283a69')) + ->add(new TemplateVariableHeader('user_name', 'Jon Bush')) + ->add(new TemplateVariableHeader('next_step_link', 'https://mailtrap.io/')) + ->add(new TemplateVariableHeader('get_started_link', 'https://mailtrap.io/')) + ->add(new TemplateVariableHeader('onboarding_video_link', 'some_video_link')) + ; + + $response = $mailtrap->bulkSending()->emails()->send($email); + + var_dump(ResponseHelper::toArray($response)); // body (array) +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} diff --git a/examples/sandbox/attachments.php b/examples/testing/attachments.php similarity index 100% rename from examples/sandbox/attachments.php rename to examples/testing/attachments.php diff --git a/examples/sandbox/emails.php b/examples/testing/emails.php similarity index 100% rename from examples/sandbox/emails.php rename to examples/testing/emails.php diff --git a/examples/sandbox/inboxes.php b/examples/testing/inboxes.php similarity index 100% rename from examples/sandbox/inboxes.php rename to examples/testing/inboxes.php diff --git a/examples/sandbox/messages.php b/examples/testing/messages.php similarity index 100% rename from examples/sandbox/messages.php rename to examples/testing/messages.php diff --git a/examples/sandbox/projects.php b/examples/testing/projects.php similarity index 100% rename from examples/sandbox/projects.php rename to examples/testing/projects.php From f877acc6c6213dce45cd79fbecec5c4a62742834 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Tue, 30 Apr 2024 20:15:21 +0200 Subject: [PATCH 2/3] add examples and tests --- examples/testing/emails.php | 39 +++++++++++++++++++++ tests/Api/Sandbox/EmailsTest.php | 58 ++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/examples/testing/emails.php b/examples/testing/emails.php index e0d8110..94d029a 100644 --- a/examples/testing/emails.php +++ b/examples/testing/emails.php @@ -3,6 +3,8 @@ use Mailtrap\Config; use Mailtrap\EmailHeader\CategoryHeader; use Mailtrap\EmailHeader\CustomVariableHeader; +use Mailtrap\EmailHeader\Template\TemplateUuidHeader; +use Mailtrap\EmailHeader\Template\TemplateVariableHeader; use Mailtrap\Helper\ResponseHelper; use Mailtrap\MailtrapClient; use Symfony\Component\Mime\Address; @@ -77,3 +79,40 @@ } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } + + +/** + * Test Email WITH TEMPLATE + * + * WARNING! If template is provided then subject, text, html, category and other params are forbidden. + * + * UUID of email template. Subject, text and html will be generated from template using optional template_variables. + * Optional template variables that will be used to generate actual subject, text and html from email template + */ +try { + // your API token from here https://mailtrap.io/api-tokens + $apiKey = getenv('MAILTRAP_API_KEY'); + $mailtrap = new MailtrapClient(new Config($apiKey)); + + $email = (new Email()) + ->from(new Address('example@YOUR-DOMAIN-HERE.com', 'Mailtrap Test')) // <--- you should use the domain which is linked to template UUID (otherwise you will get 401) + ->replyTo(new Address('reply@YOUR-DOMAIN-HERE.com')) + ->to(new Address('example@gmail.com', 'Jon')) + ; + + // Template UUID and Variables + $email->getHeaders() + ->add(new TemplateUuidHeader('bfa432fd-0000-0000-0000-8493da283a69')) + ->add(new TemplateVariableHeader('user_name', 'Jon Bush')) + ->add(new TemplateVariableHeader('next_step_link', 'https://mailtrap.io/')) + ->add(new TemplateVariableHeader('get_started_link', 'https://mailtrap.io/')) + ->add(new TemplateVariableHeader('onboarding_video_link', 'some_video_link')) + ; + + // Required param -> inbox_id + $response = $mailtrap->sandbox()->emails()->send($email, 1000001); // <--- you should use your inbox_id here (otherwise you will get 401) + + var_dump(ResponseHelper::toArray($response)); // body (array) +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} diff --git a/tests/Api/Sandbox/EmailsTest.php b/tests/Api/Sandbox/EmailsTest.php index e331ed9..d4c2404 100644 --- a/tests/Api/Sandbox/EmailsTest.php +++ b/tests/Api/Sandbox/EmailsTest.php @@ -6,6 +6,8 @@ use Mailtrap\Api\AbstractApi; use Mailtrap\Api\Sandbox\Emails; +use Mailtrap\EmailHeader\Template\TemplateUuidHeader; +use Mailtrap\EmailHeader\Template\TemplateVariableHeader; use Mailtrap\Helper\ResponseHelper; use Mailtrap\Tests\MailtrapTestCase; use Nyholm\Psr7\Response; @@ -99,4 +101,60 @@ public function testValidSendToSandBox(): void $this->assertArrayHasKey('success', $responseData); $this->assertArrayHasKey('message_ids', $responseData); } + + public function testValidSendTemplate(): void + { + $inboxId = 1000001; + $expectedData = [ + "success" => true, + "message_ids" => [ + "0c7fd939-02cf-11ed-88c2-0a58a9feac02" + ] + ]; + + $email = (new Email()) + ->from(new Address('foo@example.com', 'Ms. Foo Bar')) + ->to(new Address('bar@example.com', 'Mr. Recipient')) + ; + + $email->getHeaders() + ->add(new TemplateUuidHeader('bfa432fd-0000-0000-0000-8493da283a69')) + ->add(new TemplateVariableHeader('user_name', 'Jon Bush')) + ->add(new TemplateVariableHeader('unicode_user_name', 'Subašić')) + ->add(new TemplateVariableHeader('next_step_link', 'https://mailtrap.io/')) + ->add(new TemplateVariableHeader('get_started_link', 'https://mailtrap.io/')) + ->add(new TemplateVariableHeader('onboarding_video_link', 'some_video_link')) + ; + + $this->email + ->expects($this->once()) + ->method('httpPost') + ->with(AbstractApi::SENDMAIL_SANDBOX_HOST . '/api/send/' . $inboxId, [], [ + 'from' => [ + 'email' => 'foo@example.com', + 'name' => 'Ms. Foo Bar', + ], + 'to' => [[ + 'email' => 'bar@example.com', + 'name' => 'Mr. Recipient', + ]], + 'template_uuid' => 'bfa432fd-0000-0000-0000-8493da283a69', + 'template_variables' => [ + 'user_name' => 'Jon Bush', + 'unicode_user_name' => 'Subašić', // should not be encoded as it is not a real header + 'next_step_link' => 'https://mailtrap.io/', + 'get_started_link' => 'https://mailtrap.io/', + 'onboarding_video_link' => 'some_video_link', + ] + ]) + ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($expectedData))); + + $response = $this->email->send($email, $inboxId); + $responseData = ResponseHelper::toArray($response); + + $this->assertInstanceOf(Response::class, $response); + $this->assertCount(2, $responseData); + $this->assertArrayHasKey('success', $responseData); + $this->assertArrayHasKey('message_ids', $responseData); + } } From 1431a451e8ac1775ae3ad0db686a6c88ade8fc76 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Tue, 30 Apr 2024 20:29:16 +0200 Subject: [PATCH 3/3] update CHANGELOG --- CHANGELOG.md | 1 + tests/Api/Sandbox/EmailsTest.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d0628e..ef56be2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## [1.9.0] - 2024-05-06 +- Support templates in testing - Refactoring of examples - sandbox -> [testing](examples/testing) - bulkSending -> [sending](examples/sending) diff --git a/tests/Api/Sandbox/EmailsTest.php b/tests/Api/Sandbox/EmailsTest.php index d4c2404..0b87bd7 100644 --- a/tests/Api/Sandbox/EmailsTest.php +++ b/tests/Api/Sandbox/EmailsTest.php @@ -102,7 +102,7 @@ public function testValidSendToSandBox(): void $this->assertArrayHasKey('message_ids', $responseData); } - public function testValidSendTemplate(): void + public function testValidSendTemplateToSandbox(): void { $inboxId = 1000001; $expectedData = [