Send email in Ruby on Rails Use mail_to tag instead of link_to!

January 31, 2008

 
No Gravatar

I know there’s more than a few of you who have been using link_to for email links. No more! By using mail_to, you can create a mailto link tag to the specified email address, which is also used as the name of the link unless you specify otherwise. 

An added beneift is that mail_to can accept an encoded hex or javascript parameter, thereby encoding your email addresses to prevent email harvesters. Another thing you’ll notice is that link_to and mail_to are reversed:

1
2
3
4
5
6
7
8
9
10
# using link_to
link_to "Email Jonathan", "mailto:hello@jonathansng.com"
 
# using mail_to
# note the reversal
mail_to "hello@jonathansng.com", "Email Jonathan"
 
# using mail_to with :encode => "hex"
# returns <a href="mailto:%68%65%6c%6c%6f@%6a%6f%6e%61%74%68%61%6e%73%6e%67.%63%6f%6d">Email Jonathan</a>
mail_to "hello@jonathansng.com", "Email Jonathan", :encode => "hex"

Read up on mail_to

blog comments powered by Disqus