Skip to content

Commit 8021b0b

Browse files
Merge pull request #27 from railsware/feature/v.2.0.0
Upgrade SDK v.2.0 [BC BREAK]
2 parents a4e20f0 + b159619 commit 8021b0b

File tree

69 files changed

+1162
-593
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1162
-593
lines changed

Diff for: .github/workflows/ci-phpunit.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ on:
88
- 'release/**'
99
- 'feature/**'
1010
- 'main'
11+
schedule:
12+
- cron: '0 0 * * *' # Runs every day at midnight UTC
1113

1214
jobs:
1315
phpunit-tests:
@@ -16,7 +18,7 @@ jobs:
1618
strategy:
1719
matrix:
1820
os: [ ubuntu-latest ]
19-
php-version: [ '7.4','8.0','8.1','8.2','8.3' ]
21+
php-version: [ '8.0','8.1','8.2','8.3' ]
2022

2123
steps:
2224
- name: Checkout
@@ -58,8 +60,6 @@ jobs:
5860
strategy:
5961
matrix:
6062
include:
61-
- symfony: '5'
62-
php-version: '7.4'
6363
- symfony: '6'
6464
php-version: '8.2'
6565
- symfony: '7'

Diff for: .github/workflows/ci-psalm.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
matrix:
1717
os: [ ubuntu-latest ]
18-
php-versions: [ '7.4','8.0','8.1','8.2','8.3' ]
18+
php-versions: [ '8.0','8.1','8.2','8.3' ]
1919

2020
steps:
2121
- name: Checkout

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ phpunit.xml
1111
###> files for local testing ###
1212
test.php
1313
docker-compose.yaml
14-
Dockerfile
14+
Dockerfile*
1515
###> files for local testing ###
1616

1717
###> other ###

Diff for: CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## [2.0.0] - 2024-06-12
2+
- [BC BREAK] PHP 7.4 will no longer be supported (PHP 8.0+).
3+
- [BC BREAK] Rebuild `Emails` layers to use the `inboxId` at the client level ([examples](examples/testing/emails.php))
4+
- [BC BREAK] Rebuild SANDBOX `Projects` & `Messages` & `Attachments` & `Inboxes` layers ([examples](examples/testing))
5+
- [BC BREAK] Rebuild GENERAL `Accounts` & `Permissions` & `Users` layers ([examples](examples/general))
6+
- Added a short method to get the Email layer depending on config params `MailtrapClient::initSendingEmails()`
7+
- Added MailtrapEmail wrapper (MIME) for easy add category, custom variables, template uuid, etc.
8+
19
## [1.9.0] - 2024-05-06
210

311
- Support templates in testing

Diff for: README.md

