# Auto detect URL for ActionMailer?

**URL:** <https://www.ruby-forum.com/t/auto-detect-url-for-actionmailer/85824>\
**Category:** Rails\
**Created:** [February 16, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/auto-detect-url-for-actionmailer/85824 "2007-02-16T16:45:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [February 16, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/auto-detect-url-for-actionmailer/85824/1 "2007-02-16T16:45:53Z")

</div>

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}](http://localhost:3000/account/activate/#%7Buser.activation_code%7D)”  
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

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [February 16, 2007, 5:37pm UTC](https://www.ruby-forum.com/t/auto-detect-url-for-actionmailer/85824/2 "2007-02-16T17:37:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [February 17, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/auto-detect-url-for-actionmailer/85824/3 "2007-02-17T16:16:38Z")

</div>

request.host much? 🙂

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [February 20, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/auto-detect-url-for-actionmailer/85824/4 "2007-02-20T21:35:00Z")

</div>

Russell N. wrote:

> request.host much? 🙂

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!

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [February 21, 2007, 12:16am UTC](https://www.ruby-forum.com/t/auto-detect-url-for-actionmailer/85824/5 "2007-02-21T00:16:35Z")

</div>

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
