7

In User model, each user belong to different domain/host. I want to set it to be different from address on the basis of user's domain. Can I set this in User model somewhere, or how can I make the senders address dynamic according to user's domain.

We set devise default sender address in app/config/initializer/devise.rb like

Devise.setup do |config|
  config.mailer_sender = SOME EMAIL ADDRESS
end
Deej
  • 5,334
  • 12
  • 44
  • 68
kashif
  • 1,097
  • 4
  • 17
  • 32

3 Answers3

10

I bumped into this because I wanted to pull the from address from I18n, but the initializer was running before I18n was setup. This was the simplest solution for me:

config.mailer_sender = Proc.new { I18n.t('mailers.from') }
kross
  • 3,627
  • 2
  • 32
  • 60
2

To use the Mailer helper functions by Devise, extend the devise mailer, and override the methods/mails that need a different dynamic sender:

class CustomDeviseMailer < Devise::Mailer
  def confirmation_instructions(record, token, opts={})
    @token = token
    opts[:from] = "Dynamic Sender <dynamic@foo.com>"
    devise_mail(record, :confirmation_instructions, opts)
  end
end

And configure it in you devise.rb:

config.mailer = "CustomDeviseMailer"

Note: If you don't need a dynamic sender, just define the sender in devise.rb:

config.mailer_sender = "Static sender <static@foo.com>"
wspruijt
  • 1,037
  • 11
  • 15
1

you can set the mail.from per email basis

class UserMailer <ActionMailer::Base

def notification_email(user)
  mail(to:example@example.com, from:user.email, ...)
end

That will override your default settings.

I think you can change this settings in config/initializers/devise.rb

  # Configure the class responsible to send e-mails.
  # config.mailer = "Devise::Mailer"
   config.mailer = "UserMailer"

to your customized mailer.

Henry
  • 1,047
  • 9
  • 8
  • if I call devise built-in emails sending functions for "Forgot your password" or "change your password". where I can set sender email address setting on the basis of users host ? How can the above mentioned example will help and work. can you please explain ? – kashif Jul 04 '13 at 07:45
  • i edit my answer above, check devise documentation, it should be quite easy to do. – Henry Jul 04 '13 at 07:57
  • also check this post [link](http://stackoverflow.com/questions/5679571/how-can-i-customize-devise-to-send-password-reset-emails-using-postmark-mailer) – Henry Jul 04 '13 at 08:01
  • Thanks Henry - it worked. infact the above mentioned link helped. Thanks again man – kashif Jul 04 '13 at 10:32