+42-28
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,21 @@ Here's how to send a message using the SDK:
3333
```php
3434
<?php
3535

36-
use Mailtrap\Config;
37-
use Mailtrap\EmailHeader\CategoryHeader;
38-
use Mailtrap\EmailHeader\CustomVariableHeader;
3936
use Mailtrap\Helper\ResponseHelper;
4037
use Mailtrap\MailtrapClient;
38+
use Mailtrap\Mime\MailtrapEmail;
4139
use Symfony\Component\Mime\Address;
4240
use Symfony\Component\Mime\Email;
4341
use Symfony\Component\Mime\Header\UnstructuredHeader;
4442

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

47-
// your API token from here https://mailtrap.io/api-tokens
48-
$apiKey = getenv('MAILTRAP_API_KEY');
49-
$mailtrap = new MailtrapClient(new Config($apiKey));
45+
// Mailtrap SENDING client (real) for transactional emails
46+
$mailtrap = MailtrapClient::initSendingEmails(
47+
apiKey: getenv('MAILTRAP_API_KEY') # your API token from here https://mailtrap.io/api-tokens
48+
);
5049

51-
$email = (new Email())
50+
$email = (new MailtrapEmail())
5251
->from(new Address('[email protected]', 'Mailtrap Test'))
5352
->replyTo(new Address('[email protected]'))
5453
->to(new Address('[email protected]', 'Jon'))
@@ -69,42 +68,57 @@ $email = (new Email())
6968
</html>'
7069
)
7170
->embed(fopen('https://mailtrap.io/wp-content/uploads/2021/04/mailtrap-new-logo.svg', 'r'), 'logo', 'image/svg+xml')
72-
;
73-
74-
// Headers
75-
$email->getHeaders()
71+
->category('Integration Test')
72+
->customVariables([
73+
'user_id' => '45982',
74+
'batch_id' => 'PSJ-12'
75+
])
76+
;
77+
78+
// Custom email headers (optional)
79+
$email->getHeaders()
7680
->addTextHeader('X-Message-Source', 'domain.com')
7781
->add(new UnstructuredHeader('X-Mailer', 'Mailtrap PHP Client')) // the same as addTextHeader
78-
;
79-
80-
// Custom Variables
81-
$email->getHeaders()
82-
->add(new CustomVariableHeader('user_id', '45982'))
83-
->add(new CustomVariableHeader('batch_id', 'PSJ-12'))
84-
;
85-
86-
// Category (should be only one)
87-
$email->getHeaders()
88-
->add(new CategoryHeader('Integration Test'))
89-
;
90-
82+
;
83+
9184
try {
92-
$response = $mailtrap->sending()->emails()->send($email); // Email sending API (real)
93-
85+
$response = $mailtrap->send($email);
86+
9487
var_dump(ResponseHelper::toArray($response)); // body (array)
9588
} catch (Exception $e) {
9689
echo 'Caught exception: ', $e->getMessage(), "\n";
9790
}
9891

99-
// OR send email to the Mailtrap SANDBOX
10092

93+
// OR -> Mailtrap BULK SENDING client (real)
94+
try {
95+
$mailtrapBulkSending = MailtrapClient::initSendingEmails(
96+
apiKey: getenv('MAILTRAP_API_KEY'), # your API token from here https://mailtrap.io/api-tokens
97+
isBulk: true # Bulk sending (@see https://help.mailtrap.io/article/113-sending-streams)
98+
);
99+
100+
$response = $mailtrapBulkSending->send($email);
101+
102+
var_dump(ResponseHelper::toArray($response)); // body (array)
103+
} catch (Exception $e) {
104+
echo 'Caught exception: ', $e->getMessage(), "\n";
105+
}
106+
107+
// OR -> Mailtrap Testing client (sandbox)
101108
try {
102-
$response = $mailtrap->sandbox()->emails()->send($email, 1000001); // Required second param -> inbox_id
109+
$mailtrapTesting = MailtrapClient::initSendingEmails(
110+
apiKey: getenv('MAILTRAP_API_KEY'), # your API token from here https://mailtrap.io/api-tokens
111+
isSandbox: true, # Sandbox sending (@see https://help.mailtrap.io/article/109-getting-started-with-mailtrap-email-testing)
112+
inboxId: getenv('MAILTRAP_INBOX_ID') # required param for sandbox sending
113+
);
114+
115+
$response = $mailtrapTesting->send($email);
103116

104117
var_dump(ResponseHelper::toArray($response)); // body (array)
105118
} catch (Exception $e) {
106119
echo 'Caught exception: ', $e->getMessage(), "\n";
107120
}
121+
108122
```
109123

110124
### All usage examples

