I’m increasingly of the opinion, as you might have guessed from reading past articles on my blog, that if you can encrypt a thing, you must encrypt a thing, especially if it’s sent over the internet.

So, since more crypto use is always a good thing, I wanted to find a way to encrypt email sent from my WordPress blog. Specifically, I wanted to encrypt my “hire me” contact form, which is emailed to me and quite often contains sensitive information. Sometimes clients are quite forthcoming in their initial messages, so I think it’s professional to protect that.

Although the contact form is the primary use case, this code should work for any email (with only one recipient) sent via wordpress’ internal code, providing the address has a valid (non expired encrypting) public key on file. Adding a key is, in this code, a manual process, however it’d be trivial to extend the code to chat to a key server.

So, anyway, you need to find the functions.php for your theme (I wanted to do this quickly, so I didn’t write a plugin), and put in the following code.

You’ll also need to install the gnupg extension for php. If you’re on debian, this should just be a matter of apt-get install php5-gnupg.

This code will try and find a key for the to address and attempt PGP encryption.

It’s not perfect, for example, if encryption fails for whatever reason, the message will be sent in the clear. I did it this way since not everyone’s public key will be on file, but I still wanted the email sent, so this is probably a good thing.

Also, for jetpack contact forms & comments at least, the code will fire the clear message text to Akismet, if you have the plugin installed. The latest version of Akismet will default to sending the message over TLS, so this isn’t the end of the world if you’re worried about passive monitoring.

Anyway, the more encrypted traffic on the net the better. Have fun!

7 thoughts on “Extending wordpress to send PGP emails

  1. Cool. Part of me recoils at the idea of entrusting a WordPress environment to perform encryption such as this, even if private keys are properly protected. On the other hand, if you’re the only recipient, you can afford to make throw-away keys and as you point out, if you’re going to do email you might as well PGP it.

  2. Because we use the GnuPG extension (which is basically a PHP wrapper for the gnupg executable), you need to store the public key in your web server user’s keyring. This isn’t ideal, but it’s what we’ve got :/

    I found the easiest way to store your public key is to SU into your web server user and import directly:

    The default location for the keyring unfortunately is generally publicly readable, so be sure you block access using .htaccess or similar.

Leave a Reply