a comment reply by email solution for a self-hosted WordPress blog

For my self-hosted WordPress blog I don’t delegate comment management to platforms like Disqus or wordpress.com. I have nothing in principle against them; on the contrary, I acknowledge that they offer an useful and convenient service. Simply, I belong to (I presume) that minority of people that nowadays prefer a their own solution than a third-party one.

So said, I would have liked very much to enjoy the reply to comment by mail feature that is available for wordpress.com users. It is very very cushy (and in some occasions it is the only way) to continue a discussion within the mail client without the need to open the blog site.

Unfortunately it seems that the same feature isn’t available for a self-hosted WordPress blog, neither as a core function nor as a plugin extension. In theory such a feature is not impossible. I see two main ways to obtain it:

  • make WordPress checks periodically a mailbox converting email messages to blog comments
  • make WordPress site acts as a mailserver converting incoming email messages to blog comments

The first option requires a job scheduler (say: cron) to run frequently (say: every minute) and check a mailbox. My poor hosting plan does not provide me such a tool. In fact one could think that it is not mandatory to have a scheduler, and that it suffices to check the mailbox every time a new visitor lands to the blog. However I want to inform the commenter that his reply has been posted, and it is unacceptable to wait, for example, one hour if the first blog visitor arrives that time after the reply comment by email. Anyway, the needing to run a trigger very frequently hurts me a bit.

The second option requires a mailserver integrated into the web site. This solution seems to have no drawback: each comment reply by email is processed and converted as soon as it is received. But, simply speaking, I am unable to say if and how it is possible. For example my hosting provider offers me a mail server and a web server, but they cannot interact.

So I have looked for an alternative solution. My earliest successful attempt was simple to program but had a quite convoluted plan: it required a IFTTT account, a Gmail account and a GDrive account. Its weakness is that it depended on too many services. Searching a simpler way, I discovered and customized an email to http-post gateway, developed for the Google App Engine ecosystem; then, I worked to refine such solution transforming it into a direct email to wordpress gateway. A lot of pain went into it but eventually I am enough satisfied about the final result.

It has two key elements: a little piece of WordPress code which modifies email notifications headers so that any reply can be propertly decoded, and an appspot.com program which receives and processes it as a comment to the right blog post, and then notify the reply author by email about the outcome of such automated posting.

