Setting up Nodemailer with Postfix

Nodemailer is a package for Node.js applications that allows us to send emails. In this post I will show you how to configure Nodemailer with Postfix. Check out this post if you don't know how to install and configure Postfix on a Ubuntu VM.

Installing Nodemailer

npm install nodemailer

Creating a transporter

First we need to create a transporter for 'transporting' our emails.

import Mailer from 'nodemailer'

let transporter = Mailer.createTransport({
	sendmail: true,
	newline: 'unix',
	path: '/usr/sbin/sendmail',
	secure: true,
	dkim: {
		domainName: 'test.com',
		keySelector: 'default', // The key you used in your DKIM TXT DNS Record
		privateKey: key, // Content of you private key
	}
})

If you did not setup DKIM, you can remove the secure end dkim keys.

Sending mails

transporter.sendMail({
	to: 'email@example.com',
	from: '"Name" <mail@test.com>', // Make sure you don't forget the < > brackets
	subject: 'Testing a mail with Nodemailer',
	text: 'Your message in text', // Optional, but recommended
	html: '<h1>Hi</h1><br /><p>Your message</p>', // Optional
})

That's it! You can now send mails 🥳