Can I Set default_url_options for ActionMailer to request.base_url or request.domain?

I'm working on a Sandbox version of an active website. The sandbox version of the website is pointing to a different domain name. I'm working on emails that generate a URL using the value from config.action_mailer.default_url_options in my mailers.

In config/environments/production.rb I have the domain name for the active website as the host. I would like to change it where it gets the value from request.domain or request.base_url. Is this possible? If so how do I write the value for host? I've tried the following but got errors when I did the rake command.

  config.action_mailer.default_url_options = { :host => "#{request.base_url}" }

  config.action_mailer.default_url_options = { :host => request.base_url }

NameError: undefined local variable or method `request' for #<myrailsappname::Application:0x007faafc702bb0>
Did you mean?  require

I've done web searches and have not found any examples of where I can use request.base_url or request.domain.

Upvotes: 1

Views: 779

Answers (1)

Sourabh Ukkalgaonkar
Sourabh Ukkalgaonkar

Reputation: 156

you may try this..

In an application_controller.rb before_filter add:

ActionMailer::Base.default_url_options = {:host => request.host_with_port}

One other way is ..

class UserMailer < ActionMailer::Base

def welcome_email(user, request)
    @user = user
    @url  = user_url(@user, host: request.host_with_port ) #option2 (do this for each link)
    mail(:to => user.email,
         :subject => "Welcome to My Awesome Site")
  end
end

Upvotes: 1

Related Questions