heartcombo / devise

Flexible authentication solution for Rails with Warden.
http://blog.plataformatec.com.br/tag/devise/
MIT License
23.89k stars 5.54k forks source link

Dynamic definition of host mail.from and mail.reply_to #5592

Closed dvodvo closed 1 year ago

dvodvo commented 1 year ago

Current behavior

the initializer value for mail.from and mail.reply_to cannot be over-ridden. Or at the very least documentation for this is hard to find. Which is problematic for multi-tenant applications with distinct domains that are nonetheless all covered by SPF, DKIM and DMARC procedures covering the postfix send_only server.

It took quite a while until I stumbled upon this mechanism (see first line below) to set the url by altering the default create action of passwords_controller.rb

    Rails.application.routes.default_url_options[:host] = request.host
    sender = 'do-not-reply@' + request.host
#  here would be an ideal place to set the sender
    super

But I fail to wrangle a way to set the from and reply_to addresses. The auto-generated text help

  # Configure the e-mail address which will be shown in Devise::Mailer,
  # note that it will be overwritten if you use your own mailer class
  # with default "from" parameter.

is of use if one uses a distinct mailer class. But this is not the case here. Nor should it be required. A one-liner should be sufficient here (and more succinct than to generate a whole new mailer.)

What would the proper syntax be?

rafaelfranca commented 1 year ago

The right thing to do is to define your own mailer and configure devise to use it using Devise.mailer=.

Something like this should be more than enough:


class MyMultiTenantMailer < Devise::Mailer
  default from: -> { my_dynamic_method_to_get_the_from }, reply_to: -> { my_dynamic_method_to_get_the_reply_to }
end

Devise.mailer = "MyMultiTenantMailer"