Skip to main content
  1. Open Source/

Snippet: A Liquid Filter for obfuscating an Email Address

A Liquid Filter for obfuscating an Email Address (can be used with Jekyll aswell).
1 forks
2 files

In Jekyll set a variable for the mail, e.g. in the `_config.yml`


then use it in your page

    Reach me under:	{{ | mailObfuscate }}
which will generate the following HTML

    <a href="#" data-contact="bmFtZUBtYWlsLmNvbQ== " target="_blank" onfocus="this.href = 'mailto:' + atob(">    
        <script type="text/javascript">document.write(atob("bmFtZUBtYWlsLmNvbQ== "));</script>

This uses some simple obfuscation techniques of url encode and base64 encode the mail and use JS to support the link and write it to HTML programmatically. This is certainly not bulletproof, but a good shield and in combination with a good spam filter this will fix your problem with mail crawlers.

These techniques are partly from

mail_obfuscate.rb #

require "base64"
require "uri"

module ObfuscateMailAddress
  def mailObfuscate(input)
    base64Mail = Base64.strict_encode64(Addressable::URI.encode(input))

    # See
    output = "<a href=\"#\" "
    output += "data-contact=\"#{base64Mail}\" target=\"_blank\" "
    output += "onfocus=\"this.href = 'mailto:' + atob(\">"
    output += "<script type=\"text/javascript\">document.write(atob(\"#{base64Mail}\"));</script></a>"
    return output