Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#52283 new feature request

Reply to comments via emails

Reported by: imath's profile imath Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Comments Keywords: has-patch
Focuses: Cc:


Following up the discussion I had with @AudrasJB about exploring this interesting feature (I understood it was discussed during latest development chat), here are the results of my searches/tests.

WordPress Core’s Post via email feature

First, I remembered WordPress was including this feature: the first thing I did was testing it!

NB: in order to test emails, I’ve set my local Postfix configuration to use Gmail as a relay).

My thoughts about the feature:

  1. I was surprised we need to visit site.url/wp-mail.php to get the mails. I was expected a WP Cron task to be scheduled.
  2. Sending an email with Gmail to the mail server is adding a post with a title but without content.
  3. The POP3() class (in src/wp-includes/class-pop3.php) could enjoy some improvements and possibly use IMAP functions if available into the WordPress server config.

Introducing a Comment via email feature

For the rest of this ticket, please note I chose a specific use case to run my tests: Replying to a post author notification. I believe this notification is a good first candidate for the feature, as post authors are not any given user of the site: the site has their emails and we can control the post author email before inserting the comment reply to avoid the spam risk.

Identifying the incoming email is a reply to a comment

I’ve explored if there was an email header available to transport some information from the post author notification to the reply sent to the Mail server used by the WordPress site. The only one available seems to be the References: one.

The format of this reference must be like this <.*@site.url>. Most of the time, we’ll get this reference and will be able to use it most of the time (75% according to my tests).

So I’ve edited the wp_notify_postauthor() function to include references about the author, post and comment and override the Reply-To: header so that replies are sent to the WordPress mail server (the one we can set into the Settings > Writing > Post via email section). For the references, I tried to use a meaningful format, just like GitHub does:


Extracting the comment reply interesting parts

This is the most difficult part of the feature. Some information are available into the reply email headers but we need to extract the content of the reply out of the email body (mostly multipart) and only it. It will require a lot of tests with different Email Softwares & Online services because there’s not a standardized way of delimiting the reply. I’ve tested with Apple Mail, Gmail (online), Outlook (online), GMX (online).

I’ve looked for already existing parsing script on GitHub and found, to ease the job:

For my tests, I’ve decided to go with Plancake, even if I had to edit some parts of it, it was pretty easy to use. Using it, I was able to easily extract:

  • The author email (From: header)
  • The references about the author, the post, and the parent comment (References: header)
  • The reply date (Date: header)

The only issue was with Apple Mail: the references are broken the site URL part is missing, so I had to fallback on the comment permalink (included into the quoted comment below the reply) and use a Regex to extract the parent comment permalink and validate this parent comment, the post and the author.

For the body content, I was able to easily extract the plain text content of multipart emails or recreate it for text/html only emails (eg: GMX) using the wp_kses() function. But then, I had to use some complementary Regex to only get the reply text.

My first target was the real smtp.sender email that is used by every tested Email services (Gmail, Apple Mail, Outlook, GMX) to inform when and by who the comment was made. As we don’t know what's the smtp.sender for sure (in my case it’s the Email I use to relay localhost to Gmail), I’ve used a Regex to search for <emailformat>. If not found I fallback to the first line of the quoted notification that looks like > some translated text "Post Title". Finally I’m looking for more than two hyphens (usually used over the email signature) or underscores (used by Outlook).

All this is done into the src/wp-mail.php before the code used to post via email for simplicity. So if you plan to apply the attached patch to test, you’ll need to go to this url site.url/wp-mail.php to fetch commment replies made via emails.

It’s working !

My thoughts about the feature:

  • It’s a great way to improve/promote try to bring back more conversations into WordPress comments
  • We should probably use 2 different Mail Server accounts if it is used to Post via email to take no risk a comment content might be wrongly used to create a post and vice versa.
  • According to the above point, an option to activate the feature could be added to the Discussion settings. Into the patch, I’m using a filter.
  • We should use a scheduled WP Cron Job to fetch emails.
  • We could take this opportunity to improve the POP3() class, I’ve tested IMAP functions and it’s easing the way to extract email parts (See imap_fetchbody)
  • We should provide support the feature completely for post author notifications focusing on code to extract the comment content from emails and to insert comments.
  • We should include hooks to make it possible to use services like JetPack, Mailgun, Sendgrid or any other ones for huge site needs.

The attached patch is there to have a first proof of concept. It should be improved. Thoughts, feedbacks & patch improvements are very welcome.


Attachments (1)

52283.patch (22.9 KB) - added by imath 3 years ago.

Download all attachments as: .zip

Change History (7)

3 years ago

This ticket was mentioned in Slack in #core-comments by imath. View the logs.

3 years ago

#2 @dd32
3 years ago

Just noting #22942 - Post by email has been "kind of" broken for a long time, and was just slated to be removed, but that never happened.

However, things have changed greatly since then, and far more email parsers are available today than back then so improving both would be a welcome change IMHO (although I'm unsure if GPLv3 libraries could be distributed within a GPLv2 or later project such as WordPress?).

#3 @imath
3 years ago

Hi @dd32

Thanks a lot for your comment.

Concerning #22942 I'd say: Commenting via Email seems to make more sense than Posting via email, especially since the Block Editor has landed into Core. But some post types might still enjoy it. Example: I can imagine The bbPress reply post type would greatly enjoy this kind of feature :)

About licenses, I must confess it was difficult for me to be sure about GPLv2 compatibility with v3. I understood it was possible if we were using the terms "GPLv2 or later".

This ticket was mentioned in Slack in #core by imath. View the logs.

3 years ago

#5 @johnbillion
3 years ago

  • Version trunk deleted

I'm in favour of this being something available in core, although we'd need to work on making the documentation for its setup very clear.

Have you looked at plugins that provide this feature?

#6 @imath
3 years ago

Hi @johnbillion

Thanks for your feedback 👌. I’m going to explore how plugins are managing this feature, and I’ll update the ticket asap.

Note: See TracTickets for help on using tickets.