3 Clever Ways To Protect Your Email From Spammers

Posted on

Ever get spam mail in your inbox? I know before I implemented these methods below, I did.

1. CSS Text Direction

Its simple, reverse the text so bots can’t read it.
The CSS

.reverse{
	direction: rtl; 
	unicode-bidi: bidi-override;
}

The HTML

<span class="reverse">[email protected]</span>

Which looks like this: [email protected]

2. PHP Obfuscation

This one is a little more involved, but this one is click-able.

<?php

function obfuscate($string)
{
	$safe = '';
	foreach (str_split($string) as $letter)
	{
		switch (rand(1, 3))
		{
			// HTML entity code
			case 1:
				$safe .= '&#'.ord($letter).';';
			break;

			// Hex character code
			case 2:
				$safe .= '&#x'.dechex(ord($letter)).';';
			break;

			// Raw (no) encoding
			case 3:
				$safe .= $letter;
		}
	}

	return $safe;
}

function email($email)
{
	// Make sure the at sign is always obfuscated
	return str_replace('@', '&#64;', obfuscate($email));
}

function mailto($email, $title = NULL)
{
	// Obfuscate email address
	$email = email($email);

	if ($title === NULL)
	{
		// Use the email address as the title
		$title = $email;
	}

	return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email.'">'.$title.'</a>';
}

?>

Functions taken from Kohana Framework

The implementation using php:

<?php 
$email = 'info(at)ziggidycreative.com';
echo mailto($email); ?>

Which looks like this:

<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;&#x7a;&#97;&#99;&#107;&#64;zi&#103;g&#105;&#100;&#121;&#99;&#114;e&#97;&#x74;iv&#x65;&#46;&#x63;&#x6f;m">&#x7a;&#97;&#99;&#107;&#64;zi&#103;g&#105;&#100;&#121;&#99;&#114;e&#97;&#x74;iv&#x65;&#46;&#x63;&#x6f;m</a>

3. CSS Hiding

Trick the spammers by hiding some silly text in the middle of your email.
The CSS

.hidden{
	display: none;
}

The HTML

<span class="email">info@<span class="hidden">istoppedyourspam</span>ziggidycreative.com</span>

This is what the bot will see: [email protected] but non spammers will see your actual email.

Conclusion

You just learned 3 simple ways to obfuscate your email from spammers. Use these wisely and you will be spam free!

  • Chris

    Thanks, going with the second solution!