Use Smarty's Mail-Encoding in Zend Framework

| No Comments

As soon as you have a nice front end of your website, which should be functional (and compliant with current regulations), you will have to provide a contact form or a email-address somewhere. I have decided to show only my email-address, but wanted to protect it at least with a minimal encryption from mail-harvesters.

In an earlier version of my page I have used the template engine Smarty, which has some functions to encrypt your email-address with JavaScript. And I have to say that this worked fine for me so far. So my current page uses Zend Framework and I am not the great JavaScript-Coder. This is why I decided to try to merge this two codes together. And it works. Here comes how.

First you need to download Smarty.
You need a running Zend Framework Site.
And an editor.


After you have downloaded and extracted the Smarty Template Engine, you need to identify the corresponding function. It is called Mailto and is in the file function.mailto.php. The path is:

/Your Path To Smarty/Smarty/plugins/function.mailto.php

Insert function.mailto.php as View Helper

Open the file in your preferred editor. And you should see the code and the function declaration.

Now first a few preparations for the Zend Framework:
Create a new View Helper and call it however you want. I will call mine EncodeMail, so the file created is EncodeMail.php under the View Helper Directory (see Zend Reference Guide).

Now go back to the Smarty function.mailto.php file in your editor. Mark all text and copy it (e.g. with Ctrl + C).

Open the EncodeMail View Helper and paste (e.g. with Ctrl + V) the previously marked Smarty function into this file.
Save the View Helper.

Make minor adjustments to the View Helper

Now you need to make some minor adjustments/corrections in the new created View Helper.

The first thing is to make it a class. As you can see from the code it is "only" a function. So you need to declare it as a class.

class xxx_View_Helper_encodeMail
{
// Here is the smarty code
}

Next step is to redeclare the Smarty function to be compatible with the current environment from Zend Framework.
The original function looks like this:

function smarty_function_mailto($params, &$smarty)
{
...
}

Change it to:

public function encodeMail($params)
	{
...
}

As you can see do I have omitted one parameter (actually one by reference) to the $smarty variable. This makes a few more adjustments necessary.
You need to change this expression:

$smarty->trigger_error("Any String Message");

To this one:

trigger_error("A String Message");

You need to do this for all the lines where this code is shown. This is used by Smarty for the error-handling, which Zend Framework does different.

If you have done this, save your new View Helper and use it with:

<?php
$contact = array('address' => 'me@mail.com', 'encode' => 'javascript', 'text' => 'Contact Me');
echo $this->encodeMail($contact);
?>

Quite simple, isn't it?

Leave a comment