Here below I describe the steps needed to apply such solution for your self-hosted WordPress blog to enable comment reply by email.

  1. Log in into your appspot account or register yourself if you haven’t one yet. Choose a name for your application to run, which defines the address where email comment replies will be sent. Download the archive containing the program files, customize app.yaml file as shown below and upload them to Google App Engine. If you don’t know how to do, you can follow the excellent Amit Agarwal guide. It is written for a different program but the procedure is identical.

    application: your_chosen_appspot_name
    version: 1
    runtime: python27
    api_version: 1
    threadsafe: true
    - url: /_ah/mail/.+
      script: main.app
    - mail
    # The url of your WordPress xmlrpc.php file
      XMLRPC_URL: 'http://www.mysite.invalid/wp/xmlrpc.php'
    # The credentials for your WordPress blog
      USERNAME: 'myuser'
      PASSWORD: 'mypassword'
    # Set the following parameter to true if you want to use 
    # the credentials of a limited WordPress account (recommended)
      SAFE: true
    # Details about the reply to comment author 
    # to specify if safe is set to true
      AUTHOR: 'Your name'
      URL: 'www.yoursite.invalid'
      EMAIL: 'your.mailbox@yoursite.invalid'
    # Details about the outcome notification email to be sent  
      COPY_EMAIL_FROM: 'the.mailbox.you.used.to.register.the@appspot.account'
      COPY_EMAIL_TO: 'your.mailbox.to.notify@yoursite.invalid'
      COPY_EMAIL_SUBJECT: 'notification email subject'

    My Python knowledge is practically zero; if my program works is only because I just have been lucky to find some different well-written and ready-to-use pieces of code and to fit them together with some intuition, so I need to thank some persons:

  2. Add the following code to the functions.php theme file.

    function _6109NotificationText($textMsg,$coID) {
      global $_6109headerID;
      $textMsg  = "------------ Reply above this line and mark you reply close by !END ------------" . "\r\n" . $textMsg;
      return $textMsg;
    function _6109NotificationHeaders($headers) {
      global $_6109headerID;
      $pid = get_the_ID();
      $ppid = get_post($pid);
      // put your chosen address in the line below according to the appspot name you have chosen
      $rto = "whatever.name@your_chosen_appspot_name.appspotmail.com";
      $rto = str_replace("@","+" . str_replace("=","",base64_encode($ppid->post_author . ":" . $pid . ":" . $_6109headerID) . "@"),$rto);
      // change the notification from: address 
      $headers = preg_replace("/From: .*\n/","\n",$headers);
      $headers .= "From: \"WP comment manager\" <" . $rto . ">\n";
      // change the notification reply-to: address 
      $headers = preg_replace("/Reply-To: .*\n/","\n",$headers);
      $headers .= "Reply-To: \"WP comment manager\" <" . $rto . ">\n";
      return $headers;
    add_filter("comment_notification_text", "_6109NotificationText",1,2);
    add_filter("comment_notification_headers", "_6109NotificationHeaders");

    Note that the code is commented. The first comment precedes a code line to modify: it sets the address where comment reply email ares to be sent; you can also indicate your usual address and then set it so that all replies are forwarded to your chosen appspot subdomain. The other comments indicate those code lines that change the sender and reply-to address; I have preferred to set both them even if someone could change only the latter.

When done with the Google App Engine and WordPress set up, you are ready to test the operation. Insert a post comment as a guest user and check the notification email: does it contain the expected reply-to address? If not, review the WordPress code. Then reply to the notification email, inserting the string !END at the bottom of the text as a closing delimiter, and wait for few seconds: does the comment post notification email come? does it report an OK message or an error message? In the case of unable to post comment message, try disabling temporarily your antispam plugin to check if the error disappears. It happened to me that Akismet prevents the comment releasing. To solve such barrier I needed to uncheck its automatical discard as suggested elsewhere, and let it learn that my comments are not spam marking the first ones manually as legitimate comments.

Now some words on further development and privacy issue of my solution. As I was saying in the beginning, when it is possible I prefer to manage my blog data by myself than to delegate such a task to others. Until now I have thought that this is an ethic responsibility based on the belief that my data is more protected in my hands than in other hands. This is even more true for blog commenter data. I don’t require registration. I am not interested in commenter identities. On my blog comments signed with a nickname and/or a fake email are welcome as well as the other ones. I simply take charge of the burden to receive and record the visitor comments. Sure, my blog can be hacked, as well as millions of other WordPress blogs. But I don’t do data mining with them (apart spam which is a great and funny source of data to analyze), and I try to not make commenter data (or I try to not make, at very least) available to the big data brother also known as NSA.

Until now. Now I have discovered that the matter is not so simple. I read that Gravatar would have tracking capabilities (and for this reason I have disabled it) and that Akismet would collect comment data (legitimately, of course). Spam is a so deep plague that I cannot fight against it on my own. So even on my blog commenting depends on thid-party services.

Nevertheless, I am reluctant to trust another service, storing my administrator credentials in Google App Engine. SAFE parameter in app.yaml file solves such dilemma. Setting SAFE: true can use a registered blog user with the limited role of subscriber, even ad hoc created replicating post author identity, to allow only to post comments without giving him any right to access other blog sections (to be perfect, one should also delete the quoted text containing the commenter ip and email address before sending the reply). On the other hand working with administrator credentials would open some very interesting possibilities. For example to delete or mark as spam any comment by email; and, beyond this, to make the reply to comment by email feature available to all users which subscribe to comments.

For the moment, I stop here. Any observation about it will be greatly appreciated.

One thought on “a comment reply by email solution for a self-hosted WordPress blog

  1. Pingback: launch of spam analytics | sei-uno-zero-nove

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.