Skip to content

Commit 61b6f34

Browse files
authored
Merge pull request #10 from railsware/feature/inboxes
Support inbox endpoints
2 parents 31dfdc2 + 0cc0793 commit 61b6f34

File tree

13 files changed

+873
-13
lines changed

13 files changed

+873
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [1.5.0] - 2023-05-04
2+
3+
- Support sandbox inbox endpoints. Examples [here](examples/sandbox/inboxes.php)
4+
5+
16
## [1.4.0] - 2023-04-20
27

38
- Support general permission endpoints. Examples [here](examples/general/permissions.php)

examples/sandbox/inboxes.php

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<?php
2+
3+
use Mailtrap\Config;
4+
use Mailtrap\DTO\Request\Inbox;
5+
use Mailtrap\Helper\ResponseHelper;
6+
use Mailtrap\MailtrapClient;
7+
8+
require __DIR__ . '/../vendor/autoload.php';
9+
10+
// your API token from here https://mailtrap.io/api-tokens
11+
$apiKey = getenv('MAILTRAP_API_KEY');
12+
$mailtrap = new MailtrapClient(new Config($apiKey));
13+
14+
15+
/**
16+
* Get a list of inboxes.
17+
*
18+
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes
19+
*/
20+
try {
21+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
22+
23+
$response = $mailtrap->sandbox()->inboxes()->getList($accountId);
24+
25+
// print the response body (array)
26+
var_dump(ResponseHelper::toArray($response));
27+
} catch (Exception $e) {
28+
echo 'Caught exception: ', $e->getMessage(), "\n";
29+
}
30+
31+
32+
/**
33+
* Create an inbox
34+
*
35+
* POST https://mailtrap.io/api/accounts/{account_id}/projects/{project_id}/inboxes
36+
*/
37+
try {
38+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
39+
$projectId = getenv('MAILTRAP_PROJECT_ID');
40+
$inboxName = 'First inbox';
41+
42+
$response = $mailtrap->sandbox()->inboxes()->create($accountId, $projectId, $inboxName);
43+
44+
// print the response body (array)
45+
var_dump(ResponseHelper::toArray($response));
46+
} catch (Exception $e) {
47+
echo 'Caught exception: ', $e->getMessage(), "\n";
48+
}
49+
50+
51+
/**
52+
* Get inbox attributes
53+
*
54+
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}
55+
*/
56+
try {
57+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
58+
$inboxId = getenv('MAILTRAP_INBOX_ID');
59+
60+
$response = $mailtrap->sandbox()->inboxes()->getInboxAttributes($accountId, $inboxId);
61+
62+
// print the response body (array)
63+
var_dump(ResponseHelper::toArray($response));
64+
} catch (Exception $e) {
65+
echo 'Caught exception: ', $e->getMessage(), "\n";
66+
}
67+
68+
69+
/**
70+
* Delete an inbox
71+
*
72+
* DELETE https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}
73+
*/
74+
try {
75+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
76+
$inboxId = getenv('MAILTRAP_INBOX_ID');
77+
78+
$response = $mailtrap->sandbox()->inboxes()->delete($accountId, $inboxId);
79+
80+
// print the response body (array)
81+
var_dump(ResponseHelper::toArray($response));
82+
} catch (Exception $e) {
83+
echo 'Caught exception: ', $e->getMessage(), "\n";
84+
}
85+
86+
87+
/**
88+
* Reset email address
89+
*
90+
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/reset_email_username
91+
*/
92+
try {
93+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
94+
$inboxId = getenv('MAILTRAP_INBOX_ID');
95+
96+
$response = $mailtrap->sandbox()->inboxes()->resetEmailAddress($accountId, $inboxId);
97+
98+
// print the response body (array)
99+
var_dump(ResponseHelper::toArray($response));
100+
} catch (Exception $e) {
101+
echo 'Caught exception: ', $e->getMessage(), "\n";
102+
}
103+
104+
105+
/**
106+
* Enable/Disable email address
107+
*
108+
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/toggle_email_username
109+
*/
110+
try {
111+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
112+
$inboxId = getenv('MAILTRAP_INBOX_ID');
113+
114+
$response = $mailtrap->sandbox()->inboxes()->toggleEmailAddress($accountId, $inboxId);
115+
116+
// print the response body (array)
117+
var_dump(ResponseHelper::toArray($response));
118+
} catch (Exception $e) {
119+
echo 'Caught exception: ', $e->getMessage(), "\n";
120+
}
121+
122+
123+
/**
124+
* Reset credentials
125+
*
126+
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/reset_credentials
127+
*/
128+
try {
129+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
130+
$inboxId = getenv('MAILTRAP_INBOX_ID');
131+
132+
$response = $mailtrap->sandbox()->inboxes()->resetSmtpCredentials($accountId, $inboxId);
133+
134+
// print the response body (array)
135+
var_dump(ResponseHelper::toArray($response));
136+
} catch (Exception $e) {
137+
echo 'Caught exception: ', $e->getMessage(), "\n";
138+
}
139+
140+
141+
/**
142+
* Mark as read
143+
*
144+
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/all_read
145+
*/
146+
try {
147+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
148+
$inboxId = getenv('MAILTRAP_INBOX_ID');
149+
150+
$response = $mailtrap->sandbox()->inboxes()->markAsRead($accountId, $inboxId);
151+
152+
// print the response body (array)
153+
var_dump(ResponseHelper::toArray($response));
154+
} catch (Exception $e) {
155+
echo 'Caught exception: ', $e->getMessage(), "\n";
156+
}
157+
158+
159+
/**
160+
* Clean inbox
161+
*
162+
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/clean
163+
*/
164+
try {
165+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
166+
$inboxId = getenv('MAILTRAP_INBOX_ID');
167+
168+
$response = $mailtrap->sandbox()->inboxes()->clean($accountId, $inboxId);
169+
170+
// print the response body (array)
171+
var_dump(ResponseHelper::toArray($response));
172+
} catch (Exception $e) {
173+
echo 'Caught exception: ', $e->getMessage(), "\n";
174+
}
175+
176+
177+
/**
178+
* Update an inbox
179+
*
180+
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}
181+
*/
182+
try {
183+
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
184+
$inboxId = getenv('MAILTRAP_INBOX_ID');
185+
$newInboxName = 'New inbox name';
186+
$newEmailUsername = 'new-email-username';
187+
188+
$response = $mailtrap->sandbox()->inboxes()->update(
189+
$accountId,
190+
$inboxId,
191+
new Inbox($newInboxName, $newEmailUsername)
192+
);
193+
194+
// print the response body (array)
195+
var_dump(ResponseHelper::toArray($response));
196+
} catch (Exception $e) {
197+
echo 'Caught exception: ', $e->getMessage(), "\n";
198+
}

