DEV Community

Cover image for Sending Custom emails with NodeJS from 🪄scratch  and for no cost whatsoever🗡
IroncladDev
IroncladDev

Posted on

Sending Custom emails with NodeJS from 🪄scratch and for no cost whatsoever🗡

In this tutorial, I'm going to show you how you can send custom emails with just NodeJS, two sweet npm packages, and a single email address.

The best part? No email hosting is required, you can do this for no cost at all, and all in under 50 lines of code!

Let's get started!


In this example, I'll be using gmail to get an email. You may do this anywhere else, but I would prefer google due to how easy it is to get an alternate email account.

After signing up for google, go to https://myaccount.google.com/, go to the Security section and scroll to the Signing in to Google section.

Signing into Google

To be able to send emails, you'll need an app password from your google account. You can obtain this after setting up two-step verification. If you are not using google, use the password for that email account and keep it somewhere safe since we'll need it again.

Click on App Passwords

After turning on two-step verification, click on App Passwords and create a new one for Mail. As for the device, click on Other (custom name) and name it anything you want.

Setting up the password

After that, copy the 16-digit app password, save it, and we're ready to start getting into the code.


First, install two packages from npm - nodemailer and nodemailer-juice

const nodemailer = require('nodemailer');
const inLineCss = require('nodemailer-juice');
Enter fullscreen mode Exit fullscreen mode

Why do we need nodemailer-juice? Emails typically only accept inline styles and not any styling from CSS. If you don't want to sweat your brain to pieces, it's a good option to use nodemailer juice - it automatically converts css within <style></style> tags into inline CSS for you.

Now for the emailing function. I've already made it, so all you have to do is call it to send an email.

function sendEmail(to, subject, message) {
  let transporter = nodemailer.createTransport({
    service: 'gmail', //change if not using gmail
    host: 'smtp.gmail.com', // also change if not using gmail
    port: 465,
    secure: true,
    auth: {
      user: "<you@email.com>",
      pass: "<your-app-password>"
    }
  });
  transporter.use('compile', inLineCss()); //makes your email nice and full of css

  let mailDetails = {
    from: "<you@email.com>",
    to: to,
    subject: subject,
    html: message,
  };

  transporter.sendMail(mailDetails, function (err, data) {
    if (err) console.error(err)
  });
}
Enter fullscreen mode Exit fullscreen mode

For the sendEmail function, you will have to pass in an html document for the parameter message. Just to let you know, hover states, css listeners, and literally anything that won't run in inline styles will not run.

Unfortunately, I'm too lazy to write an entire document here for you. I do have a template email at https://replit.com/@IroncladDev/Noseletter in case you wanted to use it.

To make things a bit easier, I would store the html document in a function (as a template string) and have some parameters in which I could pass title, body, and some other options.

const emailTemplate = (title, body) => `<!DOCTYPE html>
<html lang="en">
  <head>
    ...
    <style>
      ...
    </style>
  </head>
  <body>
    <h1>${title}</h1>
    <p>${body}</p>
  </body>
</html>`;
Enter fullscreen mode Exit fullscreen mode

To pass the email template function into the sendEmail function, simply do like so:

sendEmail("someone@email.com", "Email Title", emailTemplate("Email Title", "This is the body of the email"));

That, my friend, is all it takes to send emails in NodeJS.


Liked this post?
Don't forget to subscribe to my newsletter (located on my website footer) for new posts, new projects, recaps, and more!

Join my discord to get in touch and hang out!

Also, be sure to support this post with a couple reactions!

Discussion (0)