kobaltz
kobaltz

Reputation: 7070

Return root url when sending ActionMailer email

I'm trying to set up my ActionMailer so that it will read the URL it is being posted from. When this application is deployed, it could end up on many different servers with different domain names. Instead of having the user go into the code and force enter the URL statically, I would like the reset password url to include the domain that it was generated from (including http:// or https://).

I've tried ::Rails.root, request.host_with_port and ::Rails.root_path within the Mailer, but none have produced results. request.host_with_port generates an undefined method error.

  def reset_password_email(user)
    @user = user
    @url  = "#{::Rails.root_path}/password_resets/#{user.reset_password_token}/edit"
    mail(:to => user.email,
         :subject => "Your password has been reset")
  end

Upvotes: 0

Views: 1284

Answers (1)

Sergey Kuznetsov
Sergey Kuznetsov

Reputation: 8721

I assume that you call reset_password_email(user) from one of your app controllers.

You can update this method definition and send with user the current host and port to it:

def reset_password_email(user, request)
  @user = user
  @url  = "#{request.protocol}#{request.host_with_port}/password_resets/#{user.reset_password_token}/edit"
  mail(:to => user.email, :subject => "Your password has been reset")
end

Don't forget to update your Controller's code.

Upvotes: 1

Related Questions