Skip to content

Commit

Permalink
Merge pull request #5595 from louim/bugfix/mailer-defaults-lambda-arity
Browse files Browse the repository at this point in the history
Make sure Mailer defaults :from and :reply_to are handled correctly
  • Loading branch information
rafaelfranca authored Jun 9, 2023
2 parents 9be24c0 + ed1c2a1 commit e81ec9c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
24 changes: 9 additions & 15 deletions lib/devise/mailers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,22 @@ def headers_for(action, opts)
subject: subject_for(action),
to: resource.email,
from: mailer_sender(devise_mapping),
reply_to: mailer_reply_to(devise_mapping),
reply_to: mailer_sender(devise_mapping),
template_path: template_paths,
template_name: action
}.merge(opts)
}
# Give priority to the mailer's default if they exists.
headers.delete(:from) if default_params[:from]
headers.delete(:reply_to) if default_params[:reply_to]

headers.merge!(opts)

@email = headers[:to]
headers
end

def mailer_reply_to(mapping)
mailer_sender(mapping, :reply_to)
end

def mailer_from(mapping)
mailer_sender(mapping, :from)
end

def mailer_sender(mapping, sender = :from)
default_sender = default_params[sender]
if default_sender.present?
default_sender.respond_to?(:to_proc) ? instance_eval(&default_sender) : default_sender
elsif Devise.mailer_sender.is_a?(Proc)
def mailer_sender(mapping)
if Devise.mailer_sender.is_a?(Proc)
Devise.mailer_sender.call(mapping.name)
else
Devise.mailer_sender
Expand Down
26 changes: 26 additions & 0 deletions test/mailers/mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,30 @@ def confirmation_instructions(record, token, opts = {})

assert mail.content_transfer_encoding, "7bit"
end

test "default values defined as proc with different arity are handled correctly" do
class TestMailerWithDefault < Devise::Mailer
default from: -> { computed_from }
default reply_to: ->(_) { computed_reply_to }

def confirmation_instructions(record, token, opts = {})
@token = token
devise_mail(record, :confirmation_instructions, opts)
end

private

def computed_from
"[email protected]"
end

def computed_reply_to
"[email protected]"
end
end

mail = TestMailerWithDefault.confirmation_instructions(create_user, "confirmation-token")
assert mail.from, "[email protected]"
assert mail.reply_to, "[email protected]"
end
end

0 comments on commit e81ec9c

Please sign in to comment.