-
Notifications
You must be signed in to change notification settings - Fork 17
Add tracking opt-outs and create-suppression endpoints #78
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
3 commits
Select commit
Hold shift + click to select a range
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
File renamed without changes.
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,74 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Mailtrap\Config; | ||
| use Mailtrap\DTO\Request\TrackingOptOut\CreateTrackingOptOut; | ||
| use Mailtrap\DTO\Request\TrackingOptOut\TrackingOptOutsFilter; | ||
| use Mailtrap\Helper\ResponseHelper; | ||
| use Mailtrap\MailtrapSendingClient; | ||
|
|
||
| require __DIR__ . '/../../vendor/autoload.php'; | ||
|
|
||
| $config = new Config($_ENV['MAILTRAP_API_KEY']); #your API token from here https://mailtrap.io/api-tokens | ||
| $domainId = (int) $_ENV['MAILTRAP_DOMAIN_ID']; | ||
|
|
||
| $trackingOptOuts = (new MailtrapSendingClient($config))->trackingOptOuts(); | ||
|
|
||
| /** | ||
| * Create a tracking opt-out | ||
| * | ||
| * POST https://mailtrap.io/api/tracking_opt_outs | ||
| */ | ||
| try { | ||
| $response = $trackingOptOuts->createTrackingOptOut( | ||
| new CreateTrackingOptOut(email: 'tracked@example.com', domainId: $domainId) | ||
| ); | ||
|
|
||
| // print the response body (array) | ||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Get tracking opt-outs | ||
| * | ||
| * GET https://mailtrap.io/api/tracking_opt_outs | ||
| * | ||
| * Returns up to 1000 records per request. When `last_id` is not null, pass it | ||
| * back as a filter to fetch the next page. | ||
| */ | ||
| try { | ||
| $response = $trackingOptOuts->getTrackingOptOuts(); | ||
|
|
||
| // OR filter by email and creation time | ||
| $response = $trackingOptOuts->getTrackingOptOuts( | ||
| new TrackingOptOutsFilter( | ||
| email: 'tracked@example.com', | ||
| startTime: '2025-01-01T00:00:00Z', | ||
| endTime: '2025-12-31T23:59:59Z' | ||
| ) | ||
| ); | ||
|
|
||
| // print the response body (array) | ||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Delete a tracking opt-out | ||
| * | ||
| * DELETE https://mailtrap.io/api/tracking_opt_outs/{tracking_opt_out_id} | ||
| */ | ||
| try { | ||
| $response = $trackingOptOuts->deleteTrackingOptOut('64d71bf3-1276-417b-86e1-8e66f138acfe'); | ||
|
|
||
| // print the response body (array) — the deleted record | ||
| var_dump(ResponseHelper::toArray($response)); | ||
| } 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
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,71 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Mailtrap\Api\Sending; | ||
|
|
||
| use Mailtrap\Api\AbstractApi; | ||
| use Mailtrap\DTO\Request\TrackingOptOut\CreateTrackingOptOut; | ||
| use Mailtrap\DTO\Request\TrackingOptOut\TrackingOptOutsFilter; | ||
| use Psr\Http\Message\ResponseInterface; | ||
|
|
||
| /** | ||
| * Class TrackingOptOut | ||
| */ | ||
| class TrackingOptOut extends AbstractApi implements SendingInterface | ||
| { | ||
| /** | ||
| * List email addresses that have opted out of open and click tracking. | ||
| * The endpoint returns up to 1000 records per request; pass the previous | ||
| * response's last_id to fetch the next page. | ||
| * | ||
| * @param TrackingOptOutsFilter|null $filter | ||
| * @return ResponseInterface | ||
| */ | ||
| public function getTrackingOptOuts(?TrackingOptOutsFilter $filter = null): ResponseInterface | ||
| { | ||
| return $this->handleResponse( | ||
| $this->httpGet( | ||
| $this->getBasePath(), | ||
| $filter ? $filter->toArray() : [] | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Add an email address to the tracking opt-out list for a sending domain. | ||
| * | ||
| * @param CreateTrackingOptOut $trackingOptOut | ||
| * @return ResponseInterface | ||
| */ | ||
| public function createTrackingOptOut(CreateTrackingOptOut $trackingOptOut): ResponseInterface | ||
| { | ||
| return $this->handleResponse( | ||
| $this->httpPost( | ||
| path: $this->getBasePath(), | ||
| body: $trackingOptOut->toArray() | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Remove an email address from the tracking opt-out list so open and click | ||
| * tracking can apply again. | ||
| * | ||
| * @param string $trackingOptOutId | ||
| * @return ResponseInterface | ||
| */ | ||
| public function deleteTrackingOptOut(string $trackingOptOutId): ResponseInterface | ||
| { | ||
| return $this->handleResponse( | ||
| $this->httpDelete( | ||
| sprintf('%s/%s', $this->getBasePath(), $trackingOptOutId) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| private function getBasePath(): string | ||
| { | ||
| return sprintf('%s/api/tracking_opt_outs', $this->getHost()); | ||
| } | ||
| } |
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,43 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Mailtrap\DTO\Request\Suppression; | ||
|
|
||
| /** | ||
| * Class CreateSuppression | ||
| * | ||
| * Adds an email address to the account's suppression list. | ||
| */ | ||
| final class CreateSuppression implements SuppressionInterface | ||
| { | ||
| /** | ||
| * @param string $email Email address to suppress | ||
| * @param int $domainId ID of the domain to suppress this email for | ||
| * @param string $sendingStream One of Suppression::SENDING_STREAM_* | ||
| * @param string|null $type One of Suppression::TYPE_*. The API defaults to | ||
| * "manual import" when omitted. | ||
| */ | ||
| public function __construct( | ||
| private string $email, | ||
| private int $domainId, | ||
| private string $sendingStream, | ||
| private ?string $type = null, | ||
| ) { | ||
| } | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| $payload = [ | ||
| 'email' => $this->email, | ||
| 'domain_id' => $this->domainId, | ||
| 'sending_stream' => $this->sendingStream, | ||
| ]; | ||
|
|
||
| if ($this->type !== null) { | ||
| $payload['type'] = $this->type; | ||
| } | ||
|
|
||
| return $payload; | ||
| } | ||
| } |
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,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Mailtrap\DTO\Request\Suppression; | ||
|
|
||
| /** | ||
| * Suppression vocabulary: sending streams and suppression types. | ||
| */ | ||
| final class Suppression | ||
| { | ||
| public const SENDING_STREAM_TRANSACTIONAL = 'transactional'; | ||
| public const SENDING_STREAM_BULK = 'bulk'; | ||
|
|
||
| public const TYPE_HARD_BOUNCE = 'hard bounce'; | ||
| public const TYPE_UNSUBSCRIPTION = 'unsubscription'; | ||
| public const TYPE_SPAM_COMPLAINT = 'spam complaint'; | ||
| public const TYPE_MANUAL_IMPORT = 'manual import'; | ||
|
|
||
| private function __construct() | ||
| { | ||
| } | ||
| } |
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,11 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Mailtrap\DTO\Request\Suppression; | ||
|
|
||
| use Mailtrap\DTO\Request\RequestInterface; | ||
|
|
||
| interface SuppressionInterface extends RequestInterface | ||
| { | ||
| } |
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Delete the record created by this example.
The create call does not provide the ID used by the delete call. The fixed UUID will normally fail on a clean account and does not delete the record created above. Capture the created record ID, or select it from the list response, before calling
deleteTrackingOptOut().🤖 Prompt for AI Agents