You can always roll your own but, first, that gem definitely works. Here's what I did...
Using the Gem
I added the gem to a Rails 4.2 app:
# Gemfile
gem 'actionview-encoded_mail_to'
I installed it:
$ bundle install
I entered the console:
$ rails console
I made the helper methods available in the console:
include ActionView::Helpers::UrlHelper
Called the mail_to
helper with the same arguments as the example:
mail_to "me@domain.com", nil, replace_at: "_at_", replace_dot: "_dot_", class: "email"
...and got this result:
"<a class=\"email\" href=\"mailto:me@domain.com\">me_at_domain_dot_com</a>"
That "me_at_domain_dot_com"
looks properly obfuscated. What steps did you take?
Rolling your own obfuscation
It would be a trivial string substitution to obfuscate an email string, we can just use sub
:
def obfuscate_email(email, replace_at: '_at_', replace_dot: '_dot_')
email.sub("@", replace_at).sub ".", replace_dot
end
I tested that out in the console:
obfuscate_email "me@example.com"
# => "me_at_example_dot_com"
You can place that method in application_helper.rb
and use it in any of your views:
link_to obfuscate_email(user.email), "mailto:#{user.email}"
# => "<a href=\"mailto:me@example.com\">me_at_example_dot_com</a>"
Note that the href must be the unobfuscated email in order for it to work properly.
Towards a more complete mail_to
helper
def obfuscated_mail_to(email, options = {})
link_to obfuscate_email(email), "mail_to:#{email}", options
end
Testing that in the console:
obfuscated_mail_to "me@example.com", class: "email"
=> "<a class=\"email\" href=\"mail_to:me@example.com\">me_at_example_dot_com</a>"
Hope that helps!