How to Send Mass Email

Email Blasts: How to Send Mass Email Messages with NodeJS

So you did an excellent job with your website’s subscription or contact forms, collecting 100k email addresses from your visitors.

Now it’s time to get in touch with them. Maybe you want to do a marketing campaign for one of your products. Maybe you want to invite them to a webinar. Sending a single email to a large distribution list simultaneously is called an email blast.

But how do you send mass email messages to so many addresses?

Back in the old days, placing the emails in the BCC field was how to send a mass email and hide recipients.

DON’T DO THAT!

It’s unprofessional and you can have a limited number of addresses in BCC (for example GMail allows up to 2000 addresses per message).

The best reliable method of sending out email blasts to a large list of addresses is by using an email sending service.

Which Bulk Email Service Is The Best?

I know there are all sorts of lists out there with free email marketing services, but are they good? And most important, are they free?

Bottom line, you can’t have both free and good if you are serious about email marketing and if you want to send large amounts of emails in one go.

Most of the services advertised as free email marketing software is actually limited by the amount of emails you can send per day. If you go over that, then you have to pay. Also, you need to think about email deliverability. It’s useless if you send out emails only to have them all land in the spam folder.

I’ve looked at a few of the main email sending service providers to compare their prices.

Provider50,000 emails/mo500,000 emails/mo
SendGrid$14.95$249
Elastic Email$4.50$45
Amazon SES$5$50
Mailjet$35$425

In my view Amazon SES has the best prices. It has a huge infrastructure which means 100% uptime and they offer features for very high deliverability.

How to Use Amazon SES to Send Email

Amazon SES is a high quality email sending service and the offered documentation for sending emails gives you all the details you need.

So, you want me wondering what is my article about then? Well, I’ll give you the bare minimal boilerplate NodeJS code for sending out emails with Amazon SES.

I will assume that you’ve went through the steps of signing up for an AWS account, verify your “from” email address with Amazon SES or your “from” domain and signup for Amazon SES access for your AWS account.

If you’re having troubles with any of the steps above, just drop me a question in the comments below and I will try to help.

Once all that is setup you will be able to get an AWS access key which we’ll use in our code.

Let’s start coding!

We’ll create a NodeJS project, so I will also assume that you have that installed on your computer. Open a terminal to make a new folder and initialize the NodeJS project.

mkdir send-emails-aws
cd send-emails-aws
npm init
npm install node-ses -save

Our project is now setup. Now let’s actually send an email with Amazon SES service.

Create a file and name it index.js with the following content:

const AMAZON_KEY = 'YOUR AMAZON ACCESS KEY';
const AMAZON_SECRET = 'YOUR AMAZON SECRET ACCESS KEY';
const FROM_ADDRESS = '[email protected]';

var ses = require('node-ses')
, client = ses.createClient({key:AMAZON_KEY, secret: AMAZON_SECRET});

// Give SES the details and let it construct the message for you.
client.sendEmail({
to: '[email protected]'
, from: FROM_ADDRESS
, subject: 'Email subject line'
, message: 'your <b>HTML message</b> goes here'
}, function (err, data, res) {
if (!err){
console.log("EMAIL SENT!");
} else {
console.error(err);
}
});

Replace the values for the access keys, from address and to address with your own. Run the file using node ./index.js and if you get the message “EMAIL SENT!” then it means that your email is away.

Congratulations, now you know how to use amazon SES to send an email message.

But, how about sending 100k emails?

Well, you guessed it, you have to do the same 100k times. I know it may sound like overkill, but really it’s the best way to ensure all your emails make it to the intended recipient.

How to Send Mass Emails with Amazon SES

Basically you’ll need a file to hold the list of recipient email addresses and put the sendEmail call inside a for loop.

But, before you do that you need to make some preparations. If you have a new Amazon SES account then you’ll be placed in a sandbox. That means that you will have 2 limitations:

  • a maximum daily sends (200 emails per day)
  • a maximum sending rate (1 email per second)

Fortunately you can pretty easily upgrade that by sending a request to lift sandbox limitations and get into production mode. You’ll have to indicate how you collected the email addresses and your requirements for daily sends & sending rate.

For me a daily send limit of 200k and 50 emails/second is more than enough, but you can go higher than that. Also, the limitations are per Amazon data center regions. That’s the big advantage with Amazon SES – they have this huge infrastructure, so limits will not be a problem.

Create a text file in your project folder and name it emails.txt. This file should contain your list of recipient emails, one email address per line.

You can get this file by exporting your users from a database. I usually create MySQL materialized views with lists of user email addresses from my WordPress and non-WordPress websites. Then it’s very easy to export the view to a text file.

With a few modifications, our index.js file should look like:

const AMAZON_KEY =  'YOUR AMAZON ACCESS KEY';
const AMAZON_SECRET = 'YOUR AMAZON SECRET ACCESS KEY';
const FROM_ADDRESS = '[email protected]';

var ses = require('node-ses')
  , client = ses.createClient({key:AMAZON_KEY, secret: AMAZON_SECRET});
const readline = require('readline');
const fs = require('fs');


function send(toEmail){
  // Give SES the details and let it construct the message for you.
  client.sendEmail({
     to: toEmail
   , from: FROM_ADDRESS
   , subject: 'Email subject line'
   , message: 'your HTML message goes here'
  }, function (err, data, res) {
    if (!err){
      console.log("EMAIL SENT!");
    } else {
      console.error(err);
    }
  });
}

const readInterface = readline.createInterface({
    input: fs.createReadStream('./emails.txt'),
    console: false
});

readInterface.on('line', function(emailAddress) {
   send(emailAddress);
});

Let’s look at the code.

First, the sendEmail call is now part of the function send() that takes in the recipient’s email address as a parameter.

We’re reading the list of email addresses using the package readline that will take in our emails.txt file and issue a line event for each line. We catch that event and call the send() function for each email address we get.

In this way we’ve basically setup everything we need to send out as many emails as we want using Amazon SES.

 

John Negoita

View posts by John Negoita
I'm a Java programmer, been into programming since 1999 and having tons of fun with it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top