-
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 #10 from railsware/feature/inboxes
Support inbox endpoints
- Loading branch information
Showing
13 changed files
with
873 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
<?php | ||
|
||
use Mailtrap\Config; | ||
use Mailtrap\DTO\Request\Inbox; | ||
use Mailtrap\Helper\ResponseHelper; | ||
use Mailtrap\MailtrapClient; | ||
|
||
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)); | ||
|
||
|
||
/** | ||
* Get a list of inboxes. | ||
* | ||
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
|
||
$response = $mailtrap->sandbox()->inboxes()->getList($accountId); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/** | ||
* Create an inbox | ||
* | ||
* POST https://mailtrap.io/api/accounts/{account_id}/projects/{project_id}/inboxes | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$projectId = getenv('MAILTRAP_PROJECT_ID'); | ||
$inboxName = 'First inbox'; | ||
|
||
$response = $mailtrap->sandbox()->inboxes()->create($accountId, $projectId, $inboxName); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/** | ||
* Get inbox attributes | ||
* | ||
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id} | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$inboxId = getenv('MAILTRAP_INBOX_ID'); | ||
|
||
$response = $mailtrap->sandbox()->inboxes()->getInboxAttributes($accountId, $inboxId); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/** | ||
* Delete an inbox | ||
* | ||
* DELETE https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id} | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$inboxId = getenv('MAILTRAP_INBOX_ID'); | ||
|
||
$response = $mailtrap->sandbox()->inboxes()->delete($accountId, $inboxId); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/** | ||
* Reset email address | ||
* | ||
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/reset_email_username | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$inboxId = getenv('MAILTRAP_INBOX_ID'); | ||
|
||
$response = $mailtrap->sandbox()->inboxes()->resetEmailAddress($accountId, $inboxId); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/** | ||
* Enable/Disable email address | ||
* | ||
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/toggle_email_username | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$inboxId = getenv('MAILTRAP_INBOX_ID'); | ||
|
||
$response = $mailtrap->sandbox()->inboxes()->toggleEmailAddress($accountId, $inboxId); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/** | ||
* Reset credentials | ||
* | ||
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/reset_credentials | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$inboxId = getenv('MAILTRAP_INBOX_ID'); | ||
|
||
$response = $mailtrap->sandbox()->inboxes()->resetSmtpCredentials($accountId, $inboxId); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/** | ||
* Mark as read | ||
* | ||
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/all_read | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$inboxId = getenv('MAILTRAP_INBOX_ID'); | ||
|
||
$response = $mailtrap->sandbox()->inboxes()->markAsRead($accountId, $inboxId); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/** | ||
* Clean inbox | ||
* | ||
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/clean | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$inboxId = getenv('MAILTRAP_INBOX_ID'); | ||
|
||
$response = $mailtrap->sandbox()->inboxes()->clean($accountId, $inboxId); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/** | ||
* Update an inbox | ||
* | ||
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id} | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$inboxId = getenv('MAILTRAP_INBOX_ID'); | ||
$newInboxName = 'New inbox name'; | ||
$newEmailUsername = 'new-email-username'; | ||
|
||
$response = $mailtrap->sandbox()->inboxes()->update( | ||
$accountId, | ||
$inboxId, | ||
new Inbox($newInboxName, $newEmailUsername) | ||
); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,181 @@ | ||
<?php | ||
|
||
namespace Mailtrap\Api\Sandbox; | ||
|
||
use Mailtrap\Api\AbstractApi; | ||
use Mailtrap\DTO\Request\Inbox as InboxRequest; | ||
use Mailtrap\Exception\RuntimeException; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* Class Inbox | ||
*/ | ||
class Inbox extends AbstractApi implements SandboxInterface | ||
{ | ||
/** | ||
* Get a list of inboxes. | ||
* | ||
* @param int $accountId | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function getList(int $accountId): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpGet( | ||
sprintf('%s/api/accounts/%s/inboxes', $this->getHost(), $accountId) | ||
)); | ||
} | ||
|
||
/** | ||
* Get inbox attributes by inbox id. See the list of attributes in the example | ||
* | ||
* @param int $accountId | ||
* @param int $inboxId | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function getInboxAttributes(int $accountId, int $inboxId): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpGet( | ||
sprintf('%s/api/accounts/%s/inboxes/%s', $this->getHost(), $accountId, $inboxId) | ||
)); | ||
} | ||
|
||
/** | ||
* Create an inbox in a project. | ||
* | ||
* @param int $accountId | ||
* @param int $projectId | ||
* @param string $inboxName | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function create(int $accountId, int $projectId, string $inboxName): ResponseInterface | ||
{ | ||
return $this->handleResponse( | ||
$this->httpPost( | ||
sprintf('%s/api/accounts/%s/projects/%s/inboxes', $this->getHost(), $accountId, $projectId), | ||
[], | ||
['inbox' => ['name' => $inboxName]] | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Delete an inbox with all its emails. | ||
* | ||
* @param int $accountId | ||
* @param int $inboxId | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function delete(int $accountId, int $inboxId): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpDelete( | ||
sprintf('%s/api/accounts/%s/inboxes/%s', $this->getHost(), $accountId, $inboxId) | ||
)); | ||
} | ||
|
||
/** | ||
* Update inbox name and/or inbox email username. | ||
* | ||
* @param int $accountId | ||
* @param int $inboxId | ||
* @param InboxRequest $updateInbox | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function update(int $accountId, int $inboxId, InboxRequest $updateInbox): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpPatch( | ||
sprintf('%s/api/accounts/%s/inboxes/%s', $this->getHost(), $accountId, $inboxId), | ||
[], | ||
['inbox' => $this->getUpdatePayload($updateInbox)] | ||
)); | ||
} | ||
|
||
/** | ||
* Delete all messages (emails) from inbox. | ||
* | ||
* @param int $accountId | ||
* @param int $inboxId | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function clean(int $accountId, int $inboxId): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpPatch( | ||
sprintf('%s/api/accounts/%s/inboxes/%s/clean', $this->getHost(), $accountId, $inboxId) | ||
)); | ||
} | ||
|
||
/** | ||
* Mark all messages in the inbox as read. | ||
* | ||
* @param int $accountId | ||
* @param int $inboxId | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function markAsRead(int $accountId, int $inboxId): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpPatch( | ||
sprintf('%s/api/accounts/%s/inboxes/%s/all_read', $this->getHost(), $accountId, $inboxId) | ||
)); | ||
} | ||
|
||
/** | ||
* Reset SMTP credentials of the inbox. | ||
* | ||
* @param int $accountId | ||
* @param int $inboxId | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function resetSmtpCredentials(int $accountId, int $inboxId): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpPatch( | ||
sprintf('%s/api/accounts/%s/inboxes/%s/reset_credentials', $this->getHost(), $accountId, $inboxId) | ||
)); | ||
} | ||
|
||
/** | ||
* Turn the email address of the inbox on/off. | ||
* | ||
* @param int $accountId | ||
* @param int $inboxId | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function toggleEmailAddress(int $accountId, int $inboxId): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpPatch( | ||
sprintf('%s/api/accounts/%s/inboxes/%s/toggle_email_username', $this->getHost(), $accountId, $inboxId) | ||
)); | ||
} | ||
|
||
/** | ||
* Reset username of email address per inbox. | ||
* | ||
* @param int $accountId | ||
* @param int $inboxId | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function resetEmailAddress(int $accountId, int $inboxId): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpPatch( | ||
sprintf('%s/api/accounts/%s/inboxes/%s/reset_email_username', $this->getHost(), $accountId, $inboxId) | ||
)); | ||
} | ||
|
||
private function getUpdatePayload(InboxRequest $updateInbox): array | ||
{ | ||
$result = $updateInbox->toArray(); | ||
if (empty($result)) { | ||
throw new RuntimeException('At least one inbox parameter should be populated ("name" or "email_username")'); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
Oops, something went wrong.