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

Thanks to MJML
LGF • Views: 40,412

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
Editorial: Kobach Is Desperate for Trump’s Approval Kris Kobach has been running a Trump-style gubernatorial campaign since the day he announced. From his empty insistence that there’s a “culture of corruption” in Topeka to his argument that the Legislature’s decision to repeal Gov. Sam Brownback’s tax ...
Thanos
6 hours, 20 minutes ago
Views: 85 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
First Aid Kit - It’s a Shame"It's a Shame" by First Aid Kit is available now: smarturl.it Production Company: Slutet är Nära / slutetarnara.se Director: Mats UddProducer: Petra KrigströmExecutive Producer: Alexandros Bakos Lyrics: genius.com Follow First Aid Kit: firstaidkitband.com facebook.com @firstaidkitband instagram.com
Thanos
1 day, 5 hours ago
Views: 170 • Comments: 1 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Jason Isbell and the 400 Unit - American Girl10/13/2017 Encore on night 4 of Ryman residency
Thanos
1 day, 5 hours ago
Views: 182 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
WALK the MOON - Headphones Get WALK THE MOON's “One Foot” and "Headphones" Instantly When You Pre-Order The New Album 'What If Nothing':iTunes - wtmband.comAmazon - wtmband.comSpotify Pre-Save - wtmband.comGoogle Play - wtmband.comMerch Store - wtmband.com Stream "Headphones" Now:Apple Music - wtmband.comSpotify - wtmband.com ...
Thanos
1 day, 5 hours ago
Views: 150 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Nightmare at PDX In all, Cristina was held for 48 hours with the jail population, though she had committed no crime. She was treated poorly, and denied medical treatment when she requested it. She had no money in her jail phone account ...
Thanos
4 days, 17 hours ago
Views: 725 • Comments: 0 • Rating: 3
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
First-Ever Sex Discrimination Legal Network Is Ready to Defend Women in Trump’s America Shortly after the election, the attorneys and advocates who work at the National Women’s Law Center (NWLC) started having a conversation. It seemed that the incoming Trump administration was likely to move backward on enforcing civil rights, loosening regulations, ...
Birth Control Works
5 days, 15 hours ago
Views: 750 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Meet Rhiannon Giddens, Newly Minted MacArthur ‘Genius’ As a solo artist — and, before that, as a member of the Carolina Chocolate Drops — singer-songwriter Rhiannon Giddens has made a career out of revitalizing and invigorating decades' worth of music reflecting the African-American experience. That's meant ...
Thanos
1 week, 1 day ago
Views: 1,177 • Comments: 0 • Rating: 2
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Lies About Black America Are ‘Central’ to Gun Ownership: A Reporter on What We Get Wrong About Gun Violence Lies that the GOP and Donald Trump promote all day long. “If you look at the neighborhoods where [gun violence] is most clustered,” Beckett said, it’s located in areas with high levels of poverty, low levels of education, and ...
Unshaken Defiance
1 week, 2 days ago
Views: 1,774 • Comments: 0 • Rating: 1
Tweets: 63 • Share to Facebook
Shares: 2
Comments: 0
: 2
St. Vincent Performs ‘Los Ageless’ One of Stephen's very favorite singer-songwriters collaborates with three pairs of boots to perform a song off her new album "Masseduction". Subscribe To "The Late Show" Channel HERE: bit.lyFor more content from "The Late Show with Stephen Colbert", click ...
Thanos
1 week, 2 days ago
Views: 1,415 • Comments: 0 • Rating: 1
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Star Wars: The Last Jedi Trailer (Official) Watch the new trailer for Star Wars: The Last Jedi and see it in theaters December 15. Get your tickets now: fandango.com Visit Star Wars at starwars.comSubscribe to Star Wars on YouTube at youtube.comLike Star Wars on Facebook at ...
Thanos
1 week, 2 days ago
Views: 1,366 • Comments: 0 • Rating: 0
Tweets: 1 • Share to Facebook
Shares: 0
Comments: 0
: 0