[wp-trac] [WordPress Trac] #52283: Reply to comments via emails
WordPress Trac
noreply at wordpress.org
Tue Jan 12 17:51:18 UTC 2021
#52283: Reply to comments via emails
-----------------------------+-----------------------------
Reporter: imath | Owner: (none)
Type: feature request | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Comments | Version: trunk
Severity: normal | Keywords: has-patch
Focuses: |
-----------------------------+-----------------------------
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 ====
[[Image(https://cldup.com/PdoTXB08-2.png)]]
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
[https://gist.github.com/loziju/66d3f024e102704ff5222e54a4bfd50e 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
[https://www.php.net/manual/en/book.imap 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.
[[Image(https://cldup.com/L5sdLdl4Sr.jpg)]]
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:
`<author_username/post-type/post-ID/comment-type/comment-ID at site.url>`
=== 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:
- [https://github.com/daniele-occhipinti/php-email-parser Plancake Email
Parser] (GPLv3)
- [https://github.com/willdurand/EmailReplyParser EmailReplyParser] (MIT
License)
- [https://github.com/zbateson/mail-mime-parser MailMimeParser] (BSD
2-Clause “Simplified”)
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 !
[[Image(https://cldup.com/w64Mz6V_cv.jpg)]]
=== 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
[https://www.php.net/manual/en/function.imap-fetchbody.php
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.
👋
--
Ticket URL: <https://core.trac.wordpress.org/ticket/52283>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list