如何保护我的电子邮件地址免受垃圾邮件的侵害 [英] How to protect my e-mail address from spambots
本文介绍了如何保护我的电子邮件地址免受垃圾邮件的侵害的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道Rails提供了哪些功能来混淆电子邮件地址以保护其免受爬行器、垃圾邮件和邮件收集器的攻击,收集地址以发送垃圾邮件。
可能是我使用了错误的关键字,但实际上找不到宝石。
我发现了一个统计数据,比较了屏蔽邮件地址的不同方法: http://techblog.tilllate.com/2008/07/20/ten-methods-to-obfuscate-e-mail-addresses-compared/
我编写了一个合并了前两个方法的代码段。
被剪下的东西还不成熟,但无论如何我还是想分享一下,它可能是其他面临同样问题的人的一个起点。 (下一步是将已链接的地址替换为模糊的纯文本。)
在继续之前,我想了解一下Rails中的最佳实践是什么。这是一个常见的问题,我一定是错过了解决它的好机会!?
如果我使用我的方法,在我的应用中集成/触发它的最佳方式是什么?
过滤之前有什么吗?在渲染之前?差不多吧?
或者像我现在这样做,在视图中将其调用为helper_Methode?
甚至可以将其添加到字符串类…我的application_helper.rb
def obfuscate_emails(content, domain_prefix = 'nirvana', clss = 'maildecode')
# This shall protect emails from spam spiders/crawlers gathering emails from webpages
# Add the following SASS to your Stylesheets
#
# span.maildecode
# direction: rtl
# unicode-bidi: bidi-override
#
# Further more you might want to use Javascript(.erb) to add links to the email addresses like this
#
# $(document).ready(function() {
# function link_emails(subdomain){
# console.log("Find an replace reverse emails, fake subdomain is "+subdomain);
# $(".maildecode").each(function() {
# email = $(this).text().replace('.'+subdomain,'').split("").reverse().join("");
# console.log("- clean email is "+email);
# // $(this).html($(this).text().replace('.'+subdomain,'')); // uncomment if you like to clean up the html a bit
# $(this).wrap('<a href="mailto:'+email+'">');
# });
# }
#
# link_emails('<%= ENV['OBFUSCATE_EMAIL_SUBDOMAIN'] %>');
# });
#
# Thanks to
# http://techblog.tilllate.com/2008/07/20/ten-methods-to-obfuscate-e-mail-addresses-compared/
email_re = /[w.!#$%+-]+@[w-]+(?:.[w-]+)+/
content.scan(email_re).each do |mail|
obfuscate_mail = "<span class='#{clss}'>#{mail.reverse.split('@')[0]}<span style='display: none;'>.#{domain_prefix}</span>@#{mail.reverse.split('@')[1]}</span>"
content = content.sub(mail, obfuscate_mail)
end
content # use raw(obfuscate_emails(content)) otherwise rails will escape the html
end
推荐答案
只需使用Rails内置的mail_to
帮助器.
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-mail_to
mail_to 'email@here.com', 'click to email', :encode => .... # couple of encoding options
注意:这在Rails 4中不再起作用。来自文档:在Rails 4.0之前,mail_to提供了对地址进行编码的选项,以阻止电子邮件收集器。要利用这些选项,请安装actionview-encoded_mail_to gem。(感谢@zwippie)
这篇关于如何保护我的电子邮件地址免受垃圾邮件的侵害的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文