module ActionView::Helpers::UrlHelper
def mail_to(email_address, name = nil, html_options = {}, &block)
Email me: me@domain.com
# =>
<% end %>
Email me: me@domain.com
<%= mail_to "me@domain.com" do %>
You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
# => me@domain.com
subject: "This is an example email"
mail_to "me@domain.com", cc: "ccaddress@domain.com",
# => My email
mail_to "me@domain.com", "My email"
# => me@domain.com
mail_to "me@domain.com"
==== Examples
install the +actionview-encoded_mail_to+ gem.
in order to hinder email harvesters. To take advantage of these options,
Prior to Rails 4.0, +mail_to+ provided options for encoding the address
==== Obfuscation
* :reply_to - Preset the +Reply-To+ field of the email.
* :bcc - Blind Carbon Copy additional recipients on the email.
* :cc - Carbon Copy additional recipients on the email.
* :body - Preset the body of the email.
* :subject - Preset the subject line of the email.
==== Options
passing special keys to +html_options+.
+mail_to+ has several methods for customizing the email itself by
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 = {}, &block) html_options, name = name, nil if name.is_a?(Hash) html_options = (html_options || {}).stringify_keys extras = %w{ cc bcc body subject reply_to }.map! { |item| option = html_options.delete(item).presence || next "#{item.dasherize}=#{ERB::Util.url_encode(option)}" }.compact extras = extras.empty? ? "" : "?" + extras.join("&") encoded_email_address = ERB::Util.url_encode(email_address).gsub("%40", "@") html_options["href"] = "mailto:#{encoded_email_address}#{extras}" content_tag("a", name || email_address, html_options, &block) end