MT-22401: Add Email Campaigns API - #72
Conversation
Decisions:
- Request bodies are flat JSON — no ['email_campaign' => ...] wrapper; mailsend_domain_id is a string UUID
- Single-campaign and stats responses documented/tested as {data: ...} envelopes; delete returns 204 with no body
- Typed TemplateAttributes (subject/body_html/body_text/merge_tags, no template id) and ReplyTo DTOs replace raw arrays
- contact_list_ids/contact_segment_ids kept behind the !== null filter so [] still clears the audience
- Five lifecycle endpoints (start/schedule/cancel/terminate/reset); schedule accepts string or DateTimeInterface; stats gains optional start_date/end_date
📝 WalkthroughWalkthroughThis PR adds an account-scoped Email Campaigns API to the Mailtrap SDK. The implementation includes request DTOs with flat serialization, an API client with list, CRUD, lifecycle, scheduling, and statistics operations, comprehensive unit tests, a complete usage example, and documentation updates. ChangesEmail Campaigns
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant Application
participant MailtrapGeneralClient
participant EmailCampaign
participant MailtrapAPI
Application->>MailtrapGeneralClient: emailCampaigns(accountId)
MailtrapGeneralClient-->>Application: EmailCampaign client
Application->>EmailCampaign: createEmailCampaign(CreateEmailCampaign)
EmailCampaign->>MailtrapAPI: POST /api/email_campaigns
MailtrapAPI-->>EmailCampaign: campaign response
EmailCampaign-->>Application: handled response
Application->>EmailCampaign: scheduleEmailCampaign(id, datetime)
EmailCampaign->>MailtrapAPI: POST /api/email_campaigns/{id}/schedule
MailtrapAPI-->>EmailCampaign: scheduled campaign response
EmailCampaign-->>Application: handled response
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@examples/email-campaigns/all.php`:
- Around line 58-71: Update the example flow around createEmailCampaign and all
dependent operations to extract and reuse the created campaign ID from response
data instead of hard-coded 4567. Check the create response for failure and stop
before update, lifecycle, stats, or delete actions when creation does not
succeed; preserve the existing operation order and output behavior for
successful creation.
- Around line 173-181: Update the campaign lifecycle sequence around
resetEmailCampaign so resetEmailCampaign is called while the campaign remains
scheduled. Move the reset demonstration immediately after scheduling, then
schedule the campaign again before the cancellation, start, and termination
steps, or use a separate campaign identifier for the later sequence.
- Line 60: Update the email campaign configuration around mailsendDomainId and
the related contact list/segment IDs to remove account-scoped hardcoded
fallbacks. Require the corresponding environment or configuration values, or
mark each fallback explicitly as a placeholder that must be replaced before use.
- Around line 16-18: Cast the $_ENV['MAILTRAP_ACCOUNT_ID'] value to an integer
when assigning $accountId, then pass that integer unchanged to
MailtrapGeneralClient::emailCampaigns().
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: b23572ec-0178-4077-92d2-77e45f57fc88
📒 Files selected for processing (13)
CHANGELOG.mdREADME.mdexamples/README.mdexamples/email-campaigns/all.phpsrc/Api/General/EmailCampaign.phpsrc/DTO/Request/EmailCampaign/CreateEmailCampaign.phpsrc/DTO/Request/EmailCampaign/EmailCampaignInterface.phpsrc/DTO/Request/EmailCampaign/ReplyTo.phpsrc/DTO/Request/EmailCampaign/TemplateAttributes.phpsrc/DTO/Request/EmailCampaign/UpdateEmailCampaign.phpsrc/MailtrapGeneralClient.phptests/Api/General/EmailCampaignTest.phptests/MailtrapGeneralClientTest.php
Decisions: - Reuse the created campaign ID across all dependent example steps; exit early when creation fails - Reorder lifecycle to schedule -> reset -> schedule again -> cancel -> start -> terminate so every action runs from a valid state - Require MAILTRAP_DOMAIN_ID (no arbitrary UUID fallback) and mark contact list/segment IDs as placeholders - Cast MAILTRAP_ACCOUNT_ID to int (strict_types + int param) - Derive stats date window at runtime instead of fixed dates
|
Addressed all 4 CodeRabbit findings in d7105c4:
Also derived the stats date window at runtime ( Verified: full phpunit suite (418 tests) green, psalm clean. |
Motivation
MT-22401
Port the Email Campaigns public API (MT-21113) to the PHP SDK.
Changes
emailCampaigns()to the General client with the full contract: list (per_page/search/token), get, create, update, delete (204), the five lifecycle actions (start,schedule,cancel,terminate,reset), and stats with an optional date rangemailsendDomainId, newTemplateAttributes(subject/bodyHtml/bodyText/mergeTags) andReplyToDTOs, audience id arrays (empty array clears the audience),rapid/gradualdelivery modes, 10-value state constantsexamples/email-campaigns/all.php+ README/CHANGELOG entriesHow to test
examples/email-campaigns/all.phpwith a real API token and a verified sending domain — create a draft, update design/audience, schedule + cancel, fetch stats, deletedatapayload and a lifecycle 422 surfaces the API error messageSummary by CodeRabbit
New Features
Tests