-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from railsware/feature/v.2.0.0
Upgrade SDK v.2.0 [BC BREAK]
- Loading branch information
Showing
69 changed files
with
1,162 additions
and
593 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,22 +33,21 @@ Here's how to send a message using the SDK: | |
```php | ||
<?php | ||
|
||
use Mailtrap\Config; | ||
use Mailtrap\EmailHeader\CategoryHeader; | ||
use Mailtrap\EmailHeader\CustomVariableHeader; | ||
use Mailtrap\Helper\ResponseHelper; | ||
use Mailtrap\MailtrapClient; | ||
use Mailtrap\Mime\MailtrapEmail; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Component\Mime\Email; | ||
use Symfony\Component\Mime\Header\UnstructuredHeader; | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
// your API token from here https://mailtrap.io/api-tokens | ||
$apiKey = getenv('MAILTRAP_API_KEY'); | ||
$mailtrap = new MailtrapClient(new Config($apiKey)); | ||
// Mailtrap SENDING client (real) for transactional emails | ||
$mailtrap = MailtrapClient::initSendingEmails( | ||
apiKey: getenv('MAILTRAP_API_KEY') # your API token from here https://mailtrap.io/api-tokens | ||
); | ||
|
||
$email = (new Email()) | ||
$email = (new MailtrapEmail()) | ||
->from(new Address('[email protected]', 'Mailtrap Test')) | ||
->replyTo(new Address('[email protected]')) | ||
->to(new Address('[email protected]', 'Jon')) | ||
|
@@ -69,42 +68,57 @@ $email = (new Email()) | |
</html>' | ||
) | ||
->embed(fopen('https://mailtrap.io/wp-content/uploads/2021/04/mailtrap-new-logo.svg', 'r'), 'logo', 'image/svg+xml') | ||
; | ||
|
||
// Headers | ||
$email->getHeaders() | ||
->category('Integration Test') | ||
->customVariables([ | ||
'user_id' => '45982', | ||
'batch_id' => 'PSJ-12' | ||
]) | ||
; | ||
|
||
// Custom email headers (optional) | ||
$email->getHeaders() | ||
->addTextHeader('X-Message-Source', 'domain.com') | ||
->add(new UnstructuredHeader('X-Mailer', 'Mailtrap PHP Client')) // the same as addTextHeader | ||
; | ||
|
||
// 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')) | ||
; | ||
|
||
; | ||
|
||
try { | ||
$response = $mailtrap->sending()->emails()->send($email); // Email sending API (real) | ||
$response = $mailtrap->send($email); | ||
|
||
var_dump(ResponseHelper::toArray($response)); // body (array) | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
// OR send email to the Mailtrap SANDBOX | ||
|
||
// OR -> Mailtrap BULK SENDING client (real) | ||
try { | ||
$mailtrapBulkSending = MailtrapClient::initSendingEmails( | ||
apiKey: getenv('MAILTRAP_API_KEY'), # your API token from here https://mailtrap.io/api-tokens | ||
isBulk: true # Bulk sending (@see https://help.mailtrap.io/article/113-sending-streams) | ||
); | ||
|
||
$response = $mailtrapBulkSending->send($email); | ||
|
||
var_dump(ResponseHelper::toArray($response)); // body (array) | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
// OR -> Mailtrap Testing client (sandbox) | ||
try { | ||
$response = $mailtrap->sandbox()->emails()->send($email, 1000001); // Required second param -> inbox_id | ||
$mailtrapTesting = MailtrapClient::initSendingEmails( | ||
apiKey: getenv('MAILTRAP_API_KEY'), # your API token from here https://mailtrap.io/api-tokens | ||
isSandbox: true, # Sandbox sending (@see https://help.mailtrap.io/article/109-getting-started-with-mailtrap-email-testing) | ||
inboxId: getenv('MAILTRAP_INBOX_ID') # required param for sandbox sending | ||
); | ||
|
||
$response = $mailtrapTesting->send($email); | ||
|
||
var_dump(ResponseHelper::toArray($response)); // body (array) | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
``` | ||
|
||
### All usage examples | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
UPGRADE FROM 1.x to 2.0 | ||
======================= | ||
|
||
### Email Layers | ||
* Added a short method to get one of the Email layers (Sending/Bulk/Sandbox) depending on config params `MailtrapClient::initSendingEmails()` | ||
* string $apiKey | ||
* bool $isBulk = false | ||
* bool $isSandbox = false | ||
* int $inboxId = null | ||
* **BC BREAK**: In Sandbox layer `inboxId` should be passed at the client level. | ||
|
||
__Before__: | ||
```php | ||
$mailtrap = new MailtrapClient(new Config(getenv('MAILTRAP_API_KEY'))); | ||
|
||
$response = $mailtrap | ||
->sandbox() | ||
->emails() | ||
->send($email, 1000001); # <--- inboxId here | ||
``` | ||
__After__: | ||
```php | ||
# short method using `initSendingEmails` | ||
$mailtrap = MailtrapClient::initSendingEmails( | ||
apiKey: getenv('MAILTRAP_API_KEY'), #your API token from here https://mailtrap.io/api-tokens | ||
isSandbox: true, # required param for sandbox sending | ||
inboxId: getenv('MAILTRAP_INBOX_ID') # <--- inboxId here | ||
); | ||
|
||
# or using the client directly (old variant) | ||
$mailtrap = (new MailtrapClient(new Config(getenv('MAILTRAP_API_KEY')))) | ||
->sandbox() | ||
->emails(getenv('MAILTRAP_INBOX_ID')); # <--- inboxId here | ||
|
||
$response = $mailtrap->send($email); | ||
``` | ||
|
||
### General API | ||
* **BC BREAK**: Rebuild `Accounts` & `Permissions` & `Users` layers ([examples](examples/general)) | ||
|
||
__Before__: | ||
```php | ||
$mailtrap = new MailtrapClient(new Config(getenv('MAILTRAP_API_KEY'))); # no changes here | ||
|
||
$response = $mailtrap | ||
->general() | ||
->permissions() | ||
->getResources(getenv('MAILTRAP_ACCOUNT_ID')); # <--- accountId here | ||
|
||
$response = $mailtrap | ||
->general() | ||
->users() | ||
->getList(getenv('MAILTRAP_ACCOUNT_ID')); # <--- accountId here | ||
``` | ||
__After__: | ||
```php | ||
// all permissions endpoints | ||
$response = $mailtrap | ||
->general() | ||
->permissions(getenv('MAILTRAP_ACCOUNT_ID')) # <--- accountId here | ||
->getResources(); | ||
|
||
// all users endpoints | ||
$response = $mailtrap | ||
->general() | ||
->users(getenv('MAILTRAP_ACCOUNT_ID')) # <--- accountId here | ||
->getList(); | ||
``` | ||
|
||
### Sandbox API | ||
* **BC BREAK**: Rebuild `Projects` & `Messages` & `Attachments` & `Inboxes` layers ([examples](examples/testing)) | ||
|
||
__Before__: | ||
```php | ||
$mailtrap = new MailtrapClient(new Config(getenv('MAILTRAP_API_KEY'))); # no changes here | ||
|
||
$response = $mailtrap | ||
->sandbox() | ||
->inboxes() | ||
->getList(getenv('MAILTRAP_ACCOUNT_ID')); # <--- accountId here | ||
``` | ||
__After__: | ||
```php | ||
// all sandbox(testing) endpoints: projects, messages, attachments, inboxes | ||
$response = $mailtrap | ||
->sandbox() | ||
->inboxes(getenv('MAILTRAP_ACCOUNT_ID')) # <--- accountId here | ||
->getList(); | ||
``` | ||
|
||
### Email Template class | ||
* Added `MailtrapEmail` wrapper (MIME) for easy use category, custom variables, template uuid, etc. | ||
|
||
__Before__: | ||
```php | ||
$email = (new Email()) | ||
->from(new Address('[email protected]', '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('[email protected]')) | ||
->to(new Address('[email protected]', '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')) | ||
; | ||
``` | ||
|
||
__After__: | ||
```php | ||
use Mailtrap\Mime\MailtrapEmail; | ||
|
||
$email = (new MailtrapEmail()) # <--- new MIME class with template support | ||
->from(new Address('[email protected]', '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('[email protected]')) | ||
->to(new Address('[email protected]', 'Jon')) | ||
->templateUuid('bfa432fd-0000-0000-0000-8493da283a69') | ||
->templateVariables([ | ||
'user_name' => 'Jon Bush', | ||
'next_step_link' => 'https://mailtrap.io/', | ||
'get_started_link' => 'https://mailtrap.io/', | ||
'onboarding_video_link' => 'some_video_link' | ||
]) | ||
; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.