'

How to protect an email from Spam scrambling it, an advanced approach for WordPress

Few days ago I published a function to scramble an email address whois purpose was simplicity. You can read more in Protect the email address from spam bots a reusable approach for WordPress.

Then I relized that I wanted more options and more elasticity.

Taking a step forward, I rewrote it with few enhancements. Still the goal is to make it as simple as possible.

I wanted these improvements:

  • The Admin email has to be used as default if no other email had been provided;
  • Possibility to use a custom text;
  • Possibility to define a class to style the output via CSS.

In this version I have also opted for masking the plain email inserting a pattern of periods and @ encapsulated between comments. It seems to be a good way to confuse a spam bot. An example of the output:

<!-- @ . -->i<!-- @ . -->n<!-- @ . -->f<!-- @ . -->o<!-- @ . -->@<!-- @ . -->e<!-- @ . -->m<!-- @ . -->a<!-- @ . -->i<!-- @ . -->l<!-- @ . -->.<!-- @ . -->c<!-- @ . -->o<!-- @ . -->m<!-- @ . -->

On the other hand it should be straight forward for you to implement your own scrambling set and pattern.

The final function

Writing down the nitty-gritty, this the final function.

/* Scramble Email
-----------------------------------------------------------*/
/*	source: http://carlorizzante.com
	it scrambles an email, if not provided any, 
	the admin email is used as set in settings */

function scramble_email($args = array()) {
	// setting the defaults
	$defaults = array(
		'email'				=> '',
		'alt_text'			=> '',
		'scramble'			=> true,
		'scrambling_set'	=> 'abcdefghijklmnopqrstuvxyz1234567890',
		'class'				=> 'scrambled-email'
	);
	// matching the setting provided with the defaults
	$args = wp_parse_args( $args, $defaults );
	//
	$email 			= $args['email'];
	$alt_text 		= $args['alt_text'];
	$scramble 		= $args['scramble'];
	$scrambling_set = $args['scrambling_set'];
	$class 			= $args['class'];
	// setting the variables
	if (!$email) {
		// the admin email address is used as default
		// as set in the Settings section on WordPress
		// but you can pass as argument an other email as well
		$email = get_bloginfo('admin_email');
	}
	// resetting for a new output
	$scrambled_email = null;
	$spanned_email = null;
	// scrambling
	for ($i=0; $i< (strlen($email)); $i++) {
		$x = substr($email, $i, 1);
		if($scramble){
			$spanned_email .= "<!-- @ . -->$x<!-- . @ -->";
		} else {
			$spanned_email .= $x;
		}
		$scrambled_email .= $x . substr($scrambling_set, ($i % strlen($scrambling_set)), 1);
	}
	if (!$alt_text) {
		// if no alternative text is provided
		$alt_text = $spanned_email;
	}
	// getting the result
	$v = "'$scrambled_email'";
	$z = "'$alt_text'";
	// printing the output
	$output = '<a class="' . $class. '" href="#">';
	$output .= '<span onclick="';
	$output .= "var adr=$v; var s = ''; for(i=0;i*2<adr.length;i++) s += adr[i*2]; document.location='mailto:'+s; return false\"><script>(((document).write))($z)</script></span>";
	$output .= '</a>';
	print $output;
}

How to use it?

The usage can vary depending by what is desirable. Let’s see few example.

Default settings

<?php scramble_email(); ?>

The output is the Admin email address, scrambled.

Styling

The visible output is encapsulated in a link with the class="scrambled-email". To style it, set the class .scrambled-email at wish via CSS. It is also possible to pass a different class name to the function.

<?php> scramble_email(array('class' => 'new-class')) ?>

Alternative text

<?php scramble_email(array('alt_text' => 'Write me!')); ?>

The default output is the sentence “Write me!”. The default email is the admin email address, as set up in Settings.

Alternative email

<?php scramble_email(array('email' => 'example@email.com')); ?>

On click this will be the email set as recipient.

Providing all the parameters

<?php $args = array(
	'email'				=> 'example@email.com',
	'alt_text'			=> 'Write me!',
	'scramble'			=> true,
	'scrambling_set'	=> 'ABNKDO@_DSAJSDS:K',
	'class'				=> 'my-class'
);
scramble_email($args); ?>

The above is an example in which all the default parameters are overridden.

Defaults

Useful to know, these below are the defaults setting.

<?php $defaults = array(
	'email'				=> '', // the Admin email is used
	'alt_text'			=> '', // the email is used as output
	'scramble'			=> true,
	'scrambling_set'	=> 'abcdefghijklmnopqrstuvxyz1234567890', // the default set
	'class'				=> 'scrambled-email' //the default class for the anchor
);

Please feel free to suggest ways to improve it.

Important note Credit has to be given to Mads Olsen who concretely supported me in the task.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>