Easy Email Address Encoder

By Jesse Kochis (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:

Download code <?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:

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


In your view, you can do this:
Download code
<?php e($mailto->encode($mail$text'test foo', array('subject'=>'subject''body'=>'body'))) ?>

or this:
Download code
<?php e($mailto->encode($mail$text'test foo')) ?>

or this:
Download code
<?php e($mailto->encode($mail$text)) ?>

or just this:
Download code
<?php e($mailto->encode($mail)) ?>

You'll get something like:
Download code
<?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 720

CakePHP Team Comments Author Comments
 

Question

1 problems

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
Posted Jun 26, 2008 by Mark
 

Comment

2 Wrong file location

Mark: add it to the views/helper folder.
Posted Jun 26, 2008 by James Logsdon
 

Comment

3 oh yeah

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 Jun 26, 2008 by Mark
 

Comment

4 Updated for clarity

Thanks, Guys. I updated the article to be a little more descriptive.

Posted Jun 29, 2008 by Jesse Kochis
 

Comment

5 This is awesome

Thanks so much for posting this.
Posted Aug 14, 2008 by Andrew Kolesnikov