-
Notifications
You must be signed in to change notification settings - Fork 17
Add Inbound Email API support #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e033963
Add inbound folder and inbox request DTOs
mklocek 6468788
Extract EmailPayloadTrait from AbstractEmails
mklocek 3313673
Add inbound email API classes and inbound() client layer
mklocek 7b2f5f8
Add inbound email tests
mklocek 895fe5a
Add inbound_inbox_id to webhook DTOs
mklocek dc2182e
Add inbound email examples and README entries
mklocek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,79 @@ | ||
| <?php | ||
|
|
||
| use Mailtrap\Config; | ||
| use Mailtrap\DTO\Request\Inbound\CreateInboundFolder; | ||
| use Mailtrap\DTO\Request\Inbound\UpdateInboundFolder; | ||
| use Mailtrap\Helper\ResponseHelper; | ||
| use Mailtrap\MailtrapInboundClient; | ||
|
|
||
| require __DIR__ . '/../../vendor/autoload.php'; | ||
|
|
||
| $config = new Config($_ENV['MAILTRAP_API_KEY']); #your API token from here https://mailtrap.io/api-tokens | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| $folders = (new MailtrapInboundClient($config))->folders(); | ||
|
|
||
| $folderId = (int) ($_ENV['MAILTRAP_INBOUND_FOLDER_ID'] ?? 0); | ||
|
|
||
| /** | ||
| * List all inbound folders. | ||
| * | ||
| * GET https://mailtrap.io/api/inbound/folders | ||
| */ | ||
| try { | ||
| $response = $folders->getList(); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Create an inbound folder. | ||
| * | ||
| * POST https://mailtrap.io/api/inbound/folders | ||
| */ | ||
| try { | ||
| $response = $folders->create(new CreateInboundFolder('Support')); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Get an inbound folder by ID. | ||
| * | ||
| * GET https://mailtrap.io/api/inbound/folders/{folder_id} | ||
| */ | ||
| try { | ||
| $response = $folders->getById($folderId); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Rename an inbound folder. | ||
| * | ||
| * PATCH https://mailtrap.io/api/inbound/folders/{folder_id} | ||
| */ | ||
| try { | ||
| $response = $folders->update($folderId, new UpdateInboundFolder('Support (renamed)')); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Delete an inbound folder (removes the folder and all of its inboxes). | ||
| * | ||
| * DELETE https://mailtrap.io/api/inbound/folders/{folder_id} | ||
| */ | ||
| try { | ||
| $response = $folders->delete($folderId); | ||
|
|
||
| var_dump($response->getStatusCode()); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
This file contains hidden or 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,85 @@ | ||
| <?php | ||
|
|
||
| use Mailtrap\Config; | ||
| use Mailtrap\DTO\Request\Inbound\CreateInboundInbox; | ||
| use Mailtrap\DTO\Request\Inbound\UpdateInboundInbox; | ||
| use Mailtrap\Helper\ResponseHelper; | ||
| use Mailtrap\MailtrapInboundClient; | ||
|
|
||
| require __DIR__ . '/../../vendor/autoload.php'; | ||
|
|
||
| $config = new Config($_ENV['MAILTRAP_API_KEY']); #your API token from here https://mailtrap.io/api-tokens | ||
|
|
||
| $folderId = (int) ($_ENV['MAILTRAP_INBOUND_FOLDER_ID'] ?? 0); | ||
| $inboxId = (int) ($_ENV['MAILTRAP_INBOUND_INBOX_ID'] ?? 0); | ||
|
|
||
| // Inboxes are scoped to a folder. | ||
| $inboxes = (new MailtrapInboundClient($config))->inboxes($folderId); | ||
|
|
||
| /** | ||
| * List inboxes in the folder. | ||
| * | ||
| * GET https://mailtrap.io/api/inbound/folders/{folder_id}/inboxes | ||
| */ | ||
| try { | ||
| $response = $inboxes->getList(); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Create an inbox. Omit the domain id for a Mailtrap-hosted inbox; pass it to | ||
| * create a custom-domain (catch-all) inbox. | ||
| * | ||
| * POST https://mailtrap.io/api/inbound/folders/{folder_id}/inboxes | ||
| */ | ||
| try { | ||
| $response = $inboxes->create( | ||
| new CreateInboundInbox('Tickets', $_ENV['MAILTRAP_INBOUND_DOMAIN_ID'] ?? null) | ||
| ); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Get an inbox by ID. | ||
| * | ||
| * GET https://mailtrap.io/api/inbound/folders/{folder_id}/inboxes/{inbox_id} | ||
| */ | ||
| try { | ||
| $response = $inboxes->getById($inboxId); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Rename an inbox. | ||
| * | ||
| * PATCH https://mailtrap.io/api/inbound/folders/{folder_id}/inboxes/{inbox_id} | ||
| */ | ||
| try { | ||
| $response = $inboxes->update($inboxId, new UpdateInboundInbox('Tickets (renamed)')); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Delete an inbox. | ||
| * | ||
| * DELETE https://mailtrap.io/api/inbound/folders/{folder_id}/inboxes/{inbox_id} | ||
| */ | ||
| try { | ||
| $response = $inboxes->delete($inboxId); | ||
|
|
||
| var_dump($response->getStatusCode()); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } |
This file contains hidden or 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,106 @@ | ||
| <?php | ||
|
|
||
| use Mailtrap\Config; | ||
| use Mailtrap\Helper\ResponseHelper; | ||
| use Mailtrap\MailtrapInboundClient; | ||
| use Symfony\Component\Mime\Address; | ||
| use Symfony\Component\Mime\Email; | ||
|
|
||
| require __DIR__ . '/../../vendor/autoload.php'; | ||
|
|
||
| $config = new Config($_ENV['MAILTRAP_API_KEY']); #your API token from here https://mailtrap.io/api-tokens | ||
|
|
||
| $inboxId = (int) ($_ENV['MAILTRAP_INBOUND_INBOX_ID'] ?? 0); | ||
| $messageId = $_ENV['MAILTRAP_INBOUND_MESSAGE_ID'] ?? ''; | ||
|
|
||
| // Messages are scoped to an inbox. | ||
| $messages = (new MailtrapInboundClient($config))->messages($inboxId); | ||
|
|
||
| /** | ||
| * List received messages. Pass the previous page's last id to paginate. | ||
| * | ||
| * GET https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages | ||
| */ | ||
| try { | ||
| $response = $messages->getList(); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Get a single message with its body and attachment download URLs. | ||
| * | ||
| * GET https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages/{message_id} | ||
| */ | ||
| try { | ||
| $response = $messages->getById($messageId); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Reply to a message (sends a real email to the original sender). | ||
| * reply/replyAll/forward accept a Symfony Mime Email, just like the send API. | ||
| * | ||
| * POST https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages/{message_id}/reply | ||
| */ | ||
| try { | ||
| $email = (new Email()) | ||
| ->text('Thanks for reaching out!') | ||
| ->html('<p>Thanks for reaching out!</p>'); | ||
|
|
||
| $response = $messages->reply($messageId, $email); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Reply to a message and copy the original's other recipients. | ||
| * | ||
| * POST https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages/{message_id}/reply_all | ||
| */ | ||
| try { | ||
| $email = (new Email())->text('Looping everyone in.'); | ||
|
|
||
| $response = $messages->replyAll($messageId, $email); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Forward a message to new recipients (at least one is required). | ||
| * | ||
| * POST https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages/{message_id}/forward | ||
| */ | ||
| try { | ||
| $email = (new Email()) | ||
| ->to(new Address('colleague@example.com')) | ||
| ->text('Please take a look.'); | ||
|
|
||
| $response = $messages->forward($messageId, $email); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Delete a message. | ||
| * | ||
| * DELETE https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages/{message_id} | ||
| */ | ||
| try { | ||
| $response = $messages->delete($messageId); | ||
|
|
||
| var_dump($response->getStatusCode()); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