Skip to content

Commit

Permalink
Merge pull request #12 from railsware/feature/messages
Browse files Browse the repository at this point in the history
Support message endpoints
  • Loading branch information
gaalferov authored May 12, 2023
2 parents 2246857 + f61d382 commit 85f817f
Show file tree
Hide file tree
Showing 6 changed files with 921 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [1.7.0] - 2023-05-16

- Support sandbox message endpoints. Examples [here](examples/sandbox/messages.php)


## [1.6.0] - 2023-05-05

- Support sandbox attachment endpoints. Examples [here](examples/sandbox/attachments.php)
Expand Down
224 changes: 224 additions & 0 deletions examples/sandbox/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?php

use Mailtrap\Config;
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 messages
* Note: if you want to get all messages you need to use "page" param (by default will return only 30 messages per page)
*
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages
*/
try {
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
$inboxId = getenv('MAILTRAP_INBOX_ID');

// not required parameters
$page = 1; // by default 30 messages per page
$search = 'hello'; // it works like case insensitive pattern matching by subject, to_email, to_name
$lastMessageId = 3000000003; // get emails, where primary key is less then this param (does not work with page param)

$response = $mailtrap->sandbox()->messages()->getList($accountId, $inboxId);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Show email message
*
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}
*/
try {
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
$inboxId = getenv('MAILTRAP_INBOX_ID');
$messageId = getenv('MAILTRAP_INBOX_MESSAGE_ID');

$response = $mailtrap->sandbox()->messages()->getById($accountId, $inboxId, $messageId);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Get message source
*
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}/body.htmlsource
*/
try {
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
$inboxId = getenv('MAILTRAP_INBOX_ID');
$messageId = getenv('MAILTRAP_INBOX_MESSAGE_ID');

$response = $mailtrap->sandbox()->messages()->getSource($accountId, $inboxId, $messageId);

// print the response body (string)
var_dump(ResponseHelper::toString($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Get message as .eml
*
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}/body.eml
*/
try {
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
$inboxId = getenv('MAILTRAP_INBOX_ID');
$messageId = getenv('MAILTRAP_INBOX_MESSAGE_ID');

$response = $mailtrap->sandbox()->messages()->getEml($accountId, $inboxId, $messageId);

// print the response body (string)
var_dump(ResponseHelper::toString($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Get HTML message
*
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}/body.html
*/
try {
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
$inboxId = getenv('MAILTRAP_INBOX_ID');
$messageId = getenv('MAILTRAP_INBOX_MESSAGE_ID');

$response = $mailtrap->sandbox()->messages()->getHtml($accountId, $inboxId, $messageId);

// print the response body (string)
var_dump(ResponseHelper::toString($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Get raw message
*
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}/body.raw
*/
try {
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
$inboxId = getenv('MAILTRAP_INBOX_ID');
$messageId = getenv('MAILTRAP_INBOX_MESSAGE_ID');

$response = $mailtrap->sandbox()->messages()->getRaw($accountId, $inboxId, $messageId);

// print the response body (string)
var_dump(ResponseHelper::toString($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Get text message
*
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}/body.txt
*/
try {
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
$inboxId = getenv('MAILTRAP_INBOX_ID');
$messageId = getenv('MAILTRAP_INBOX_MESSAGE_ID');

$response = $mailtrap->sandbox()->messages()->getText($accountId, $inboxId, $messageId);

// print the response body (string)
var_dump(ResponseHelper::toString($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Get message HTML analysis
*
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}/analyze
*/
try {
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
$inboxId = getenv('MAILTRAP_INBOX_ID');
$messageId = getenv('MAILTRAP_INBOX_MESSAGE_ID');

$response = $mailtrap->sandbox()->messages()->getHtmlAnalysis($accountId, $inboxId, $messageId);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Get message spam score
*
* GET https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}/spam_report
*/
try {
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
$inboxId = getenv('MAILTRAP_INBOX_ID');
$messageId = getenv('MAILTRAP_INBOX_MESSAGE_ID');

$response = $mailtrap->sandbox()->messages()->getSpamScore($accountId, $inboxId, $messageId);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Update message (mark as read)
*
* PATCH https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}
*/
try {
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
$inboxId = getenv('MAILTRAP_INBOX_ID');
$messageId = getenv('MAILTRAP_INBOX_MESSAGE_ID');

$response = $mailtrap->sandbox()->messages()->markAsRead($accountId, $inboxId, $messageId);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}


/**
* Delete message
*
* DELETE https://mailtrap.io/api/accounts/{account_id}/inboxes/{inbox_id}/messages/{message_id}
*/
try {
$accountId = getenv('MAILTRAP_ACCOUNT_ID');
$inboxId = getenv('MAILTRAP_INBOX_ID');
$messageId = getenv('MAILTRAP_INBOX_MESSAGE_ID');

$response = $mailtrap->sandbox()->messages()->delete($accountId, $inboxId, $messageId);

// print the response body (array)
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Loading

0 comments on commit 85f817f

Please sign in to comment.