Code snippets for symfony 1.x


Encode URL and caption like Smarty does ;)

This is very simple and effective mail helper. Original code is taken from Smarty's mailto plugin.
This helper scrambles your email addresses so those nasty web bots 'can not' harvest them.

In /lib/helper/MailEncodeHelper.php

function encode_mail($Address, $Caption=null)
  if ($Caption == null)
    $Caption = $Address;
  $string = 'document.write(\'<a href="mailto:'.$Address.'">'.$Caption.'</a>\');';
  $js_encode = '';
  for ($x=0; $x < strlen($string); $x++)
    $js_encode .= '%' . bin2hex($string[$x]);
  return '<script type="text/javascript">eval(unescape(\''.$js_encode.'\'))</script>';

In template

<?php use_helper('MailEncode') ?>
My email: <?php echo encode_mail(''); ?>  
My email: <?php echo encode_mail('', 'Damjan Malis'); ?>
by Damjan Malis on 2006-07-19, tagged email  encode  helper 

Comments on this snippet

gravatar icon
#1 Romain Dorgueil on 2006-07-25 at 05:46

wasn't the symfony's mail_to helper supposed to do the same?



gravatar icon
#2 Olivier Verdier on 2006-07-25 at 06:31

Well, there are thousands of ways to encode an email address. This snippet is one, and mail_to is another (without javascript). And neither of those are very convincing, if you ask me...

gravatar icon
#3 Damjan Malis on 2006-07-26 at 10:37

Yes, it's true that mail_to helper does the same thing. The reason why I've posted this snippet is that I wasn't aware of mail_to() existance :)

My bad.

gravatar icon
#4 Olivier Verdier on 2006-07-27 at 07:28

No, the mail_to helper does not do the same thing. It does not use javascript, whereas this snippet does. mail_to only "encodes" email addresses using xhtml codes.