Skip to content

Set all mail attributes including template #70

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

i7an
Copy link
Contributor

@i7an i7an commented Aug 21, 2025

Motivation

Provide a possibility to use templates from action mailer. This approach is questionable, but let's give our users a "sharp knife" and let them decide how to use it.

Changes

  • Set template_uuid and template_variables from message

How to test

  • Send a message with action mailer
  • Send a message with action mailer specifying template_uuid

Images and GIFs

N/A

Summary by CodeRabbit

  • New Features

    • Adds support for email templates when building mail from messages, capturing template UUID and template variables from headers.
  • Tests

    • Introduces test coverage validating template UUID and variables handling.
  • Documentation

    • Updates changelog with a new entry for version 2.4.1.
  • Chores

    • Bumps version to 2.4.1.

Copy link

coderabbitai bot commented Aug 21, 2025

Walkthrough

Updates Mailtrap::Mail.from_message to extract template_uuid and template_variables from headers and adjust address extraction to accept header objects. Adds specs for template fields, bumps version to 2.4.1, and updates the changelog accordingly.

Changes

Cohort / File(s) Summary
Mail parsing and template headers
lib/mailtrap/mail.rb
from_message now reads template_uuid/template_variables from headers; prepare_addresses now accepts a header object and derives addresses via address_list(header)&.addresses; call sites updated to pass header fields directly.
Specs for template support
spec/mailtrap/mail_spec.rb
Adds contexts verifying template_uuid and template_variables are set when present in message headers.
Version bump
lib/mailtrap/version.rb
Updates Mailtrap::VERSION to 2.4.1.
Changelog
CHANGELOG.md
Adds 2.4.1 entry noting template_uuid/template_variables set when building from Mail::Message.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor App
  participant MailMessage as Mail::Message
  participant Mail as Mailtrap::Mail
  participant Base as Mailtrap::Mail::Base

  App->>Mail: from_message(MailMessage)
  activate Mail
  Mail->>MailMessage: read headers: from, to, cc, bcc
  Mail->>Mail: prepare_addresses(from_header)
  Mail->>Mail: prepare_addresses(to_header)
  Mail->>Mail: prepare_addresses(cc_header)
  Mail->>Mail: prepare_addresses(bcc_header)
  Mail->>MailMessage: read headers: template_uuid, template_variables
  Mail->>Base: new(...addresses..., template_uuid, template_variables)
  deactivate Mail
  Base-->>App: Mail object
  note over Mail,Base: prepare_addresses now accepts header objects
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • mklocek
  • IgorDobryn
  • ayanko

Poem

A whisk of bytes, a hop through mail,
I tuck a template in the trail.
Headers whisper UUIDs,
Variables rustle like springtime leaves.
Version bumped—thump, thump—my feet!
Changelog carrots: crisp and neat.
Delivering joy, complete. 🥕🐇

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch action-mailer-send-template

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@i7an i7an marked this pull request as ready for review August 21, 2025 05:47
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
CHANGELOG.md (1)

1-3: Changelog entry looks good; consider linking to the PR and clarifying Action Mailer context.

Optional polish:

  • Add a link to PR #70 next to the bullet so readers can trace discussion.
  • Wording tweak for consistency with prior entries that start with a verb (“Add/Set/Improve” is fine) and maybe call out “via Action Mailer headers” explicitly.

Example:

  • Set template_uuid and template_variables when building mail from Mail::Message (Action Mailer), see #70.
lib/mailtrap/mail.rb (2)

192-207: Handle hyphenated header names and avoid header echo for template fields.

Great addition exposing template_uuid/template_variables from Mail::Message. Two improvements to make this more robust:

  1. Action Mailer/custom headers are typically hyphenated (e.g., Template-UUID, Template-Variables, Custom-Variables). The current lookup uses underscored names, which may miss standard hyphenated headers. Support both to be resilient.

Apply this minimal diff inside the changed block:

