Auto detect URL for ActionMailer?

Hi,

how can I call the url in an actionMailer definition - so that it
automatically detects what the rails application server is and puts it
in the url that is mailed to the client? (I don’t want to have to change
it every time the hostname is changed - I’d like the application to auto
detect what the host is - whether it’s localhost:3000 or my own
website…

example:

class UserNotifier < ActionMailer::Base

def signup_notification(user)
setup_email(user)
@subject += ‘Please activate your new account’
@body[:url] =
http://localhost:3000/account/activate/#{user.activation_code}
end

I think this has been answered on the forum already, but from the
answers I could find, I still can’t figure it out… I hope my question
is clear…

Thanks!
Dustin

I’m thinking the solution to my dilemma is to use the url_for function
from the UrlWriter module…

I haven’t figured it out yet - wondering if this is the right way to
go… isn’t there just an easy way to figure out what the domain is that
you’re in and pass it back to the application?

request.host much? :slight_smile:

Russell N. wrote:

request.host much? :slight_smile:

Thanks - that’s definitely what I want to do, but I can’t figure out how
to call it/where to call it… it doesn’t seem to work from the model.

we are sending a user activation confirmation e-mail from ActionMailer

We are trying to build a URL in the user_notifier.rb model based on the
hostname… how can I access the server’s hostname from a model?

Thanks!

Dustin A. wrote:

Hi,

how can I call the url in an actionMailer definition - so that it
automatically detects what the rails application server is and puts it
in the url that is mailed to the client?

As far as I know, you have to pass it in as a parameter, since it’s only
the controller that can accurately determine the url.

For example,

UserNotifier.deliver_signup_notification(
@user,
url_for(:controller => :account,
:action => :activate,
:code => activation_code(@user)
)

Then in your mailer method, you have something like:

def signup_notification(user, activation_url)
[…]
@body[‘url’] = activation_url
[…]
end

–Al Evans