Drupal Module: Share by Mail Failure in Social Media Share Module

This is a rather otaku (nerdy) error you may find when using the Social Media Share module for D8: when trying to share by mail the browser opens the mail client with an incomplete title and body. Except, it only does it for certain articles. Sounds like a mystery; it's not.
share block
A simple block allowing users to share an article by mail.

A little background: one of my clients recently put together a blog. On that blog, we were careful to put together a custom styled share block using the Social Media Share module--we run the D8 version (8.x-1.8). We decided to add Facebook and Mail share links at the bottom of our articles.

As stakeholders are known to do, somebody stumbled across an odd and somewhat rare quirk: the share by mail link, in some cases, would open up the users e-mail client with an incomplete e-mail. Under normal circumstances, clicking on the share by mail link will open the users e-mail client with the article title as the subject of the mail and a link to our site in the body. The error, however, was causing the subject to truncate and was leaving the body completely blank.

In order to visualize this better, here's an example of how an e-mail looks when everything goes right. Take note of the subject and body:

share by mail success
Entirely unrelated note: dark themes for Ubuntu are the best!

This is exactly what you'd expect. The share module is rendering a mailto for the share by mail link that looks something like this:


mailto:?subject=Caring for a Loved One With Alzheimer%u2019s &body=Check out this article http://stage.example.com/blog/caring-loved-one-alzheimers

So, what does it look like when it doesn't work?--title cut short and no body:

share by mail feature failure
The irony! An unexplained error on an article about getting enough sleep... if God has a sense of humor, it's undeniably a dark humor.

I remember seeing this and thinking: "... whaaaaaat!?"

My first instinct was to look at several different articles to see how widespread the issue was. Initially, it seemed that this sleep article was the only one suffering the error. This would point to something being uniquely different in that specific article (node)--a problem with the page, and not necessarily the module. Except, there isn't really anything different about the article in question--it belongs to a content type that has a standardized layout; our blog articles are all essentially the same: the only thing that differs on them is copy. As I slowly began to wonder just how long it would take me to find a cause for this, I desperately browsed through our blog index to try and find other pages affected with the problem. That's when I spotted the culprit. If the game is "one of these things is not like the other", let's see if you spot it, too:

the index page for our blog
Hint: it's in the page title; if you're a grammarian you'll catch it with ease.

This was a "good God!!!" kind of moment for me: the apostrophes in the titles are different. The one on the right is a proper apostrophe; the one on the sleep article is a single quote. Now, I don't spend too much time in PHP these days, but I do spend a good amount of time working in JavaScript/jQuery--where single quotes are used there in abundance. I know PHP uses single quotes as well, so I think to myself: "This module is obviously taking the node title as a variable and passing it to a function that will insert that variable into the mailto link. Chances are better than not  that whoever wrote this module has wrapped the mailto object in single quotes." If that were the case, a single quote in the middle of the title might just close the mailto object prematurely. That would explain why the title truncates in the mail client exactly where the single quote occurs, and why the body doesn't inherit any of the meta-data in the rest of the mailto. What happens if we swap the single quote with an apostrophe?

Sure enough. That was the fix.

Quick e-mail to stakeholders:

Be sure to use apostrophes and not single quotes when creating/editing blog titles. MS Word should be rendering these according to it's auto-correct capabilities, but you never know...

That's not the end of the story, unfortunately. As I went on to learn, double quotes will also break the share by mail feature. In PHP, I understand that escaping (\) single and double quotes is what's required for code syntax to remain intact. Unfortunately, I can't seem to find the function responsible for handling the mailto link anywhere in this module. My guess is that the module is actually leveraging the mail capabilities of Drupal core in order to pull this off. I haven't quite given up yet. It'd be nice to find a work around so that stakeholders are free to use double and single quotes. Part of me thinks, though, that double quotes don't belong in titles in the first place (stylistically, if you need quotes in your title, your writing should be revised). It'd be cool to solve this either way, though.

If I can find the time, I may open an issue with the Social Media Share module.