A PHP Fix for "Gmail Isn't Responding"
Every time I emailed the latest post from my gaming blog to my subscribers, the Gmail app on mobile would hang with the message "Gmail isn't responding." Thankfully, I finally found a fix!
My initial assumptions were wrong
I first thought that the problem must have been the images. Were they too big? I chopped and changed both the code and the image types and sizes, but to no avail.
It was weird. The post would look fine in Gmail on my computer, and it loaded instantly in Yahoo Mail on my phone. Only the Gmail app had this issue. But why?
An incomplete html file?
Gemini gave a number of possible reasons, each of which I ruled out. One of them made sense - to include complete "DOCTYPE" and "html" tags in the email. I added those in, but it still didn't solve the problem.
The moment of realisation
It wasn't until I sent a test mail from this very blog that a light bulb turned on in my head. My last Nick Builds post rendered instantly in Gmail, despite having the same number of images and approximate word length as the post from the gaming blog. So what was the difference?
When I write posts on that blog, I always link the games I talk about to their respective pages on Steam. Sometimes, as in this case, I included a long list of games, 17 to be exact.
My Mailer class was converting all the text, including links from Markdown into HTML as a single long string. Computers, and seemingly Yahoo Mail, have no problem with this, but Gmail on mobile just can't handle such a long, unbroken string. As Gemini explained,
It really was the "Infinite Line" problem. It’s a classic edge case: desktop computers are powerful enough to chew through a 5,000-character line of code instantly, but the mobile renderer (specifically the Android TextView component used by Gmail) tries to calculate the word-wrapping for that entire line in one go, runs out of memory, and hangs.
The solution
To fix this, I simply had to force line breaks into the string, as follows:
// Prevents Gmail Mobile from freezing on long, unbroken HTML strings. $content = str_replace( ['</p>', '</li>', '</ul>', '</h1>', '</h2>', '</h3>', '</div>', '<br>'], ["</p>\n", "</li>\n", "</ul>\n", "</h1>\n", "</h2>\n", "</h3>\n", "</div>\n", "<br>\n"], $content );
And with that, my subscribers can enjoy my gaming blog posts in full on their phones without having to wait 60 seconds for Gmail to figure it all out.