DEV Community

Cover image for NodeMailer vs EmailJS
Scofield Idehen
Scofield Idehen

Posted on • Originally published at blog.learnhub.africa

NodeMailer vs EmailJS

Sending email is a common need for many web applications. As a popular JavaScript framework for building server-side rendered React applications, Next.js provides easy integration with Node.js on the backend.

Two popular Node.js packages for sending email from Next.js apps are Nodemailer and EmailJS. Both have their pros and cons depending on the use case.

Nodemailer is a mature and full-featured email-sending package for Node.js. It has comprehensive email server setup and configuration options, and supports attachments, templates, etc.

EmailJS provides an easier abstraction than Nodemailer and other transports to send email quickly without configuring SMTP.

If you want to get started as a programmer, check out the article, How to Learn Programming in 2023 

Nodemailer

Nodemailer is the original and most popular Node.js package for sending email, with over 13 million weekly downloads. It has been around since 2010 and is considered the "de facto" solution for sending mail with Node. Some key features:

  • Supports SMTP, Sendmail, and other transports for delivering email. SMTP is the most common.
  • Highly configurable options for SMTP connections, authentication, proxies, etc.
  • Supports TLS and SSL for secure connections.
  • Easy SMTP connection pooling.
  • Template engine support for Handlebars, Pug, mjml, etc.
  • Attachments, HTML body, plaintext, etc.
  • Sending bulk email and batching.
  • Well-formatted email builder and full access to message options.
  • Plugins for SES, Mailgun, SparkPost, etc.

The SMTP email sending gives Nodemailer much flexibility for production email setups. Developers have full control over configuring SMTP options optimal for their infrastructure.

Connections are pooled and reused efficiently. The API provides great access to set message parameters when constructing an email.

Overall, Nodemailer is a developer-focused package emphasizing freedom and control. The extensive configuration takes more work upfront but pays off later with greater customizability for a production application.

EmailJS

EmailJS launched in 2012 as a simpler alternative to Nodemailer focused on developer experience. It has over 3 million weekly downloads. The key highlights include:

  • Simple client-side usage from the browser or Node.js backend.
  • Abstracts away SMTP and providers behind a simple API.
  • Templating with Handlebars or Jinja supported out of the box.
  • Attachments and all common email features are supported.
  • Magic link authentication for simple user identification.
  • Built-in spam filtering using AI.
  • Can BYO SMTP or use EmailJS shared SMTP pools?

The client-side JS library makes EmailJS unique. Developers can use the same email-sending API in the browser or backend Node.js code. Email can be sent directly from the client without needing a server endpoint.

EmailJS configures and pools SMTP connections behind the scenes so developers avoid tedious setups. The simple API and templating abstracts away much of the complexity of building emails.

Magic links provide a clever authentication alternative, sending emails with verification links rather than dealing with passwords. Overall, the package focuses on developer productivity and ease of use.

Under the hood, EmailJS uses Nodemailer for SMTP delivery by default. But it adds value with the simpler API, built-in templates, connection handling, and client-side support.

Sending Emails with Nodemailer in Next.js

To use Nodemailer with Next.js, first install the nodemailer package:

npm install nodemailer

Then create a file such as lib/email.js with a function to send email:

import nodemailer from 'nodemailer';

export default async ({ to, subject, html }) => {

  // Create SMTP transport
  const transporter = nodemailer.createTransport({
    host: process.env.MAIL_HOST,
    port: process.env.MAIL_PORT, 
    auth: {
      user: process.env.MAIL_USER,
      pass: process.env.MAIL_PASS
    } 

  });


  // Send email

  return transporter.sendMail({

    from: '"Fred Foo ???" <foo@example.com>',

    to,

    subject,

    html

  });

}
Enter fullscreen mode Exit fullscreen mode

The transporter can be reused to send multiple emails. Optionally create a template function to generate the HTML, etc.

To send an email:

import sendEmail from '../lib/email';

await sendEmail({
  to: 'bar@example.com',
  subject: 'Hello World',
  html: '<p>Hello bar! This is from Next.js</p>' 
})
Enter fullscreen mode Exit fullscreen mode

The email will be sent using the configured SMTP settings.

Sending Email with EmailJS in Next.js

First, install the EmailJS client package:

npm install emailjs-com

Then configure the EmailJS service ID and user ID (can be client-side):

import emailjs from 'emailjs-com';
emailjs.init(process.env.EMAIL_JS_SERVICE_ID);</code></pre>
Define an email template:
<pre><code class="language-javascript">const template = {
  to: 'bar@example.com',
  templateId: 'template_abc123',
  variables: {
    name: 'John'
  }
}
Enter fullscreen mode Exit fullscreen mode

And send the email:

emailjs.send(
  process.env.EMAIL_JS_USER_ID, 
  template.templateId,
  template.variables
)
.then(res =&gt; {
  console.log('Email sent!');
})
Enter fullscreen mode Exit fullscreen mode

EmailJS will handle rendering the template, sending via SMTP, etc. Much simpler than directly using Nodemailer!

The same API works in the browser, allowing sending email directly from client-side code.

Tech and Drugs: The Dark Side of Tech As more people rely on drugs to become programmers, find out how deep drug has eaten deep into the tech ecosystem

Comparing the Options

  • Simplicity

EmailJS provides a simpler developer experience needing only a few lines of code to start sending email in Node.js and client-side. Whereas Nodemailer requires more boilerplate and setup.

  • Flexibility

Nodemailer offers more customization over SMTP connections and email construction. For more complex email needs, Nodemailer has great flexibility.

  • Templating

EmailJS builds in easy templating support. Nodemailer templating requires integrating a separate package.

  • Client-side Sending

A big differentiator is EmailJS allows sending email directly from browser JavaScript using the same API as on the server. Nodemailer only works within Node.js.

  • Spam Prevention

EmailJS provides built-in spam filtering using AI to protect sending reputation. Nodemailer leaves spam prevention up to the developer.

  • Cost

Nodemailer is open source and free. EmailJS offers a free tier and paid plans for higher usage starting at $7/month.

  • TypeScript Support

Nodemailer includes TypeScript definitions out of the box. EmailJS relies on community TypeScript definitions.

Conclusion

Nodemailer and EmailJS both enable sending email from Next.js, with different tradeoffs. Nodemailer is the more mature and configurable option. EmailJS simplifies sending on the client and server.

For getting started quickly, EmailJS removes much boilerplate. Nodemailer offers more customization for complex email scenarios. Considering the use case and priorities around simplicity vs flexibility helps determine the best option.

Both integrate nicely with Next.js and can handle sending emails from API routes, getServerSideProps, or the client side. Sending email is essential for many applications, and Next.js developers are fortunate to have excellent choices like Nodemailer and EmailJS!

Resource

Top comments (2)

Collapse
 
xr0master profile image
Sergey Khomushin

Please note, that the emailjs-com is deprecated, you may use @emailjs/browser or @emailjs/nodejs.
Thank you for your article.

Collapse
 
scofieldidehen profile image
Scofield Idehen

Ok, would correct.