public Method

UrlHelper.mail_to(email_address, name = nil, html_options = {})

Contents:

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(unescape('%64%6f%63...%6d%65%6e'))</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">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>

Source Code

# File action_view/helpers/url_helper.rb, line 372
def mail_to(email_address, name = nil, html_options = {})
  html_options = html_options.stringify_keys
  encode = html_options.delete("encode").to_s
  cc, bcc, subject, body = html_options.delete("cc"), html_options.delete("bcc"), html_options.delete("subject"), html_options.delete("body")

  string = ''
  extras = ''
  extras << "cc=#{CGI.escape(cc).gsub("+", "%20")}&" unless cc.nil?
  extras << "bcc=#{CGI.escape(bcc).gsub("+", "%20")}&" unless bcc.nil?
  extras << "body=#{CGI.escape(body).gsub("+", "%20")}&" unless body.nil?
  extras << "subject=#{CGI.escape(subject).gsub("+", "%20")}&" unless subject.nil?
  extras = "?" << extras.gsub!(/&?$/,"") unless extras.empty?

  email_address = email_address.to_s

  email_address_obfuscated = email_address.dup
  email_address_obfuscated.gsub!(/@/, html_options.delete("replace_at")) if html_options.has_key?("replace_at")
  email_address_obfuscated.gsub!(/\./, html_options.delete("replace_dot")) if html_options.has_key?("replace_dot")

  if encode == "javascript"
    "document.write('#{content_tag("a", name || email_address, html_options.merge({ "href" => "mailto:"+email_address+extras }))}');".each_byte do |c|
      string << sprintf("%%%x", c)
    end
    "<script type=\"#{Mime::JS}\">eval(unescape('#{string}'))</script>"
  elsif encode == "hex"
    email_address_encoded = ''
    email_address_obfuscated.each_byte do |c|
      email_address_encoded << sprintf("&#%d;", c)
    end

    protocol = 'mailto:'
    protocol.each_byte { |c| string << sprintf("&#%d;", c) }

    email_address.each_byte do |c|
      char = c.chr
      string << (char =~ /\w/ ? sprintf("%%%x", c) : char)
    end
    content_tag "a", name || email_address_encoded, html_options.merge({ "href" => "#{string}#{extras}" })
  else
    content_tag "a", name || email_address_obfuscated, html_options.merge({ "href" => "mailto:#{email_address}#{extras}" })
  end
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.