Evbogue.com


How to write a markdown email with Nodemailer

By Ev Bogue - August 18th 2015

There are two types of emails, HTML and plain text.

You've probably seen some pretty bad HTML emails if you use an email client that can parse HTML. I've seen some pretty bad HTML emails in Mutt that don't have plain text versions.

The good news is, if you're sending emails using Node.js you can send both.

Up in until recently I've only been sending plain text emails, because I'm a Terminology junkie and spend most of my life in dwm firing up tiled terminals to mosh into my vps.

I wrote a basic Node.js program using nodemailer and graymatter to send emails. But they are only in plain text, and I want to be able to send HTML emails too. This way you can click the links.

So today I was experimenting with sending markdown emails using nodemailer-markdown. The example in the module readme didn't work, so this is the one I used.

var nodemailer = require('nodemailer'),
    markdown   = require('nodemailer-markdown').markdown;

var transporter = nodemailer.createTransport();

transporter.use('compile', markdown());

transporter.sendMail({
    from: 'ev@evbogue.com',
    to: 'ev@evbogue.com',
    subject: 'Markdown email',
    markdown: '# Hello World!\n\nThis is a [markdown](http://evbogue.com/) email.'
})

Make sure you npm install nodemailer nodemailer-markdown for the program to work.

This program renders two different version of an email, html and plain text, and sends them in the email.

The only weird thing is the plain text version is written in markdown, which doesn't seem write to me. But I can't think about how to do it differently, so I suppose that'll have to work. Pretty much anyone using a plain text email reader can probably parse markdown with their brains anyway, right?

Try it out. See if it works. You can email me with a markdown formatted email, if you want.

How to use Node.js to make animated terminal gifs →

← Why run your own distributed email server?

git

This is Decent: ?


about | blog | ssb | decent | ev@evbogue.com | gitmx