How To Use Nodemailer To Send Bulk Emails

Susanne Lundkvist
3 min readSep 20, 2019

--

If you want to send your emails to millions of receivers, here are some tips when using Nodemailer.

Tech. Sgt. Michelle Smith displays her many letters (free pic, 205).
Tech. Sgt. Michelle Smith displays her many letters (from 2005, free to reuse).

Use a dedicated delivery provider

Do not use services that offer SMTP as a sideline or for free to send bulk emails (e.g. Gmail or your hosting company) as you will hit maximum limits quickly. Free email is only free to a certain amount.

Instead use a dedicated delivery provider like one of these examples. Using a dedicated provider means your emails will be delivered in just a second or two, it allows the recipients to unsubscribe, to use built-in templates and to be able to complete the emailing quickly. Make sure the service meets current and future needs so that you will be able to use the same service if your needs grow without having to transfer over to another service. Most of them have free trials, so just tell the client it is free for them to decide if it is for them or not, and no harm done.

Use a dedicated queue manager

You will also need a dedicated queue manager. There are different queue categories and a typical bulk email scenario involves a significant number of messages in each of these queue categories:

  • Submission- A submission queue contains messages that are waiting in the pipeline to be processed by transport agents.
  • Unreachable- A message is routed to the unreachable queue only when it could not be delivered to the destination mailbox.
  • Poison- After a message has undergone the maximum number of delivery attempts, it’s deemed to be dangerous for the server and classified as poison.

Nodemailer creates a callback function with related scopes for every message so it might be hard on memory if you pile up the data for 10 million messages at once. Better to take the data from a queue when there’s a free spot in the connection pool (previously sent message returns its callback).

A popular queue manager is RabbitMQ but there are many more to choose from as you can see in this overview.

Use pooled SMTP

Set pool option to true not to have to create a new connection and doing the SMTP handshake for every single email. Pooled connections brings this time and effort to a minimum.

Set maxMessages to Infinity

Dedicated SMTP providers accept your emails as long as you are paying for them. The default value is 100, which means that when a connection is used to send 100 messages it is removed from the pool and a new connection is created. Therefore set maxMessages option to Infinity for the pooled SMTP transport.

Setting of maxConnections

Your setting for maxConnections is dependent on what your system can manage. It is possible that there are limits on the receiver side so you should not set it to Infinity. A larger number means more messages are sent in parallel.

Use file paths for attachments

You should not use URLs for email attachments since reading the same file from the disk a million times, the file contents probably get cached somewhere between your app and the physical hard disk. If using a URL every new message makes a fresh HTTP fetch to receive the file from the server.

That’s why you should use file paths instead of URLs.

SMTP or HTTP?

If the SMTP service accepts HTTP API as well you still might prefer using SMTP and not the HTTP API as HTTP introduces additional overhead. You probably want to use HTTP over SMTP if the HTTP API is bulk aware — you send a message template and the list of millions of recipients and the service compiles this information into emails itself.

Susanne Lundkvist

--

--

Susanne Lundkvist

Product and Programme Manager, coding in Ruby on Rails, JavaScript/React and Python. Improving product and project management with data analytics.