Tech Note: The “Email an Article” Makeover, Now Mobile-Friendly

Thanks to MJML
LGF • Views: 40,881

Image via Shutterstock

In the past few days I’ve been working on a part of the LGF site that’s been woefully neglected for too long: our “Email an Article” feature, which you get to by clicking the little envelope icon below each article — the one that looks like this:

Would you believe this code hasn’t been seriously worked on for 10 years?

In a way it’s kind of cool that it was still functional despite the lack of attention, but with the advent of mobile devices it really started to show its age. The idea of the feature is to send a nicely formatted HTML email that contains most of the article, with images and links and all that good stuff. (It also includes an alternate non-HTML version for people who don’t want HTML in their emails.)

But on mobile devices, this HTML code was … well … less than optimal. Not perfect. Oh, let’s face it, it sucked. On some mobile devices it looked OK, but was shrunken down to an unreadable tiny size because it wasn’t scaled to the mobile screen. On other mobile devices it looked flat out awful, because they didn’t support CSS properly, or a myriad of other reasons.

The thing about mobile HTML email is that it’s a real nightmare to code, with so many devices out there, each with its own quirks and bugs. That’s one reason why I hadn’t done much with the code for a long time, because it was a daunting task.

But I’m pleased to announce that I recently discovered an open source framework called MJML that makes all of these compatibility headaches disappear like magic. Instead of writing the HTML directly, you code it with MJML’s custom tags first, then compile it to the special HTML that works in emails. Here’s a sample of what the MJML code looks like:

<mjml>
   <mj-head>
     <mj-title>Hello MJML</mj-title>
   </mj-head>
   <mj-body>
     <mj-container>
       <mj-section>
         <mj-column>
           <mj-text>
             Hello World!
           </mj-text>
         </mj-column>
       </mj-section>
     </mj-container>
   </mj-body>
 </mjml>

The MJML framework gives us a vastly improved “Email an Article” feature that looks great on almost any mobile device or desktop computer. The HTML version now scales beautifully to fit the screen size of whatever device you’re using to view it, and it works even with the most troublesome email clients (I’m looking at you, Microsoft Outlook). And as a bonus, our emails now include links to share the articles to Facebook and Twitter.

I always like to end these Tech Notes with a tip for programmers, so here’s something I figured out while working with MJML that wasn’t obvious and wasn’t in their documentation.

Since I use PHP to generate the email for this feature, I build the MJML code as a string. The official docs for MJML only describe how to compile the code from a file on disk and write the resulting code back to the disk, but this seemed like way too much overhead. So I worked out a way to use PHP to pass a string into the MJML compiler and get back a string of compiled HTML code. And here it is:

<?php
$mjml = '[[ MJML CODE HERE ]]';
$html = trim(
	shell_exec(
		'echo -n ' . escapeshellarg($mjml) . ' | mjml -i -s -m'
	)
);
?>

This uses PHP’s shell_exec command to pipe the MJML string to the MJML command line compiler, and return the compiled code in the variable $html. The trim command is in there just in case there’s some white space at the beginning or end of the compiled code.

Jump to top

Create a PageThis is the LGF Pages posting bookmarklet. To use it, drag this button to your browser's bookmark bar, and title it 'LGF Pages' (or whatever you like). Then browse to a site you want to post, select some text on the page to use for a quote, click the bookmarklet, and the Pages posting window will appear with the title, text, and any embedded video or audio files already filled in, ready to go.
Or... you can just click this button to open the Pages posting window right away.
Last updated: 2016-01-01 10:29 am PST
LGF User's Guide RSS Feeds Tweet

Help support Little Green Footballs!

Subscribe now for ad-free access!Register and sign in to a free LGF account before subscribing, and your ad-free access will be automatically enabled.

