Dynamic email signatures

It allows me to generate dynamic email signatures, and at the same time avoid duplicating and maintaining data.

Dynamic email signatures

Richard is a Windows Mail user. He’s pointed out for some time how my plain text signature has a corrupting influence on the body of my email messages. While a better email client might fix this for him, his most recent example coincided with a wet Saturday afternoon, so I set out to create an HTML signature – a change was clearly overdue.

I thought my new signature should match the style of this website, and my next thought was “and it should contain dynamic content too, just like the website”. Oops. Scope creep.

My email client is Thunderbird, running on Windows XP, but the following project could be adapted to most email clients with a little ingenuity. It allows me to generate dynamic email signatures, and at the same time avoid duplicating and maintaining data.

Here’s what my new signature looks like.

The first two items below the green dividing line are dynamic, and so is the quotation at the bottom. The entire signature is generated by a PHP routine hosted on my website, where I already store and present this data in different forms. The result is that every email message I compose has a slightly different signature, but I don’t have to manage the source data in an additional location.

Here’s how I did it.

Thunderbird signatures are text files which may contain plain text or HTML. If it were possible to load the signature files from a URL, inclusion of dynamic content would be simple. Sadly the Bird only supports the local file system.

A little research took me to a Thunderbird add-on called Signature Switch which allows the inclusion of “fortune cookie” content. I could have used this to create the dynamic content I wanted, but it would have meant duplicating data that’s already in tables on my website’s database.

I could have avoided duplication by writing a Windows utility to refresh the signature file periodically, perhaps in response to system notification of the file being accessed, but I didn’t have any development environment on my laptop.

Yes, it does seem unlikely. But I’ve mislaid my Delphi install disc – phone me if you know where it is – and deliberately restrict Eclipse and SharpDevelop to my desktop because they’re such resource hogs. I do most my web development, incidentally, in the delightful PSPad.

But Signature Switch provides a better answer anyway. On its Advanced tab it allows an executable file to be run before each signature file is loaded. An executable file? Enter the wonderful wget.exe.

wget.exe is a cross-platform utility that does one task exceptionally well; it retrieves web content and writes it to the local hard drive. It accepts command line parameters, or more complex tasks can be written to a file with multiple entries.

I chose to drive wget.exe with a batch file because it made testing easier and meant I could have the various parts of the puzzle installed in different locations on different computers.

And here’s what happens.

When I click the Compose button in Thunderbird, Signature Switch tells wget.exe to download the specified PHP file from my website. This PHP file generates a new HTML signature including dynamically-created content taken from database tables, and wget.exe takes the resulting stream and overwrites my signature file. Easy peasy.

After I’d verifed that everything worked as it should, my final step was to uncheck the Wait for executable to finish option in Signature Switch. Now when I click Compose in Thunderbird, the new message opens immediately using the previously-loaded signature file, while wget.exe begins downloading the next signature in the background. It’s quicker.

If I’m ever off-line I still have the previously-downloaded signature file – wget.exe won’t overwrite it if it can’t access the replacement. One major risk tidily mitigated.

It would be possible to include more dynamic content in my email signature. I could use cURL and RSS feeds to insert Wellington weather; I could generate different signatures for different contexts; and I could include links to randomly-chosen articles like this one.

I suspect there may be a few refinements ahead.