-
Notifications
You must be signed in to change notification settings - Fork 17
MT-22401: Add Email Campaigns API #72
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
Draft
Rabsztok
wants to merge
2
commits into
main
Choose a base branch
from
MT-22401-php-email-campaigns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 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
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,226 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Mailtrap\Config; | ||
| use Mailtrap\DTO\Request\EmailCampaign\CreateEmailCampaign; | ||
| use Mailtrap\DTO\Request\EmailCampaign\EmailCampaignInterface; | ||
| use Mailtrap\DTO\Request\EmailCampaign\ReplyTo; | ||
| use Mailtrap\DTO\Request\EmailCampaign\TemplateAttributes; | ||
| use Mailtrap\DTO\Request\EmailCampaign\UpdateEmailCampaign; | ||
| use Mailtrap\Helper\ResponseHelper; | ||
| use Mailtrap\MailtrapGeneralClient; | ||
|
|
||
| require __DIR__ . '/../../vendor/autoload.php'; | ||
|
|
||
| $accountId = (int) $_ENV['MAILTRAP_ACCOUNT_ID']; | ||
| $config = new Config($_ENV['MAILTRAP_API_KEY']); // your API token from https://mailtrap.io/api-tokens | ||
| $emailCampaigns = (new MailtrapGeneralClient($config))->emailCampaigns($accountId); | ||
|
|
||
| /** | ||
| * Get a paginated list of email campaigns (newest first). | ||
| * Optional filters: $perPage (max 100, default 50), $search (name filter), $token (page number). | ||
| * | ||
| * The response is wrapped in `{ data: [...], pagination }`. | ||
| * | ||
| * GET https://mailtrap.io/api/email_campaigns | ||
| */ | ||
| try { | ||
| $response = $emailCampaigns->getEmailCampaigns(perPage: 50, search: 'Spring', token: 1); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Create a new email campaign in the `draft` state (wrapped in `data`). | ||
| * `name`, `mailsend_domain_id` (a UUID), `from_local_part` and a template `subject` are required. | ||
| * | ||
| * POST https://mailtrap.io/api/email_campaigns | ||
| */ | ||
| try { | ||
| $response = $emailCampaigns->createEmailCampaign(new CreateEmailCampaign( | ||
| name: 'Spring Sale', | ||
| mailsendDomainId: $_ENV['MAILTRAP_DOMAIN_ID'], // set this environment variable with your sending domain UUID | ||
| fromLocalPart: 'news', | ||
| templateAttributes: new TemplateAttributes(subject: 'Spring is here — 30% off'), | ||
| fromDisplayName: 'Acme Marketing', | ||
| replyTo: new ReplyTo( | ||
| displayName: 'Acme Support', | ||
| localPart: 'support', | ||
| domain: 'acme.com', | ||
| ), | ||
| )); | ||
|
|
||
| $emailCampaign = ResponseHelper::toArray($response); | ||
| $emailCampaignId = $emailCampaign['data']['id']; // reused by all the examples below | ||
|
|
||
| var_dump($emailCampaign); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
|
|
||
| exit(1); // the examples below operate on the campaign created here | ||
| } | ||
|
|
||
| /** | ||
| * Get a single email campaign by ID (wrapped in `data`). | ||
| * | ||
| * GET https://mailtrap.io/api/email_campaigns/{email_campaign_id} | ||
| */ | ||
| try { | ||
| $response = $emailCampaigns->getEmailCampaign($emailCampaignId); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Update an existing `draft` email campaign (PATCH — only provided attributes change). | ||
| * Add the design and pick the audience; `contact_list_ids`/`contact_segment_ids` are the | ||
| * full set of included lists/segments (`[]` clears them). | ||
| * | ||
| * PATCH https://mailtrap.io/api/email_campaigns/{email_campaign_id} | ||
| */ | ||
| try { | ||
| $response = $emailCampaigns->updateEmailCampaign( | ||
| $emailCampaignId, | ||
| new UpdateEmailCampaign( | ||
| name: 'Spring Sale (updated)', | ||
| deliveryMode: EmailCampaignInterface::DELIVERY_MODE_GRADUAL, | ||
| deliveryOptions: ['emails_per_hour' => 1000], | ||
| templateAttributes: new TemplateAttributes( | ||
| subject: 'New subject', | ||
| bodyHtml: '<html><body><h1>Hi {{first_name}}!</h1>' | ||
| . '<p><a href="__unsubscribe_url__">Unsubscribe</a></p></body></html>', | ||
| mergeTags: ['first_name'], | ||
| ), | ||
| contactListIds: [55, 56], // replace with your contact list IDs | ||
| contactSegmentIds: [12], // replace with your contact segment IDs | ||
| ) | ||
| ); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Schedule a `draft` campaign to start sending at a future time (no more than 1 month ahead). | ||
| * Accepts an ISO 8601 string or a \DateTimeInterface. The scheduled time is reported back in | ||
| * `current_state_metadata.scheduled_at`. | ||
| * | ||
| * POST https://mailtrap.io/api/email_campaigns/{email_campaign_id}/schedule | ||
| */ | ||
| try { | ||
| $response = $emailCampaigns->scheduleEmailCampaign( | ||
| $emailCampaignId, | ||
| new DateTimeImmutable('+1 week', new DateTimeZone('UTC')) | ||
| ); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Reset a `scheduled` campaign back to the `draft` state. | ||
| * | ||
| * POST https://mailtrap.io/api/email_campaigns/{email_campaign_id}/reset | ||
| */ | ||
| try { | ||
| $response = $emailCampaigns->resetEmailCampaign($emailCampaignId); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Schedule the campaign again so the cancellation below acts on a `scheduled` campaign. | ||
| * | ||
| * POST https://mailtrap.io/api/email_campaigns/{email_campaign_id}/schedule | ||
| */ | ||
| try { | ||
| $response = $emailCampaigns->scheduleEmailCampaign( | ||
| $emailCampaignId, | ||
| new DateTimeImmutable('+1 day', new DateTimeZone('UTC')) | ||
| ); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Cancel a `scheduled` campaign, returning it to the `draft` state. | ||
| * | ||
| * POST https://mailtrap.io/api/email_campaigns/{email_campaign_id}/cancel | ||
| */ | ||
| try { | ||
| $response = $emailCampaigns->cancelEmailCampaign($emailCampaignId); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Start sending a `draft` campaign immediately. | ||
| * | ||
| * POST https://mailtrap.io/api/email_campaigns/{email_campaign_id}/start | ||
| */ | ||
| try { | ||
| $response = $emailCampaigns->startEmailCampaign($emailCampaignId); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Terminate a campaign that is currently sending (`started`, `queued`, or `paused`). | ||
| * | ||
| * POST https://mailtrap.io/api/email_campaigns/{email_campaign_id}/terminate | ||
| */ | ||
| try { | ||
| $response = $emailCampaigns->terminateEmailCampaign($emailCampaignId); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Get aggregated performance statistics for a campaign (wrapped in `data`). | ||
| * All counts and rates are `0` when the campaign has never been started. Use the optional | ||
| * `YYYY-MM-DD` date parameters to narrow the aggregation window. | ||
| * | ||
| * GET https://mailtrap.io/api/email_campaigns/{email_campaign_id}/stats | ||
| */ | ||
| try { | ||
| $response = $emailCampaigns->getEmailCampaignStats( | ||
| $emailCampaignId, | ||
| startDate: (new DateTimeImmutable('-30 days'))->format('Y-m-d'), | ||
| endDate: (new DateTimeImmutable('today'))->format('Y-m-d') | ||
| ); | ||
|
|
||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
| /** | ||
| * Delete an email campaign. The campaign must not be in a sending state. | ||
| * Returns `204 No Content` with an empty body. | ||
| * | ||
| * DELETE https://mailtrap.io/api/email_campaigns/{email_campaign_id} | ||
| */ | ||
| try { | ||
| $response = $emailCampaigns->deleteEmailCampaign($emailCampaignId); | ||
|
|
||
| var_dump($response->getStatusCode()); // 204 | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.