DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
David Paluy
David Paluy

Posted on

Customizable Email sender address with Devise

When installing the devise gem in your Rails application, you will find the following:

Devise.setup do |config|
  # ...
  config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'
  # ...
end
Enter fullscreen mode Exit fullscreen mode

You can set static email, for example:

Devise.setup do |config|
  # ...
  config.mailer_sender = 'Acme Team <team@acme.com>'
  # ...
end
Enter fullscreen mode Exit fullscreen mode

But if you need to allow your back-office team member periodically to customize this parameter, how would you do this?

I recommend using rails-settings gem.

class Setting < RailsSettings::Base
  field :email_from, type: :string, default: 'Acme Team <team@acme.com>'
end
Enter fullscreen mode Exit fullscreen mode

Now, if you try adding this Setting.email_from to devise.rb initializer, you will get an error: You cannot use settings before Rails initialize. (RuntimeError).

To solve this problem, do the following:

Devise.setup do |config|
  # ...
  config.mailer_sender = ->(_devise_mapping) { SiteConfig.email_from }
  # ...
end
Enter fullscreen mode Exit fullscreen mode

Happy Hacking!

Top comments (0)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: