As many of you know, I’ve recently launched a new weekly newsletter called Web Tools Weekly. In addition to providing a weekly, categorized list of tools for front-end developers, most issues begin with a brief tip or tutorial that usually includes code examples that have syntax highlighting.
The screenshot below shows an example of embedded highlighted code, from Issue #6 of my newsletter:
Unfortunately, the MailChimp post Joshua referenced seems to be out of date and the video it claims to embed isn’t showing up. So I came up with a simple way to do this on my own, based on Joshua’s suggestion of Pygments themes. Here’s a quick step-by-step showing how I do this.
Visit Pygments.org in Chrome or Safari
I use Google Chrome (more on this in a moment) to visit Pygments.org (a site that lets you add syntax-highlighted code to pretty much anything). I paste the code I want highlighted in the textarea field at the bottom of the page.
After I paste the code into the field, I type a name for the code example (the name doesn’t matter, so I usually just put something random), and select the code language from the drop-down list. This option lists 291 programming languages, so I think it’s safe to say you shouldn’t have a problem highlighting any type of code.
Entering my name is optional. Once I’ve done that, I click the “Highlight!” button and I see the following:
Note that Pygments allows you to actually save your syntax highlighted code snippet. You dont have to do this in this case. This mediary page is simply used for the copy and paste done in the next step, so I don’t actually click the “save” option that you see in the last screenshot.
Copy and Paste Directly from Pygments
Once I’ve got the code highlighted, to get the code into the MailChimp editor, I just copy it right on the page. This is where Chrome and Safari come in handy.
Apparently, Chrome and Safari are the only browsers that will copy the contents of a web page in rich text format. I’m not sure if Firefox, IE or Opera used to do this or not, but I think the way Chrome and Safari do this is the right way so I hope they don’t change that. I’ve tested this on both Mac and Windows, with the same results.
Once the rich-text-formatted code is on the clipboard, I simply paste it directly into the MailChimp email editor:
Notice, however, that the rich-text paste doesn’t include the background color.
Use “Source” Mode in MailChimp Editor
To fix the fact that the background color doesn’t show up, I simply view the email I’m editing in the MailChimp editor in “source” mode. This is done by hitting the “Source” option:
Then I can see all the wonderfully-ugly HTML and CSS that builds the email newsletter. I find the section that has the syntax-highlighted code, and I change the
background-color property on the “pre” element from
The preview pane on the left will display immediately what the syntax-highlighted code will look like:
And that’s basically it. I should point out that because the padding on the syntax highlighted code shows up differently on different email clients, I’ve had to add some extra HTML table code to get a fairly cross-client look in recent issues. For example Outlook on Windows strips out the padding on the
<pre> element. Using padding on a parent
<table> element instead seems to work universally. (I use PhraseExpress on my Windows machine to auto-generate that extra code for each highlighted section of code, so I don’t have to type it each time.)
There are probably other ways to do this. For me, this is a simple method that works well with MailChimp. If you’re creating custom HTML emails outside of MailChimp’s editor, you can probably do this using their CSS Inliner or a similar tool.
If anyone has another suggestion for embedding code with custom syntax highlighting in HTML email, let me know!