-          custom_variables: message['custom_variables']&.unparsed_value,
-          template_uuid: message['template_uuid']&.unparsed_value,
-          template_variables: message['template_variables']&.unparsed_value
+          custom_variables: (message['Custom-Variables'] || message['custom_variables'])&.unparsed_value,
+          template_uuid: (message['Template-UUID'] || message['template_uuid'])&.unparsed_value,
+          template_variables: (message['Template-Variables'] || message['template_variables'])&.unparsed_value
  1. Since you now extract these into dedicated attributes, consider excluding Template-UUID and Template-Variables from mail.headers to prevent them from being forwarded as arbitrary headers. This mirrors how category/custom variables are handled. You can extend the removal list (outside this hunk), for example:
# Add alongside other SPECIAL_HEADERS:
# templateuuid and templatevariables use normalized, hyphen-less names
SPECIAL_HEADERS = %w[from to cc bcc subject category customvariables contenttype templateuuid templatevariables].freeze

Optionally, if callers may pass JSON strings for variables, you might parse JSON when the header value is a String; otherwise leave as-is (happy to draft a helper if you want).

Would you like me to open a follow-up PR/commit that (a) adds those two tokens to SPECIAL_HEADERS and (b) adds specs asserting that template headers are not present in mail.headers?


224-227: Switch to header-driven address parsing LGTM.

Accepting a Mail::Field and delegating to address_list(header)&.addresses is cleaner and centralizes error handling. This also preserves the prior behavior for empty headers ([]nil for from.first) and keeps invalid headers raising via address_list.

Two tiny nits (optional):

  • Update the YARD doc for the return type to reflect an Array of Hashes, e.g., @return [Array<Hash>].
  • Consider renaming param to address_header for clarity.
spec/mailtrap/mail_spec.rb (1)

146-156: Good coverage for template fields; add checks for hyphenated headers and header removal.

To lock in the intended behavior and the suggested robustness from the implementation comment:

  • Add a spec where Template-UUID/Template-Variables are set via hyphenated names to ensure they’re recognized.
  • Assert that mail.headers does not include Template-UUID/Template-Variables once extracted.

Example additions (outside this hunk):

context 'when template headers are hyphenated' do
  let(:message_params) do
    super().merge(
      'Template-UUID' => '6b9c8a83-8d75-4b7c-9e9d-9b1db88c0e2d',
      'Template-Variables' => { first_name: 'Jane' }
    )
  end

  its(:template_uuid) { is_expected.to eq('6b9c8a83-8d75-4b7c-9e9d-9b1db88c0e2d') }
  its(:template_variables) { is_expected.to eq('first_name' => 'Jane') }

  specify 'template headers are not forwarded' do
    expect(mail.headers.keys).not_to include('Template-UUID', 'Template-Variables')
  end
end
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 433e942 and e9c1478.

⛔ Files ignored due to path filters (1)
  • Gemfile.lock is excluded by !**/*.lock
📒 Files selected for processing (4)
  • CHANGELOG.md (1 hunks)
  • lib/mailtrap/mail.rb (2 hunks)
  • lib/mailtrap/version.rb (1 hunks)
  • spec/mailtrap/mail_spec.rb (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
lib/mailtrap/mail.rb (1)
lib/mailtrap/mail/base.rb (1)
  • attachments (71-73)
🪛 LanguageTool
CHANGELOG.md

[grammar] ~1-~1: There might be a mistake here.
Context: ## [2.4.1] - 2025-08-21 - Set template_uuid and `template_variab...

(QB_NEW_EN)

🔇 Additional comments (1)
lib/mailtrap/version.rb (1)

4-4: Version bump consistent with PR scope.

Matches CHANGELOG entry for 2.4.1 dated 2025-08-21. Please ensure the gemspec (if present) and any docs/snippets referencing the version are updated in the release commit.

If you want, I can generate a quick script to grep for stale version strings across the repo.

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.

2 participants