Donate with
PayPal
Square Cash Shop at amazon
as an LGF Associate!
Recent PagesClick to refresh
Orkestra Obsolete Play Blue Monday Using 1930s Instruments - BBC Arts More information: bbc.in New Order's Blue Monday was released on 7 March 1983, and its cutting-edge electronic groove changed pop music forever. But what would it have sounded like if it had been made 50 years earlier? In a ...
Thanos
19 hours, 9 minutes ago
Views: 105 • Comments: 0 • Rating: 1
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Tune-Yards - ABC 123 (Official Video) 'ABC 123' by Tune-Yards. New album 'I can feel you creep into my private life' is released January 19th 2018: smarturl.it Director: Dear Mr QuistgaardProducer: Abi Atkinson, LS ProductionsCommissioner: Gabe Spierer, Beggars GroupExec Producer: Saskia Whinney, SomesuchDOP: Deepa KeshvalaEditor: ...
Thanos
19 hours, 13 minutes ago
Views: 84 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Funding Restrictions Have Made Gun Violence Researchers Get Creative A study published Thursday concludes that a subsequent increase in gun exposure led to more accidental firearm deaths than otherwise would have occurred in the months after the school shooting. "It was the spike [in sales] itself that sort ...
Thanos
21 hours, 19 minutes ago
Views: 107 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Recycling Chaos in U.S. As China Bans ‘Foreign Waste’ Like many Portland residents, Satish and Arlene Palshikar are serious recyclers. Their house is coated with recycled bluish-white paint. They recycle their rainwater, compost their food waste and carefully separate the paper and plastic they toss out. But recently, ...
Thanos
21 hours, 33 minutes ago
Views: 173 • Comments: 0 • Rating: 1
Tweets: 2 • Share to Facebook
Shares: 0
Comments: 0
: 0
Trump Jerusalem Move: Tear Gas at Lebanon US Embassy Protest There have been violent clashes near the US embassy in Lebanon, in the latest protest against President Donald Trump's decision to recognise Jerusalem as the capital of Israel. Security forces fired tear gas and water cannon to force back ...
Thanos
21 hours, 37 minutes ago
Views: 129 • Comments: 0 • Rating: 0
Tweets: 3 • Share to Facebook
Shares: 0
Comments: 0
: 0
Trump Says He and Trudeau Disagree on Trade Deficit. U.S. Stats Seem to Be on the PM’s SideHair Furor just can't seem to catch a break these days....Despite Trump's insistence the U.S. has a trade deficit with Canada, statistics from the website of the office of the U.S. Trade Representative -- the office handling NAFTA negotiations -- ...
The Major
1 day, 3 hours ago
Views: 165 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Blondie - Doom or Destiny (Official Video)Blondie performing "Doom or Destiny" from the new album "Pollinator".Out Now: blondie.lnk.to Follow Blondiefacebook.cominstagram.com@BlondieOfficial vevo.ly
Thanos
2 days, 6 hours ago
Views: 248 • Comments: 1 • Rating: 2
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Beth Hart & Joe Bonamassa - Black Coffee (Official Music Video) Official video for Black Coffee from the new Beth Hart & Joe Bonamassa album Black Coffee. Pre-order: smarturl.it BLACK COFFEE is available to pre-order on CD and vinyl. The limited CD box includes 2 coasters, a postcard and an ...
Thanos
2 days, 6 hours ago
Views: 1,125 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Sparks - ‘This Town Ain’t Big Enough for Both of Us’ (Live at WFUV) wfuv.org • Follow @wfuv: ow.ly Sparks (allsparks.com) performs "This Town Ain't Big Enough For Both of Us" live in Studio A. Recorded 10.12.17. Host: Darren DeVivoAudio: Jim O'HaraCameras: Dan Tuozzoli, Kristal Ho, Monica Fafaul, Alexandra Brennan and Liz CarrEditor: ...
Thanos
2 days, 6 hours ago
Views: 176 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Conyers Resigns Amid Sexual Harassment Allegations Rep. John Conyers, D-Mich., has sent a letter to House Speaker Paul Ryan and Minority Leader Nancy Pelosi informing them of his decision to step down from Congress as of Tuesday, amid allegations of sexual misconduct leveled by multiple ...
Thanos
2 days, 8 hours ago
Views: 181 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0