Obfuscate email addresses

Some of my clients really don’t want to put their email addresses in clear text out in the public. After all, the internet is not very forgiving.

For me and my private projects, I’ve never taken this into consideration and I’ve never had any issues with bots finding and spamming me. But it sure exists, and you should probably be more safe than sorry.

Obfuscate emails by creating images in PHP.

The very first(and still in use) alternative to obfuscating – is to create an image of the address. Now this method can be tedious when you have a lot of emails to cover.

So why not create the images on-the-fly using built in PHP functions? Well. It work’s like it should. But you can’t click on them since, well, it’s just an image. And, yeah, you can’t put mailto: links around the images since the spiders and scrapers will find them.

function obfuscate_email($email) {
  $height = 25;
  $width = 250;

  // Create the image
  $im = imagecreatetruecolor($width, $height);

  // Create some colors
  $white = imagecolorallocate($im, 255, 255, 255);
  $orange = imagecolorallocate($im, 255, 147, 33);
  imagefilledrectangle($im, 0, 0, ($width - 1), ($height - 1), $orange);

  // Write the string
  imagestring($im, 5, 6, 4, $email, $white);

  // Output buffer
  $img_data = ob_get_clean();

  // Destroy image. We already stored it in a variable

  // Return the data inline in an img tag
  return "<img src='data:;base64," . base64_encode($img_data) . "' />";

Cloudflare to the rescue with automatic obfuscation.

A more sophisticated approach is to use Cloudflare. They provide with email obfuscation with a flick of a switch. I use it on this website.

It’s so good, you won’t even know how to control that it actually works. Try viewing the source on this page and look for my email address [email protected]. It sure works!