Skip to content

Conversation

@ghiculescu
Copy link
Contributor

@ghiculescu ghiculescu commented Nov 10, 2025

When an email is sent, Mailtrap sends back a message ID. If you use the Mailtrap client directly you can get this ID back. But it's not easily accessible if using Action Mailer.

This makes it hard to connect an incoming webhook to the email that was sent.

This PR adds some extra code to the Action Mailer delivery method, to store the message ID as a header on the message.

In another project that uses Postmark, I saw code that did this:

mail = FooMailer.foo_email(recipient)
message = mail.deliver_now
postmark_message_id = message["X-PM-Message-ID"].to_s

Thanks to this in their gem: https://github.com/ActiveCampaign/postmark-gem/blob/a50ff395d04b1c0050d11ce0ece70543c31f7f8f/lib/postmark/api_client.rb#L390

This basically does the equivalent.

Summary by CodeRabbit

  • Bug Fixes
    • Mail delivery now properly captures and records message IDs in the X-MT-Message-IDs header, enabling tracking of sent messages.

@coderabbitai
Copy link

coderabbitai bot commented Nov 10, 2025

Walkthrough

The deliver! method in the delivery method now captures message IDs from the API response and assigns them to the X-MT-Message-IDs message header for tracking purposes. A corresponding test assertion validates this header is set correctly after delivery.

Changes

Cohort / File(s) Summary
Message ID header tracking
lib/mailtrap/action_mailer/delivery_method.rb, spec/mailtrap/action_mailer/delivery_method_spec.rb
Implementation now wraps client.send(mail) in a tap block to capture the response and assign message_ids to the X-MT-Message-IDs message header. Test updated to assert the header contains the expected comma-joined message IDs.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Verify the tap block implementation correctly captures and assigns response message IDs
  • Confirm the header value format (comma-joined string) matches expectations
  • Ensure no edge cases with empty or missing message IDs in the response

Poem

🐰 A rabbit hops through mail so bright,
Tracking IDs in headers tight,
Each message marked with numbered care,
Breadcrumbs left throughout the air! 📬✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: writing message IDs to the message header on successful send. It is specific, directly related to the changeset, and communicates the primary purpose of the PR.
Description check ✅ Passed The description provides motivation (accessibility of message IDs for webhook integration), explains the changes, and includes a practical code example. However, it does not include the 'How to test' section from the template with test checkboxes, and lacks the 'Images and GIFs' section.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 10b7fbc and ccd38a9.

📒 Files selected for processing (2)
  • lib/mailtrap/action_mailer/delivery_method.rb (1 hunks)
  • spec/mailtrap/action_mailer/delivery_method_spec.rb (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
lib/mailtrap/action_mailer/delivery_method.rb (1)
lib/mailtrap/client.rb (1)
  • send (159-166)
🔇 Additional comments (2)
lib/mailtrap/action_mailer/delivery_method.rb (1)

17-19: LGTM! Clean implementation with proper return value preservation.

The tap block elegantly captures the response while preserving backward compatibility with the method's return value. The Mail gem automatically converts the array in resp[:message_ids] to a comma-separated string when assigned to a header, which aligns with the test expectations.

spec/mailtrap/action_mailer/delivery_method_spec.rb (1)

51-51: LGTM! Appropriate test coverage for the new feature.

The assertion correctly validates that the X-MT-Message-IDs header is populated with the comma-separated message IDs after delivery, using the standard Mail gem pattern with .to_s for string conversion.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@i7an i7an merged commit c8ea472 into mailtrap:main Nov 10, 2025
5 checks passed
@i7an
Copy link
Contributor

i7an commented Nov 10, 2025

@ghiculescu a new version is released. Feel free to update the gem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants