instance method
mail_to
Ruby on Rails 4.0.13
Since v2.2.3Signature
mail_to(email_address, name = nil, html_options = {}, &block)
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 customizing the email itself by passing special keys to html_options.
Options
-
:subject- Preset the subject line of the email. -
:body- Preset the body of the email. -
:cc- Carbon Copy additional recipients on the email. -
:bcc- Blind Carbon Copy additional recipients on the email.
Obfuscation
Prior to Rails 4.0, mail_to provided options for encoding the address in order to hinder email harvesters. To take advantage of these options, install the actionview-encoded_mail_to gem.
Examples
mail_to "me@domain.com" # => <a href="mailto:me@domain.com">me@domain.com</a> mail_to "me@domain.com", "My email" # => <a href="mailto:me@domain.com">My email</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>
You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
<%= mail_to "me@domain.com" do %>
<strong>Email me:</strong> <span>me@domain.com</span>
<% end %>
# => <a href="mailto:me@domain.com">
<strong>Email me:</strong> <span>me@domain.com</span>
</a>
Parameters
-
email_addressreq -
nameopt = nil -
html_optionsopt = {} -
blockblock
Source
# File actionpack/lib/action_view/helpers/url_helper.rb, line 451
def mail_to(email_address, name = nil, html_options = {}, &block)
email_address = ERB::Util.html_escape(email_address)
html_options, name = name, nil if block_given?
html_options = (html_options || {}).stringify_keys
extras = %w{ cc bcc body subject }.map { |item|
option = html_options.delete(item) || next
"#{item}=#{Rack::Utils.escape_path(option)}"
}.compact
extras = extras.empty? ? '' : '?' + ERB::Util.html_escape(extras.join('&'))
html_options["href"] = "mailto:#{email_address}#{extras}".html_safe
content_tag(:a, name || email_address.html_safe, html_options, &block)
end
Defined in actionpack/lib/action_view/helpers/url_helper.rb line 451
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::UrlHelper