psalm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<directory name="src" />
1515
<ignoreFiles>
1616
<directory name="vendor" />
17-
<directory name="src/Bridge/Laravel" />
17+
<directory name="src/Bridge" />
1818
</ignoreFiles>
1919
</projectFiles>
2020
</psalm>

src/Api/Sandbox/Inbox.php

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
3+
namespace Mailtrap\Api\Sandbox;
4+
5+
use Mailtrap\Api\AbstractApi;
6+
use Mailtrap\DTO\Request\Inbox as InboxRequest;
7+
use Mailtrap\Exception\RuntimeException;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
/**
11+
* Class Inbox
12+
*/
13+
class Inbox extends AbstractApi implements SandboxInterface
14+
{
15+
/**
16+
* Get a list of inboxes.
17+
*
18+
* @param int $accountId
19+
*
20+
* @return ResponseInterface
21+
*/
22+
public function getList(int $accountId): ResponseInterface
23+
{
24+
return $this->handleResponse($this->httpGet(
25+
sprintf('%s/api/accounts/%s/inboxes', $this->getHost(), $accountId)
26+
));
27+
}
28+
29+
/**
30+
* Get inbox attributes by inbox id. See the list of attributes in the example
31+
*
32+
* @param int $accountId
33+
* @param int $inboxId
34+
*
35+
* @return ResponseInterface
36+
*/
37+
public function getInboxAttributes(int $accountId, int $inboxId): ResponseInterface
38+
{
39+
return $this->handleResponse($this->httpGet(
40+
sprintf('%s/api/accounts/%s/inboxes/%s', $this->getHost(), $accountId, $inboxId)
41+
));
42+
}
43+
44+
/**
45+
* Create an inbox in a project.
46+
*
47+
* @param int $accountId
48+
* @param int $projectId
49+
* @param string $inboxName
50+
*
51+
* @return ResponseInterface
52+
*/
53+
public function create(int $accountId, int $projectId, string $inboxName): ResponseInterface
54+
{
55+
return $this->handleResponse(
56+
$this->httpPost(
57+
sprintf('%s/api/accounts/%s/projects/%s/inboxes', $this->getHost(), $accountId, $projectId),
58+
[],
59+
['inbox' => ['name' => $inboxName]]
60+
)
61+
);
62+
}
63+
64+
/**
65+
* Delete an inbox with all its emails.
66+
*
67+
* @param int $accountId
68+
* @param int $inboxId
69+
*
70+
* @return ResponseInterface
71+
*/
72+
public function delete(int $accountId, int $inboxId): ResponseInterface
73+
{
74+
return $this->handleResponse($this->httpDelete(
75+
sprintf('%s/api/accounts/%s/inboxes/%s', $this->getHost(), $accountId, $inboxId)
76+
));
77+
}
78+
79+
/**
80+
* Update inbox name and/or inbox email username.
81+
*
82+
* @param int $accountId
83+
* @param int $inboxId
84+
* @param InboxRequest $updateInbox
85+
*
86+
* @return ResponseInterface
87+
*/
88+
public function update(int $accountId, int $inboxId, InboxRequest $updateInbox): ResponseInterface
89+
{
90+
return $this->handleResponse($this->httpPatch(
91+
sprintf('%s/api/accounts/%s/inboxes/%s', $this->getHost(), $accountId, $inboxId),
92+
[],
93+
['inbox' => $this->getUpdatePayload($updateInbox)]
94+
));
95+
}
96+
97+
/**
98+
* Delete all messages (emails) from inbox.
99+
*
100+
* @param int $accountId
101+
* @param int $inboxId
102+
*
103+
* @return ResponseInterface
104+
*/
105+
public function clean(int $accountId, int $inboxId): ResponseInterface
106+
{
107+
return $this->handleResponse($this->httpPatch(
108+
sprintf('%s/api/accounts/%s/inboxes/%s/clean', $this->getHost(), $accountId, $inboxId)
109+
));
110+
}
111+
112+
/**
113+
* Mark all messages in the inbox as read.
114+
*
115+
* @param int $accountId
116+
* @param int $inboxId
117+
*
118+
* @return ResponseInterface
119+
*/
120+
public function markAsRead(int $accountId, int $inboxId): ResponseInterface
121+
{
122+
return $this->handleResponse($this->httpPatch(
123+
sprintf('%s/api/accounts/%s/inboxes/%s/all_read', $this->getHost(), $accountId, $inboxId)
124+
));
125+
}
126+
127+
/**
128+
* Reset SMTP credentials of the inbox.
129+
*
130+
* @param int $accountId
131+
* @param int $inboxId
132+
*
133+
* @return ResponseInterface
134+
*/
135+
public function resetSmtpCredentials(int $accountId, int $inboxId): ResponseInterface
136+
{
137+
return $this->handleResponse($this->httpPatch(
138+
sprintf('%s/api/accounts/%s/inboxes/%s/reset_credentials', $this->getHost(), $accountId, $inboxId)
139+
));
140+
}
141+
142+
/**
143+
* Turn the email address of the inbox on/off.
144+
*
145+
* @param int $accountId
146+
* @param int $inboxId
147+
*
148+
* @return ResponseInterface
149+
*/
150+
public function toggleEmailAddress(int $accountId, int $inboxId): ResponseInterface
151+
{
152+
return $this->handleResponse($this->httpPatch(
153+
sprintf('%s/api/accounts/%s/inboxes/%s/toggle_email_username', $this->getHost(), $accountId, $inboxId)
154+
));
155+
}
156+
157+
/**
158+
* Reset username of email address per inbox.
159+
*
160+
* @param int $accountId
161+
* @param int $inboxId
162+
*
163+
* @return ResponseInterface
164+
*/
165+
public function resetEmailAddress(int $accountId, int $inboxId): ResponseInterface
166+
{
167+
return $this->handleResponse($this->httpPatch(
168+
sprintf('%s/api/accounts/%s/inboxes/%s/reset_email_username', $this->getHost(), $accountId, $inboxId)
169+
));
170+
}
171+
172+
private function getUpdatePayload(InboxRequest $updateInbox): array
173+
{
174+
$result = $updateInbox->toArray();
175+
if (empty($result)) {
176+
throw new RuntimeException('At least one inbox parameter should be populated ("name" or "email_username")');
177+
}
178+
179+
return $result;
180+
}
181+
}

0 commit comments

Comments
 (0)