Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ Email Sandbox (Testing):
- Project management CRUD – [`testing/projects.php`](examples/testing/projects.php)
- Attachments operations – [`testing/attachments.php`](examples/testing/attachments.php)

Inbound Email API:
- Folder management CRUD – [`inbound/folders.php`](examples/inbound/folders.php)
- Inbox management CRUD – [`inbound/inboxes.php`](examples/inbound/inboxes.php)
- Message management (list / get / reply / reply-all / forward / delete) – [`inbound/messages.php`](examples/inbound/messages.php)
- Thread management (list / get / delete) – [`inbound/threads.php`](examples/inbound/threads.php)

Contact management:
- Contacts CRUD & listing – [`contacts/all.php`](examples/contacts/all.php)
- Contact lists CRUD – [`contact-lists/all.php`](examples/contact-lists/all.php)
Expand Down
33 changes: 25 additions & 8 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Central index of runnable example scripts demonstrating Mailtrap PHP SDK feature

## Contents

### 1. Sending (Transactional / Bulk Streams)
### Sending (Transactional / Bulk Streams)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


| Purpose | File |
|---------|------|
| Minimal transactional send | [`sending/minimal.php`](sending/minimal.php) |
Expand All @@ -18,7 +19,8 @@ Central index of runnable example scripts demonstrating Mailtrap PHP SDK feature
| Bulk API single send (stream selection) | [`bulk/bulk.php`](bulk/bulk.php) |
| Bulk API template send | [`bulk/bulk_template.php`](bulk/bulk_template.php) |

### 2. Batch Sending (multiple messages in one call)
### Batch Sending (multiple messages in one call)

| Purpose | File |
|---------|------|
| Transactional batch send | [`batch/transactional.php`](batch/transactional.php) |
Expand All @@ -28,7 +30,8 @@ Central index of runnable example scripts demonstrating Mailtrap PHP SDK feature
| Sandbox batch send | [`batch/sandbox.php`](batch/sandbox.php) |
| Sandbox batch send (template) | [`batch/sandbox_template.php`](batch/sandbox_template.php) |

### 3. Sandbox (Email Testing)
### Sandbox (Email Testing)

| Purpose | File |
|---------|------|
| Sandbox transactional send | [`testing/send-mail.php`](testing/send-mail.php) |
Expand All @@ -38,34 +41,48 @@ Central index of runnable example scripts demonstrating Mailtrap PHP SDK feature
| Message CRUD / listing | [`testing/messages.php`](testing/messages.php) |
| Project CRUD / listing | [`testing/projects.php`](testing/projects.php) |

### 4. Contact Management
### Inbound Email

| Purpose | File |
|---------|------|
| Folder CRUD / listing | [`inbound/folders.php`](inbound/folders.php) |
| Inbox CRUD / listing | [`inbound/inboxes.php`](inbound/inboxes.php) |
| Message list / get / reply / reply-all / forward / delete | [`inbound/messages.php`](inbound/messages.php) |
| Thread list / get / delete | [`inbound/threads.php`](inbound/threads.php) |

### Contact Management

| Purpose | File |
|---------|------|
| Contacts CRUD + list | [`contacts/all.php`](contacts/all.php) |
| Contact lists CRUD | [`contact-lists/all.php`](contact-lists/all.php) |
| Custom fields CRUD | [`contact-fields/all.php`](contact-fields/all.php) |

### 5. Templates & Domains
### Templates & Domains

| Purpose | File |
|---------|------|
| Templates CRUD | [`templates/all.php`](templates/all.php) |
| Sending domains CRUD | [`sending-domains/all.php`](sending-domains/all.php) |

### 6. General API
### General API

| Purpose | File |
|---------|------|
| Accounts info | [`general/accounts.php`](general/accounts.php) |
| Billing info | [`general/billing.php`](general/billing.php) |
| Permissions listing | [`general/permissions.php`](general/permissions.php) |
| Users listing | [`general/users.php`](general/users.php) |

### 7. Framework Bridges
### Framework Bridges

| Framework | Files |
|-----------|-------|
| Laravel (transactional, sandbox, template, bulk) | [`laravel/transactional.php`](laravel/transactional.php), [`laravel/sandbox.php`](laravel/sandbox.php), [`laravel/template.php`](laravel/template.php), [`laravel/bulk.php`](laravel/bulk.php) |
| Symfony (transactional, sandbox, template, bulk) | [`symfony/transactional.php`](symfony/transactional.php), [`symfony/sandbox.php`](symfony/sandbox.php), [`symfony/template.php`](symfony/template.php), [`symfony/bulk.php`](symfony/bulk.php) |

### 8. Configuration Utilities
### Configuration Utilities
Comment thread
coderabbitai[bot] marked this conversation as resolved.

| Purpose | File |
|---------|------|
| Showcase of combined config usage / initialization patterns | [`config/all.php`](config/all.php) |
Expand Down
79 changes: 79 additions & 0 deletions examples/inbound/folders.php
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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";
}
85 changes: 85 additions & 0 deletions examples/inbound/inboxes.php
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";
}
106 changes: 106 additions & 0 deletions examples/inbound/messages.php
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";
}
Loading
Loading