Diff for: UPGRADE-2.0.md

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
UPGRADE FROM 1.x to 2.0
2+
=======================
3+
4+
### Email Layers
5+
* Added a short method to get one of the Email layers (Sending/Bulk/Sandbox) depending on config params `MailtrapClient::initSendingEmails()`
6+
* string $apiKey
7+
* bool $isBulk = false
8+
* bool $isSandbox = false
9+
* int $inboxId = null
10+
* **BC BREAK**: In Sandbox layer `inboxId` should be passed at the client level.
11+
12+
__Before__:
13+
```php
14+
$mailtrap = new MailtrapClient(new Config(getenv('MAILTRAP_API_KEY')));
15+
16+
$response = $mailtrap
17+
->sandbox()
18+
->emails()
19+
->send($email, 1000001); # <--- inboxId here
20+
```
21+
__After__:
22+
```php
23+
# short method using `initSendingEmails`
24+
$mailtrap = MailtrapClient::initSendingEmails(
25+
apiKey: getenv('MAILTRAP_API_KEY'), #your API token from here https://mailtrap.io/api-tokens
26+
isSandbox: true, # required param for sandbox sending
27+
inboxId: getenv('MAILTRAP_INBOX_ID') # <--- inboxId here
28+
);
29+
30+
# or using the client directly (old variant)
31+
$mailtrap = (new MailtrapClient(new Config(getenv('MAILTRAP_API_KEY'))))
32+
->sandbox()
33+
->emails(getenv('MAILTRAP_INBOX_ID')); # <--- inboxId here
34+
35+
$response = $mailtrap->send($email);
36+
```
37+
38+
### General API
39+
* **BC BREAK**: Rebuild `Accounts` & `Permissions` & `Users` layers ([examples](examples/general))
40+
41+
__Before__:
42+
```php
43+
$mailtrap = new MailtrapClient(new Config(getenv('MAILTRAP_API_KEY'))); # no changes here
44+
45+
$response = $mailtrap
46+
->general()
47+
->permissions()
48+
->getResources(getenv('MAILTRAP_ACCOUNT_ID')); # <--- accountId here
49+
50+
$response = $mailtrap
51+
->general()
52+
->users()
53+
->getList(getenv('MAILTRAP_ACCOUNT_ID')); # <--- accountId here
54+
```
55+
__After__:
56+
```php
57+
// all permissions endpoints
58+
$response = $mailtrap
59+
->general()
60+
->permissions(getenv('MAILTRAP_ACCOUNT_ID')) # <--- accountId here
61+
->getResources();
62+
63+
// all users endpoints
64+
$response = $mailtrap
65+
->general()
66+
->users(getenv('MAILTRAP_ACCOUNT_ID')) # <--- accountId here
67+
->getList();
68+
```
69+
70+
### Sandbox API
71+
* **BC BREAK**: Rebuild `Projects` & `Messages` & `Attachments` & `Inboxes` layers ([examples](examples/testing))
72+
73+
__Before__:
74+
```php
75+
$mailtrap = new MailtrapClient(new Config(getenv('MAILTRAP_API_KEY'))); # no changes here
76+
77+
$response = $mailtrap
78+
->sandbox()
79+
->inboxes()
80+
->getList(getenv('MAILTRAP_ACCOUNT_ID')); # <--- accountId here
81+
```
82+
__After__:
83+
```php
84+
// all sandbox(testing) endpoints: projects, messages, attachments, inboxes
85+
$response = $mailtrap
86+
->sandbox()
87+
->inboxes(getenv('MAILTRAP_ACCOUNT_ID')) # <--- accountId here
88+
->getList();
89+
```
90+
91+
### Email Template class
92+
* Added `MailtrapEmail` wrapper (MIME) for easy use category, custom variables, template uuid, etc.
93+
94+
__Before__:
95+
```php
96+
$email = (new Email())
97+
->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)
98+
->replyTo(new Address('[email protected]'))
99+
->to(new Address('[email protected]', 'Jon'))
100+
;
101+
102+
// Template UUID and Variables
103+
$email->getHeaders()
104+
->add(new TemplateUuidHeader('bfa432fd-0000-0000-0000-8493da283a69'))
105+
->add(new TemplateVariableHeader('user_name', 'Jon Bush'))
106+
->add(new TemplateVariableHeader('next_step_link', 'https://mailtrap.io/'))
107+
->add(new TemplateVariableHeader('get_started_link', 'https://mailtrap.io/'))
108+
->add(new TemplateVariableHeader('onboarding_video_link', 'some_video_link'))
109+
;
110+
```
111+
112+
__After__:
113+
```php
114+
use Mailtrap\Mime\MailtrapEmail;
115+
116+
$email = (new MailtrapEmail()) # <--- new MIME class with template support
117+
->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)
118+
->replyTo(new Address('[email protected]'))
119+
->to(new Address('[email protected]', 'Jon'))
120+
->templateUuid('bfa432fd-0000-0000-0000-8493da283a69')
121+
->templateVariables([
122+
'user_name' => 'Jon Bush',
123+
'next_step_link' => 'https://mailtrap.io/',
124+
'get_started_link' => 'https://mailtrap.io/',
125+
'onboarding_video_link' => 'some_video_link'
126+
])
127+
;
128+
```

