How To Take The Headache Out Of CSS And HTML In Emails
Tags: javascript, productivity, css, node
Published: 2020-03-04 15:53:12

I recently went down a rabbit-hole of creating a high-powered email workflow using [Glitch](http://glitch.com?utm_medium=weblink&utm_source=dev.to&utm_campaign=blog&utm_content=dev) to code, compile, and preview our HTML marketing emails.

It was so useful to me to be able to do all of that in one place that I created a guide for how you can do the same.

## Who is this post for?

If you send emails, this post is for you. 
Maybe you send emails as part of your job or as a side-gig or even for fun. You might use Mailchimp, TinyLetter, Customer.io or one of the literally million other services out there.

This post is _definitely_ for you if you have found yourself coding your emails in raw HTML rather than a drag-and-drop builder.

And this is _most definitely_ for you if you like advanced style sheets.

## The problem is email

Or, more accurately, email clients. See, they all have a terribly mismatched set of requirements for rendering email. 

![Clippy](https://dev-to-uploads.s3.amazonaws.com/i/o3bautwxcx0o7sl01afg.jpeg)
_Some Outlook clients (I know) even use old Microsoft Word engines to render their email (I KNOW)._

Some clients don't respect style sheets entirely, while others don't respect certain parts of style sheets. It's a whole thing.

So the best practice in our funny corner of the world is to inline as many styles as possible.

Customizing emails with inline styles is hard work, especially when you have to do it again, and again, and again.

This is so frustrating that lots of new tools have cropped up to do this for you. Take Automattic's [Juice](https://github.com/Automattic/juice) or Litmus' [paid email builder](https://litmus.com/email-builder). All great!

But some aren't free and some only solve a piece of the puzzle.

## The solution is a task runner

A task runner (like [Grunt](https://gruntjs.com/)) combines a number of different chores into one execution step. 

I know very little about JavaScript, but I do know how to follow READMEs and tinker until I break things and get them working again.

So when I came across Grunt and finally understood it, it rocked my little email world. Especially when I found [Lee Munroe's grunt-email-workflow](https://github.com/leemunroe/grunt-email-workflow/). I could use SCSS (fancy CSS) and inline it all at the same time, keeping my templates clean while I coded but outputting everything I needed.

![Grunt logo](https://dev-to-uploads.s3.amazonaws.com/i/4t7jlrbkzr8iceu38ed6.png)

Once I got it up and running (and customized), thanks to the magic of Glitch's Preview functionality, I could see the live previews at the drop of a hat.


## What does this task runner do?

Using some basic modules, I created a Glitch app that does a few main things:
* Takes SCSS/Sass partials and combines them into a unified stylesheet
* Takes that stylesheet and inlines all of the styles into a single HTML file
* Loads that HTML into a preview file that you can copy/paste directly into your email client.

## How do I use it?

1. Go Glitch.com 🎏 and create an account if you haven't already.
2. Head to the project [grunt-for-email](https://glitch.com/edit/#!/grunt-for-email) and hit remix for your own version of the very same. You'll need to update a couple small things like some fixed links in `/public/index.html`.
3. Make it a private project if you want your code to be just for you.
4. Follow the readme to customize your folders and your workflow.
![Exploring the file tree](https://dev-to-uploads.s3.amazonaws.com/i/vl6r30rij8gz4a577ohk.gif)
5. Start running your own tasks.
![Compile using the code in the readme](https://dev-to-uploads.s3.amazonaws.com/i/kyf79cv44i51u2zcvfw1.gif)
6. Hit "Show" > "Show Next to Code" for some sweet, sweet previewing.
![Live preview demonstration](https://dev-to-uploads.s3.amazonaws.com/i/h86whf6u8d3lu3nlkvxj.gif)
7. For extra fun, check out the additional resources in the README.

If you've got questions or feedback, feel free to leave a comment! And if you find this tool useful, please Unicorn 🦄 this post.

_We're launching some exciting new features on Glitch real soon. [Be the first to know](https://glitch.com/coming-soon?utm_medium=weblink&utm_source=dev.to&utm_campaign=blog&utm_content=dev)._