If you put your email address on a public web page, you can usually be sure to get tons of spam from there on, because address harvesters will sooner or later visit your page and recognize the email address.
There are different solutions to prevent harvesters recognizing an email address. I personally don’t like the use of images to display email addresses or the use of feedback forms instead displaying addresses at all. These methods have a negative impact on the site usability, since a visitor wouldn’t be able to easily copy an email address to his email application anymore.
So here’s an easy way to use scrambled email addresses in Rails views.
Most tools that scramble text, simply use HTML character entity references (e.g.
So I made it a bit harder. The following helper method scrambles a text without the character codes itself in the result. It does this by storing character codes relative to the previous one and starting with a random value. Simple search-and-replace operations aren’t sufficient to decode texts scrambled this way. To use it, simply drop the following method into application_helper.rb:
To scramble some text in a view, simply call
safe_text with the text to be scrambled:
Contact us: <%= safe_text('firstname.lastname@example.org') %>
The result will look like this:
Update: I did some tests on different email address scrambling methods. Results are available here.