module ActionView::Helpers::UrlHelper
def mail_to(email_address, name = nil, html_options = {})
:subject => "This is an example email"
mail_to "me@domain.com", "My email", :cc => "ccaddress@domain.com",
# => me_at_domain_dot_com
mail_to "me@domain.com", nil, :replace_at => "_at_", :replace_dot => "_dot_", :class => "email"
# => My email
mail_to "me@domain.com", "My email", :encode => "hex"
# =>
mail_to "me@domain.com", "My email", :encode => "javascript"
# => me@domain.com
mail_to "me@domain.com"
==== Examples
* :bcc - Blind Carbon Copy additional recipients on the email.
* :cc - Carbon Copy addition recipients on the email.
* :body - Preset the body of the email.
* :subject - Preset the subject line of the email.
string given as the value.
obfuscate the +email_address+ by substituting the . in the email with the
+email_address+ is used for the link label. You can use this option to
* :replace_dot - When the link +name+ isn't provided, the
given as the value.
obfuscate the +email_address+ by substituting the @ sign with the string
+email_address+ is used for the link label. You can use this option to
* :replace_at - When the link +name+ isn't provided, the
encode the +email_address+ before outputting the mailto link.
the page if the user has JavaScript disabled. Passing "hex" will hex
eval it into the DOM of the page. This method will not show the link on
Passing "javascript" will dynamically create and encode the mailto link then
* :encode - This key will accept the strings "javascript" or "hex".
==== Options
the email itself by passing special keys to +html_options+.
+mail_to+ has several methods for hindering email harvesters and customizing
HTML attributes for the link can be passed in +html_options+.
also used as the name of the link unless +name+ is specified. Additional
Creates a mailto link tag to the specified +email_address+, which is
def mail_to(email_address, name = nil, html_options = {}) email_address = html_escape(email_address) 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") extras = [] extras << "cc=#{Rack::Utils.escape(cc).gsub("+", "%20")}" unless cc.nil? extras << "bcc=#{Rack::Utils.escape(bcc).gsub("+", "%20")}" unless bcc.nil? extras << "body=#{Rack::Utils.escape(body).gsub("+", "%20")}" unless body.nil? extras << "subject=#{Rack::Utils.escape(subject).gsub("+", "%20")}" unless subject.nil? extras = extras.empty? ? '' : '?' + html_escape(extras.join('&')) 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") string = '' if encode == "javascript" "document.write('#{content_tag("a", name || email_address_obfuscated.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe))}');".each_byte do |c| string << sprintf("%%%x", c) end "<script type=\"#{Mime::JS}\">eval(decodeURIComponent('#{string}'))</script>".html_safe 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_safe, html_options.merge("href" => "#{string}#{extras}".html_safe) else content_tag "a", name || email_address_obfuscated.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe) end end