In your view : <%= js_antispam_email_link('username@domain.com') %>
Code for your helper
# Takes in an email address and (optionally) anchor text,
# its purpose is to obfuscate email addresses so spiders and
# spammers can't harvest them.
def js_antispam_email_link(email, linktext=email)
user, domain = email.split('@')
user = html_obfuscate(user)
domain = html_obfuscate(domain)
# if linktext wasn't specified, throw encoded email address builder into js document.write statement
linktext = "'+'#{user}'+'@'+'#{domain}'+'" if linktext == email
rot13_encoded_email = rot13(email) # obfuscate email address as rot13
out = "#{linktext}<br/><small>#{user}(at)#{domain}</small>\n" # js disabled browsers see this
out += "// <![CDATA[
\n"
out += " \n"
out += " string = '#{rot13_encoded_email}'.replace(/[a-zA-Z]/g, function(c){ return String.fromCharCode((c = (c = c.charCodeAt(0) + 13) ? c : c - 26);});\n"
out += " document.write('#{linktext}'); \n"
out += " //\n"
out += "
// -->
// ]]>\n"
return out
endprivate
# Rot13 encodes a string
def rot13(string)
string.tr "A-Za-z", "N-ZA-Mn-za-m"
end# HTML encodes ASCII chars a-z, useful for obfuscating
# an email address from spiders and spammers
def html_obfuscate(string)
output_array = []
lower = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z)
upper = %w(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
char_array = string.split('')
char_array.each do |char|
output = lower.index(char) + 97 if lower.include?(char)
output = upper.index(char) + 65 if upper.include?(char)
if output
output_array << "&##{output};"
else
output_array << char
end
end
return output_array.join
end
1 Response to "Anti Spam Email For Your Rails Page with Javascript"
There is an art form around sending legitimate email marketing messages that I really how do you stop spam never grasped when I ran my old email marketing company. Now that I've been away from it for so long, I realize just how I disdain poorly sent emails
Post a Comment