Diff for: composer.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"homepage": "https://github.com/railsware/mailtrap-php",
66
"license": "MIT",
77
"require": {
8-
"php": "^7.4 || ^8.0",
8+
"php": "^8.0",
99
"ext-curl": "*",
1010
"ext-json": "*",
1111
"psr/http-message": "^1.0 || ^2.0",
@@ -14,15 +14,15 @@
1414
"php-http/httplug": "^2.0",
1515
"php-http/discovery": "^1.0",
1616
"php-http/message-factory": "^1.0",
17-
"symfony/mime": "^5.4|^6.0|^7.0",
17+
"symfony/mime": "^6.0|^7.0",
1818
"egulias/email-validator": "^2.1.10|^3.1|^4"
1919
},
2020
"require-dev": {
21-
"symfony/http-client": "^5.4|^6.0|^7.0",
22-
"symfony/mailer": "^5.4|^6.0|^7.0",
21+
"symfony/http-client": "^6.0|^7.0",
22+
"symfony/mailer": "^6.0|^7.0",
2323
"phpunit/phpunit": "^9",
2424
"nyholm/psr7": "^1.5",
25-
"vimeo/psalm": "^4.0 || ^5.0"
25+
"vimeo/psalm": "^5.0"
2626
},
2727
"suggest": {
2828
"nyholm/psr7": "PSR-7 message implementation",

Diff for: examples/general/accounts.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22

33
use Mailtrap\Config;
44
use Mailtrap\Helper\ResponseHelper;
5-
use Mailtrap\MailtrapClient;
5+
use Mailtrap\MailtrapGeneralClient;
66

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

9-
// your API token from here https://mailtrap.io/api-tokens
10-
$apiKey = getenv('MAILTRAP_API_KEY');
11-
$mailtrap = new MailtrapClient(new Config($apiKey));
9+
$config = new Config(getenv('MAILTRAP_API_KEY')); #your API token from here https://mailtrap.io/api-tokens
10+
$generalAccounts = (new MailtrapGeneralClient($config))->accounts();
1211

1312
/**
1413
* Get a list of your Mailtrap accounts.
1514
*
1615
* GET https://mailtrap.io/api/accounts
1716
*/
1817
try {
19-
$response = $mailtrap->general()->accounts()->getList();
18+
$response = $generalAccounts->getList();
2019

2120
var_dump(ResponseHelper::toArray($response)); // body (array)
2221
} catch (Exception $e) {

Diff for: examples/general/permissions.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,21 @@
66
use Mailtrap\DTO\Request\Permission\PermissionInterface;
77
use Mailtrap\DTO\Request\Permission\Permissions;
88
use Mailtrap\Helper\ResponseHelper;
9-
use Mailtrap\MailtrapClient;
9+
use Mailtrap\MailtrapGeneralClient;
1010

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

13-
// your API token from here https://mailtrap.io/api-tokens
14-
$apiKey = getenv('MAILTRAP_API_KEY');
15-
$mailtrap = new MailtrapClient(new Config($apiKey));
13+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
14+
$config = new Config(getenv('MAILTRAP_API_KEY')); #your API token from here https://mailtrap.io/api-tokens
15+
$generalPermissions = (new MailtrapGeneralClient($config))->permissions($accountId);
1616

1717
/**
1818
* Get resources
1919
*
2020
* GET https://mailtrap.io/api/accounts/{account_id}/permissions/resources
2121
*/
2222
try {
23-
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
24-
$response = $mailtrap->general()->permissions()->getResources($accountId);
23+
$response = $generalPermissions->getResources();
2524

2625
// print the response body (array)
2726
var_dump(ResponseHelper::toArray($response));
@@ -39,7 +38,6 @@
3938
* PUT https://mailtrap.io/api/accounts/{account_id}/account_accesses/{account_access_id}/permissions/bulk
4039
*/
4140
try {
42-
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
4341
$accountAccessId = getenv('MAILTRAP_ACCOUNT_ACCESS_ID');
4442

4543
// resource IDs
@@ -53,7 +51,7 @@
5351
new DestroyPermission($destroyProjectResourceId, PermissionInterface::TYPE_PROJECT),
5452
);
5553

56-
$response = $mailtrap->general()->permissions()->update($accountId, $accountAccessId, $permissions);
54+
$response = $generalPermissions->update($accountAccessId, $permissions);
5755

5856
// print the response body (array)
5957
var_dump(ResponseHelper::toArray($response));

0 commit comments

Comments
 (0)