2 min read

Using Signwriter to Obscure Email Addresses

Using Signwriter to Obscure Email Addresses

Signwriter is an excellent Drupal module that "allows you to use TrueType fonts to replace text in headings, blocks, menus and filtered text. It does this by replacing text with an image generated from a TrueType font file which you provide." One of the neat things you can use this module for is to obscure email addresses by displaying them as an image. However, it does require some special tweaks to accomplish this and make it worthwhile. First, install Signwriter, and also a font file such as arial.ttf. Signwriter says on its settings page that it will look in several places for font files. I placed mine in the theme directory. To use it for this purpose, you need to first create a signwriter profile in your code:

// Set up signwriter profile
$profile->fontfile = 'arial';
$profile->fontsize = 10;
$profile->imagetype = 'png';
$profile->disable_span = true;

You can create a profile with just the fontfile (it will use default settings for any you do not explicitly set), but I have also found if you do not set imagetype, it will create your cached image file without an extension. That last setting is undocumented and very important. Without it, the signwriter theme function will prefix your data with a that contains the text you are converting to an image, in this case, an email address. If you leave it alone, there's no point to doing any of this, because bots will still see the email! So set disable_span=true to remove the tag. Next, call signwriter_theme_text() and pass it the string you want to have displayed as an image, and the signwriter profile:

$email = signwriter_theme_text($string_to_obscure, $profile);

$email now contains the HTML that signwriter produced to display the image. Signwriter turned your text into an image and returned the HTML to display the image -- the tag. But we're not done. Signwriter is coded to set the alt attribute of the image to the text you just encoded. Like the above, if we don't do anything about this, there's no point, bots will still see it. We'll use a regular expression to replace the email with the string "Email Address". You can then print or return it (if you place this code into a function). Updated. See comments below.

// Change the alt tag because it contains the address
$email = preg_replace('/alt="[0-9a-zA-Z!@#$%^&*~-.+_/=?|
'alt="Email Address"',$email);

And that's it! BTW, there are a myriad of different regexes you can find out there for email addresses. But I'm not interested in any sort of validation here -- I just want to replace an email address if it appears. I've probably included more characters than I really had to for our purposes, but according to the RFC these can all appear.

Related Posts

9 min read

Create "Simple" Drupal Modules and Fill the Functionality Gap

How many times as developers have we been working on a project, and ran across an issue that seriously affected the way we were interacting with the website? Maybe it was something related simply to...
3 min read

Five Drupal 8 Modules You Aren't Using

Drupal 8 brings along with it a whole slew of quality contributed modules. The alphas are slowing becoming betas and release candidates; moving into production-capable tools that site builders and...
2 min read

The Growth and Future of Front-end Development

There was a time, not too long ago, when the front-end designer was considered a commodity. There wasn't a lot of complexity to the job; it was more like drafting on a piece of paper. There simply...
2 min read

How to use Drush to Maintain Modules

We've seen how Drush Site Aliases can save any Drupal developer time in multiple environments. Now, we'll discuss how Drush can also be used to maintain a site's readiness. Part of the way a Drupal...