mail_to(email_address, name = nil, html_options = {}) public

Creates a mailto link tag to the specified email_address, which is also used as the name of the link unless name is specified. Additional HTML attributes for the link can be passed in html_options.

mail_to has several methods for hindering email harvesters and customizing the email itself by passing special keys to html_options.

Options

  • :encode - This key will accept the strings “javascript” or “hex”. Passing “javascript” will dynamically create and encode the mailto link then eval it into the DOM of the page. This method will not show the link on the page if the user has JavaScript disabled. Passing “hex” will hex encode the email_address before outputting the mailto link.

  • :replace_at - When the link name isn’t provided, the email_address is used for the link label. You can use this option to obfuscate the email_address by substituting the @ sign with the string given as the value.

  • :replace_dot - When the link name isn’t provided, the email_address is used for the link label. You can use this option to obfuscate the email_address by substituting the . in the email with the string given as the value.

  • :subject - Preset the subject line of the email.

  • :body - Preset the body of the email.

  • :cc - Carbon Copy addition recipients on the email.

  • :bcc - Blind Carbon Copy additional recipients on the email.

Examples

mail_to "me@domain.com"
# => <a href="mailto:me@domain.com">me@domain.com</a>

mail_to "me@domain.com", "My email", :encode => "javascript"
# => <script type="text/javascript">eval(decodeURIComponent('%64%6f%63...%27%29%3b'))</script>

mail_to "me@domain.com", "My email", :encode => "hex"
# => <a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a>

mail_to "me@domain.com", nil, :replace_at => "_at_", :replace_dot => "_dot_", :class => "email"
# => <a href="mailto:me@domain.com" class="email">me_at_domain_dot_com</a>

mail_to "me@domain.com", "My email", :cc => "ccaddress@domain.com",
         :subject => "This is an example email"
# => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
Show source
Register or log in to add new notes.
January 20, 2009
5 thanks

Javascript encoding DOES work!

grosser assertion is false :

mail_to('xxx@xxx.com', nil, :encode => :javascript)
# => "<script type=\"text/javascript\">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%78%78%78%40%78%78%78%2e%63%6f%6d%22%3e%78%78%78%40%78%78%78%2e%63%6f%6d%3c%2f%61%3e%27%29%3b'))</script>"

Use “nil” as the second parameter to tell mail_to that you want to use the first parameter for both text and email link

August 1, 2008
2 thanks

email_to('xxx@xxx','xxx@xxx',:encode=>'javascript') does NOT work

as i always want the email in the link text, email_to does not help me…

so here comes the rescue!

#http://unixmonkey.net/?p=20
# Takes in an email address and (optionally) anchor text,
# its purpose is to obfuscate email addresses so spiders and
# spammers can't harvest them.
def js_antispam_email_link(email, linktext=email)
  user, domain = email.split('@')
  # if linktext wasn't specified, throw email address builder into js document.write statement
  linktext = "'+'#{user}'+'@'+'#{domain}'+'" if linktext == email 
  out =  "<noscript>#{linktext} #{user}(ät)#{domain}</noscript>\n"
  out += "<script language='javascript'>\n"
  out += "  <!--\n"
  out += "    string = '#{user}'+'@'+''+'#{domain}';\n"
  out += "    document.write('<a href='+'m'+'a'+'il'+'to:'+ string +'>#{linktext}</a>'); \n"
  out += "  //-->\n"
  out += "</script>\n"
  return out
end