Skip to content

Conversation

@boypie510
Copy link

@boypie510 boypie510 commented Aug 16, 2025

Description

This PR adds support for computed To header in Devise mailers, allowing users to customize the recipient field using default :to declarations, similar to how from and reply_to already work.

Problem

Currently, Devise hardcodes the to field to resource.email and doesn't respect custom default :to values set in mailer classes. This prevents users from:

  • Formatting recipient as "Name " for better display
  • Using dynamic recipient logic based on user attributes
  • Customizing the to field like other mailer headers

Solution

Add a single line to check default_params[:to] in the headers_for method:

headers.delete(:to) if default_params[:to]

This follows the same pattern already used for from and reply_to fields.

Example Usage

class ApplicationDeviseMailer < Devise::Mailer
  default to: -> { email_address_with_name(resource.email, resource.name) }
end

Related Issues

Fixes the issue described in: Allow providing computed method for To header

Checklist

  • Tests added/updated
  • Code follows existing style guidelines
  • No breaking changes introduced

@boypie510 boypie510 changed the title feat: allow computed method for To header in mailers feat: Allow computed method for To header in mailers Aug 16, 2025
Comment on lines -43 to -44
assert mail.from, "[email protected]"
assert mail.reply_to, "[email protected]"
Copy link
Author

Choose a reason for hiding this comment

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

https://docs.ruby-lang.org/en/2.1.0/Test/Unit/Assertions.html

These assertions will ALWAYS pass

assert mail.from - checks if mail.from is truthy (which it always is)
"[email protected]" - this second argument is completely ignored

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant