devise icon indicating copy to clipboard operation
devise copied to clipboard

feat: Allow computed method for To header in mailers

Open boypie510 opened this issue 4 months ago • 0 comments

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

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

boypie510 avatar Aug 16 '25 15:08 boypie510