How do you format code blocks in emails?

For a long time now I have tried to find a way to format code blocks in my emails, and I just can’t come up with anything good. What I wish I could do is just type some backticks around my code block, and have it format itself just like it would if I posted it on Stack Exchange or whatever. But I’m sending emails from Outlook, so that won’t work.

My current solution is to keep a formatted code block that I made in Craft with some placeholder code saved in a draft email. Then, when I want to put a code block in an email, I copy that block out of the draft and paste my code in. That works ok, but it’s clunky, and I can’t put code into a sentence like this. Emails can render html, so it seems like I should be able to rig up somethingt with Alfred or Keyboard Maestro or something that would take code from my clipboard and give it a grey background and some mono space font to distinguish it from the rest of the email text, but I can’t figure it out.

I would love to hear from others who have found a better method for this than I have.

Have you considered switching e-mail clients (e.g. Mailmate), or sending code as a plain text-based file attachment?

For inline distinction, I use the Consolas font and a different colour for the text. I do this at work on Windows & Outlook, but they are never code blocks. Usually configuration file entries; similar, but not the same.

You could explore a TextExpander rich text snippet preformatted to your code block preference, with a fill in field for the code. That might work, but I have not tested it (I don’t use Outlook on the Mac, and the Windows client I imagine isn’t an exact match behaviour-wise).

Markdown Service Tools - BrettTerpstra.com

See if Brett’s tools can convert what you write into RTF that will work with Outlook.

(I too would recommend Mailmate, but I realize that changing your mail app is “a big ask”.)

Thanks to both of you for the good suggestions. Unfortunately, work requires that I use Outlook, so although I have used Mailmate in the past, that’s no longer an option. I will keep tinkering with this, though, and will report back if I come up with any solutions worth sharing.

1 Like

I’m having a lot of success writing my emails in markdown. I render it in (anything you choose) and then copy/paste into my emails. Very happy with this.

Interesting. I had thought about doing this, but never followed through, for some reason. I think what I really need is to figure how to style the text the way I want.

That’s why I was suggesting Brett’s tools. You should be able to write in Markdown and then have it convert to RTF (which ought to work in most apps, I think) just by using macOS’ Services.

Update: I tried messing around with Brett Terpstra’s md services (which are indeed very cool) as well as other methods for generating html for my emails, but the root problem in my case (as several of you hinted) is Outlook. As it turns out, modern Outlook seems to have no way of letting you paste in your own html code (unless, I guess, maybe if you generate the html from a Word document, but life is too short to write emails that way)! I was just about to give up, when I discovered… a markdown plugin for Outlook! You do need to side-load the plugin, but that is not difficult, and once you get it installed, you are just one click away from writing your emails in sweet, sweet markdown, directly in Outlook. It even has a built-in markdown preview window!

Conclusion: if you, like me, have to use Outlook for email, but wish you could write your emails in markdown, well: now you can, code blocks and inline code snippets and all:

1 Like

Oh! That’s actually very useful to know. Thanks!

1 Like

Yes, it’s working great for me so far :+1: