Skip to content

Commit

Permalink
Merge pull request #10 from railsware/feature/inboxes
Browse files Browse the repository at this point in the history
Support inbox endpoints
  • Loading branch information
gaalferov authored May 5, 2023
2 parents 31dfdc2 + 0cc0793 commit 61b6f34
Show file tree
Hide file tree
Showing 13 changed files with 873 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [1.5.0] - 2023-05-04

- Support sandbox inbox endpoints. Examples [here](examples/sandbox/inboxes.php)


## [1.4.0] - 2023-04-20

- Support general permission endpoints. Examples [here](examples/general/permissions.php)
Expand Down
198 changes: 198 additions & 0 deletions examples/sandbox/inboxes.php
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";
}
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
<directory name="src/Bridge/Laravel" />
<directory name="src/Bridge" />
</ignoreFiles>
</projectFiles>
</psalm>
181 changes: 181 additions & 0 deletions src/Api/Sandbox/Inbox.php
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;
}
}
Loading

0 comments on commit 61b6f34

Please sign in to comment.