Skip to content

Commit

Permalink
Merge pull request #25 from railsware/feature/templates-in-testing
Browse files Browse the repository at this point in the history
Support templates in testing
  • Loading branch information
gaalferov authored May 3, 2024
2 parents d79e011 + 1431a45 commit a4e20f0
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 92 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [1.9.0] - 2024-05-06

- Support templates in testing
- 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)
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 0 additions & 79 deletions examples/sandbox/emails.php

This file was deleted.

100 changes: 100 additions & 0 deletions examples/sending/emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<html>
<body>
<p><br>Hey</br>
Learn the best practices of building HTML emails and play with ready-to-go templates.</p>
<p><a href="https://mailtrap.io/blog/build-html-email/">Mailtrap’s Guide on How to Build HTML Email</a> is live on our blog</p>
<img src="cid:logo">
</body>
</html>'
)
->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";
}
File renamed without changes.
27 changes: 16 additions & 11 deletions examples/bulkSending/emails.php → examples/testing/emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@

require __DIR__ . '/../vendor/autoload.php';


/**********************************************************************************************************************
******************************************* EMAIL BULK SENDING *******************************************************
******************************************* EMAIL TESTING ************************************************************
**********************************************************************************************************************
*/

/**
* Email Bulk Sending API
* Email Testing API
*
* POST https://bulk.api.mailtrap.io/api/send
* POST https://sandbox.api.mailtrap.io/api/send/{inbox_id}
*/
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'))
->from(new Address('mailtrap@example.com', 'Mailtrap Test'))
->replyTo(new Address('reply@example.com'))
->to(new Address('email@example.com', 'Jon'))
->priority(Email::PRIORITY_HIGH)
->cc('mailtrapqa@example.com')
->addCc('staging@example.com')
->bcc('mailtrapdev@example.com')
Expand Down Expand Up @@ -69,18 +69,22 @@
->add(new CategoryHeader('Integration Test'))
;

$response = $mailtrap->bulkSending()->emails()->send($email);
// Required param -> inbox_id
$response = $mailtrap->sandbox()->emails()->send($email, 1000001); // <--- you should use your inbox_id here (otherwise you will get 401)

// print all possible information from the response
var_dump($response->getHeaders()); //headers (array)
var_dump($response->getStatusCode()); //status code (int)
var_dump(ResponseHelper::toArray($response)); // body (array)
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Email Bulk Sending WITH TEMPLATE
* Test Email WITH TEMPLATE
*
* WARNING! If a template is provided, then subject, text, html, category and other params are forbidden.
* 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
Expand All @@ -91,7 +95,7 @@
$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)
->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'))
;
Expand All @@ -105,7 +109,8 @@
->add(new TemplateVariableHeader('onboarding_video_link', 'some_video_link'))
;

$response = $mailtrap->bulkSending()->emails()->send($email);
// 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) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 58 additions & 0 deletions tests/Api/Sandbox/EmailsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,4 +101,60 @@ public function testValidSendToSandBox(): void
$this->assertArrayHasKey('success', $responseData);
$this->assertArrayHasKey('message_ids', $responseData);
}

public function testValidSendTemplateToSandbox(): 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);
}
}

0 comments on commit a4e20f0

Please sign in to comment.