-
Notifications
You must be signed in to change notification settings - Fork 15
Add Stats API #61
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
Add Stats API #61
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| <?php | ||
|
|
||
| use Mailtrap\Config; | ||
| use Mailtrap\Helper\ResponseHelper; | ||
| use Mailtrap\MailtrapSendingClient; | ||
|
|
||
| require __DIR__ . '/../../vendor/autoload.php'; | ||
|
|
||
| $accountId = (int) $_ENV['MAILTRAP_ACCOUNT_ID']; | ||
| $config = new Config($_ENV['MAILTRAP_API_KEY']); #your API token from here https://mailtrap.io/api-tokens | ||
| $stats = (new MailtrapSendingClient($config))->stats($accountId); | ||
|
|
||
| /** | ||
| * Get aggregated sending stats. | ||
| * | ||
| * GET https://mailtrap.io/api/accounts/{account_id}/stats | ||
| */ | ||
| try { | ||
| $response = $stats->get('2026-01-01', '2026-01-31'); | ||
|
|
||
| // print the response body (array) | ||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Get aggregated sending stats with filters. | ||
| * | ||
| * GET https://mailtrap.io/api/accounts/{account_id}/stats | ||
| */ | ||
| try { | ||
| $response = $stats->get( | ||
| '2026-01-01', | ||
| '2026-01-31', | ||
| sendingDomainIds: [1, 2], | ||
| sendingStreams: ['transactional'], | ||
| categories: ['Transactional', 'Marketing'], | ||
| emailServiceProviders: ['Gmail', 'Yahoo'] | ||
| ); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Get sending stats grouped by domains. | ||
| * | ||
| * GET https://mailtrap.io/api/accounts/{account_id}/stats/domains | ||
| */ | ||
| try { | ||
| $response = $stats->byDomain('2026-01-01', '2026-01-31'); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Get sending stats grouped by categories. | ||
| * | ||
| * GET https://mailtrap.io/api/accounts/{account_id}/stats/categories | ||
| */ | ||
| try { | ||
| $response = $stats->byCategory('2026-01-01', '2026-01-31'); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Get sending stats grouped by email service providers. | ||
| * | ||
| * GET https://mailtrap.io/api/accounts/{account_id}/stats/email_service_providers | ||
| */ | ||
| try { | ||
| $response = $stats->byEmailServiceProvider('2026-01-01', '2026-01-31'); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Get sending stats grouped by date. | ||
| * | ||
| * GET https://mailtrap.io/api/accounts/{account_id}/stats/date | ||
| */ | ||
| try { | ||
| $response = $stats->byDate('2026-01-01', '2026-01-31'); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } |
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,192 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Mailtrap\Api\Sending; | ||
|
|
||
| use Mailtrap\Api\AbstractApi; | ||
| use Mailtrap\ConfigInterface; | ||
| use Psr\Http\Message\ResponseInterface; | ||
|
|
||
| /** | ||
| * Class Stats | ||
| */ | ||
| class Stats extends AbstractApi implements SendingInterface | ||
| { | ||
| public function __construct(ConfigInterface $config, private int $accountId) | ||
| { | ||
| parent::__construct($config); | ||
| } | ||
|
|
||
| /** | ||
| * Get aggregated sending stats. | ||
| * | ||
| * @param string $startDate | ||
| * @param string $endDate | ||
| * @param array $sendingDomainIds | ||
| * @param array $sendingStreams | ||
| * @param array $categories | ||
| * @param array $emailServiceProviders | ||
| * | ||
| * @return ResponseInterface | ||
| */ | ||
| public function get( | ||
| string $startDate, | ||
| string $endDate, | ||
| array $sendingDomainIds = [], | ||
| array $sendingStreams = [], | ||
| array $categories = [], | ||
| array $emailServiceProviders = [] | ||
| ): ResponseInterface { | ||
| return $this->handleResponse($this->httpGet( | ||
| $this->getBasePath(), | ||
| $this->buildQueryParams($startDate, $endDate, $sendingDomainIds, $sendingStreams, $categories, $emailServiceProviders) | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * Get sending stats grouped by domains. | ||
| * | ||
| * @param string $startDate | ||
| * @param string $endDate | ||
| * @param array $sendingDomainIds | ||
| * @param array $sendingStreams | ||
| * @param array $categories | ||
| * @param array $emailServiceProviders | ||
| * | ||
| * @return ResponseInterface | ||
| */ | ||
| public function byDomain( | ||
| string $startDate, | ||
| string $endDate, | ||
| array $sendingDomainIds = [], | ||
| array $sendingStreams = [], | ||
| array $categories = [], | ||
| array $emailServiceProviders = [] | ||
| ): ResponseInterface { | ||
| return $this->handleResponse($this->httpGet( | ||
| $this->getBasePath() . '/domains', | ||
| $this->buildQueryParams($startDate, $endDate, $sendingDomainIds, $sendingStreams, $categories, $emailServiceProviders) | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * Get sending stats grouped by categories. | ||
| * | ||
| * @param string $startDate | ||
| * @param string $endDate | ||
| * @param array $sendingDomainIds | ||
| * @param array $sendingStreams | ||
| * @param array $categories | ||
| * @param array $emailServiceProviders | ||
| * | ||
| * @return ResponseInterface | ||
| */ | ||
| public function byCategory( | ||
| string $startDate, | ||
| string $endDate, | ||
| array $sendingDomainIds = [], | ||
| array $sendingStreams = [], | ||
| array $categories = [], | ||
| array $emailServiceProviders = [] | ||
| ): ResponseInterface { | ||
| return $this->handleResponse($this->httpGet( | ||
| $this->getBasePath() . '/categories', | ||
| $this->buildQueryParams($startDate, $endDate, $sendingDomainIds, $sendingStreams, $categories, $emailServiceProviders) | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * Get sending stats grouped by email service providers. | ||
| * | ||
| * @param string $startDate | ||
| * @param string $endDate | ||
| * @param array $sendingDomainIds | ||
| * @param array $sendingStreams | ||
| * @param array $categories | ||
| * @param array $emailServiceProviders | ||
| * | ||
| * @return ResponseInterface | ||
| */ | ||
| public function byEmailServiceProvider( | ||
| string $startDate, | ||
| string $endDate, | ||
| array $sendingDomainIds = [], | ||
| array $sendingStreams = [], | ||
| array $categories = [], | ||
| array $emailServiceProviders = [] | ||
| ): ResponseInterface { | ||
| return $this->handleResponse($this->httpGet( | ||
| $this->getBasePath() . '/email_service_providers', | ||
| $this->buildQueryParams($startDate, $endDate, $sendingDomainIds, $sendingStreams, $categories, $emailServiceProviders) | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * Get sending stats grouped by date. | ||
| * | ||
| * @param string $startDate | ||
| * @param string $endDate | ||
| * @param array $sendingDomainIds | ||
| * @param array $sendingStreams | ||
| * @param array $categories | ||
| * @param array $emailServiceProviders | ||
| * | ||
| * @return ResponseInterface | ||
| */ | ||
| public function byDate( | ||
| string $startDate, | ||
| string $endDate, | ||
| array $sendingDomainIds = [], | ||
| array $sendingStreams = [], | ||
| array $categories = [], | ||
| array $emailServiceProviders = [] | ||
| ): ResponseInterface { | ||
| return $this->handleResponse($this->httpGet( | ||
| $this->getBasePath() . '/date', | ||
| $this->buildQueryParams($startDate, $endDate, $sendingDomainIds, $sendingStreams, $categories, $emailServiceProviders) | ||
| )); | ||
| } | ||
|
|
||
| public function getAccountId(): int | ||
| { | ||
| return $this->accountId; | ||
| } | ||
|
|
||
| private function getBasePath(): string | ||
| { | ||
| return sprintf('%s/api/accounts/%s/stats', $this->getHost(), $this->getAccountId()); | ||
| } | ||
|
|
||
| private function buildQueryParams( | ||
| string $startDate, | ||
| string $endDate, | ||
| array $sendingDomainIds, | ||
| array $sendingStreams, | ||
| array $categories, | ||
| array $emailServiceProviders | ||
| ): array { | ||
| $params = [ | ||
| 'start_date' => $startDate, | ||
| 'end_date' => $endDate, | ||
| ]; | ||
|
|
||
| if (count($sendingDomainIds) > 0) { | ||
| $params['sending_domain_ids'] = $sendingDomainIds; | ||
| } | ||
|
|
||
| if (count($sendingStreams) > 0) { | ||
| $params['sending_streams'] = $sendingStreams; | ||
| } | ||
|
|
||
| if (count($categories) > 0) { | ||
| $params['categories'] = $categories; | ||
| } | ||
|
|
||
| if (count($emailServiceProviders) > 0) { | ||
| $params['email_service_providers'] = $emailServiceProviders; | ||
| } | ||
|
|
||
| return $params; | ||
| } | ||
| } | ||
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
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.
Fail fast for invalid account IDs.
A non-positive account ID currently passes through and only fails at request time. Validate it in the constructor to catch misuse earlier.
Proposed fix
public function __construct(ConfigInterface $config, private int $accountId) { + if ($accountId <= 0) { + throw new \InvalidArgumentException('Account ID must be greater than 0.'); + } + parent::__construct($config); }📝 Committable suggestion
🤖 Prompt for AI Agents