We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24495
    • 407 Posts
    Hi, I’m new in modx (and sorry if my English is terrible). Here’s a suggestion in a snippet for protecting email addresses against spambots/harvesters:

    # written by Frank Burian, modified by Rainer Schlegel
    #
    # displays email address as you want (see parameter "coding")
    # the email link itself is md5 coded with "mailto" as unicode (otherwise rendering fails)
    #
    # usage: [[AntispamEmail? &mailadr=`[email protected]` &coding=`unicode`]]
    #
    # parameters:
    # mailadr - email address you wish to scramble
    # coding - output of visible email link text
    # - "unicode" -> email address output as unicode (browser renders it as visible text, spambot may fail)
    # - "atdot"   -> email address output as "test at domain dot tld"
    # - "both"    -> both :-)
    
    $mailadr = isset($mailadr)? $mailadr : '[email protected]';
    $coding = isset($coding)? $coding : 'unicode';
    
    $spamprotect = no_spam($mailadr);
    $spamprotect2 = no_spam2($mailadr,$coding);
    
    function no_spam($spamprotect) {
    $str = "";
    $a = unpack("C*", $spamprotect);
    foreach ($a as $b)
    $str .= sprintf("%%%X", $b);
    return $str;
    }
    
    function no_spam2($address,$coding)
    {
      $address=trim($address);
      $output="";
      switch($coding)
      {
        case 'unicode':
          for ($i=0;$i<strlen($address);$i++) $output.=str_replace(substr($address,$i,1),"&#".ord(substr($address,$i,1)).";",substr($address,$i,1));
        break;
        case 'atdot':
          $output=preg_replace('#(.*)\@(.*)\.(.*)#','\\1 at \\2 dot \\3',$address);
        break;
        case 'both':
          $address=preg_replace('#(.*)\@(.*)\.(.*)#','\\1 [at] \\2 [punkt] \\3',$address);
          for ($i=0;$i<strlen($address);$i++) $output.=str_replace(substr($address,$i,1),"&#".ord(substr($address,$i,1)).";",substr($address,$i,1));
        break;
      }
      return $output;
    }
    
    echo "<a href='mailto:".$spamprotect."'>".$spamprotect2."</a>";
    


    For me it works ok: try it for yourself.

    regards, Rainer
      • 32982
      • 674 Posts
      great is a good snippet whithout js i test it soon
      thanks for share
      hi
        Jabiertxof (formerly XYZVISUAL)
        My bussines: http://marker.es
        https://www.youtube.com/user/jabiertxof/videos
        • 24495
        • 407 Posts
        Another little modification. At this time the snippet shows the "real" email address as mail link text. With the little mod it shows whatever you want.

        Usage:
        [[AntispamEmail? &mailadr=`[email protected]` &coding=`anything`]]
        with "anything" as unicode / atdot / both / custom text


        E.g. if you set the &coding as "say hello to me" the mail link text will show this.

        # written by Frank Burian, modified by Rainer Schlegel
        #
        # displays email address as you want (see parameter "coding")
        # the email link itself is md5 coded with "mailto" as unicode (otherwise rendering fails)
        #
        # parameters:
        # mailadr - email address you wish to scramble
        # coding (optional, if isn't present the default is "unicode") - output of visible email link text:
        # - "unicode" -> email address output as unicode (browser renders it as visible text, spambot may fail)
        # - "atdot"   -> email address output as "test [at] domain [dot] tld"
        # - "both"    -> both :-)
        # - other text, for instance "contact", "say hello to me" ;-)
        
        $mailadr = isset($mailadr)? $mailadr : '[email protected]';
        $coding = isset($coding)? $coding : 'unicode';
        
        function no_spam($spamprotect) {
        $str = "";
        $a = unpack("C*", $spamprotect);
        foreach ($a as $b)
        $str .= sprintf("%%%X", $b);
        return $str;
        }
        
        function no_spam2($address,$coding)
        {
          $address=trim($address);
          $output="";
          switch($coding)
          {
            case 'unicode':
              for ($i=0;$i<strlen($address);$i++) $output.=str_replace(substr($address,$i,1),"&#".ord(substr($address,$i,1)).";",substr($address,$i,1));
        	break;
            case 'atdot':
              $output=preg_replace('#(.*)\@(.*)\.(.*)#','\\1 [at] \\2 [dot] \\3',$address);
        	break;
            case 'both':
              $address=preg_replace('#(.*)\@(.*)\.(.*)#','\\1 [at] \\2 [dot] \\3',$address);
              for ($i=0;$i<strlen($address);$i++) $output.=str_replace(substr($address,$i,1),"&#".ord(substr($address,$i,1)).";",substr($address,$i,1));
        	break;
        	default:
              for ($i=0;$i<strlen($coding);$i++) $output.=str_replace(substr($coding,$i,1),"&#".ord(substr($coding,$i,1)).";",substr($coding,$i,1));
        	break;
          }
          return $output;
        }
        
        $spamprotect = no_spam($mailadr);
        $spamprotect2 = no_spam2($mailadr,$coding);
        
        echo "<a href='mailto:".$spamprotect."'>".$spamprotect2."</a>";
        


        greetings, Rainer