Easy Email Address Encoder

This article is also available in the following languages:
By jeko
While there are countless solutions to protecting email addresses from page scanners, here's an easy helper to add another line of defense.
This handy helper takes a normal email address and scrambles it up into a mix of text, decimal, and hexadecimal characters.

Add the following code to views/helpers/mailto.php (you will need to create the file).

Helper Class:

<?php 
class MailtoHelper extends AppHelper
{
    function 
encode($mail$text=""$class=""$params=array())
    {
        
$encmail ="";
        for(
$i=0$i<strlen($mail); $i++)
        {
            
$encMod rand(0,2);
            switch (
$encMod) {
            case 
0// None
                
$encmail .= substr($mail,$i,1);
                break;
            case 
1// Decimal
                
$encmail .= "&#".ord(substr($mail,$i,1)).';';
                break;
            case 
2// Hexadecimal
                
$encmail .= "&#x".dechex(ord(substr($mail,$i,1))).';';
                break;
            }
        }

        if(!
$text)
        {
            
$text $encmail;
        }
        
$encmail "&#109;&#97;&#105;&#108;&#116;&#111;&#58;".$encmail;
        
$querystring "";
        foreach(
$params as $key=>$val)
        {
            if(
$querystring){
                
$querystring .= "&$key=".rawurlencode($val);
            } else {
                
$querystring "?$key=".rawurlencode($val);
            }
        }
        return 
"<a class='$class' href='$encmail$querystring'>$text</a>";
    }
}
?>

Add the helper to your controller like so:

Controller Class:

<?php 
class MyController extends AppController
{
    var 
$name 'MyController';
    var 
$helpers = array('Mailto');
}
?>


In your view, you can do this:

<?php e($mailto->encode($mail$text'test foo', array('subject'=>'subject''body'=>'body'))) ?>

or this:

<?php e($mailto->encode($mail$text'test foo')) ?>

or this:

<?php e($mailto->encode($mail$text)) ?>

or just this:

<?php e($mailto->encode($mail)) ?>

You'll get something like:

<?php e($mailto->encode('example@example.com')) ?>
// outputs <a class='' href='&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#101;&#120;a&#109;&#112;&#x6c;&#x65;&#x40;&#x65;&#x78;&#97;mp&#108;e.&#99;&#x6f;&#x6d;'>&#101;&#120;a&#109;&#112;&#x6c;&#x65;&#x40;&#x65;&#x78;&#97;mp&#108;e.&#99;&#x6f;&#x6d;</a>    

See?

Easy.

Comments

  • Posted 08/14/08 01:40:01 AM
    Thanks so much for posting this.
  • Posted 06/26/08 08:28:16 PM
    i did that..
    but i didnt know, that i had to put it in the controller -> array (...)
    etc.
    after another half n hour i figured it out.

    for newbys there is no explanation on this page here^^
    thx anyway
    • Posted 06/29/08 07:53:13 AM
      Thanks, Guys. I updated the article to be a little more descriptive.

  • Posted 06/26/08 08:22:09 PM
    Mark: add it to the views/helper folder.
  • Posted 06/26/08 06:34:40 PM
    sounds really nice!
    but how can i get it to work

    just adding a file 'mailto.php' in the views folder didnt work

    it says:
    Fatal error: Call to a member function encode() on a non-object in ...\cake\app\views\posts\index.thtml on line 24

    And it claims, that $mailto is not set or something.
    How do i know that anyway - that $mailto is the object now?

    i'm still kinda new to CakePHP^^
    thx, mark

Comments are closed for articles over a year old