Videos
Check out our tutorial video series.
Sending Emails in NodeJS with Sendgrid, SES, or Custom SMTP Servers - A guide on different ways to send programmatic emails in NodeJS
Sending emails is a core requirement of many modern businesses. Whether it's invoice receipts, password resets, or support messages many applications send emails programmatically in response to different events. Sending emails these days is easy and doesn't require knowledge of SMTP, POP3, or IMAP! Here are three ways to send email in NodeJS and how to test them.
There are many email-as-a-service APIs. Some of the most popular names include SendGrid, MailJet, MailGun. They each offer different features and pricing models but they all send emails on demand via API request.
Usually setup is required with the service to enable sending from a custom domain. Once this is done emails are typically sent with an SDK and REST API request. For this example we'll demonstrate sending emails with the SendGrid SDK and Node.
You can get the official SendGrid Javascript library from NPM.
Sending an email is easy. Just provide your API key and then describe the message you wish to send. Lastly call and let SendGrid handle the rest.
SendGrid also supports an property for sending HTML formatted emails. It also has a feature-rich templating system with a browser based UI designer. Pricing is reasonable but for more control try the next option: AWS SES.
If you run your application on cloud infrastructure chances are your cloud provider will have an email sending service. Amazon AWS has a Simple Email Service called SES. It's a great option for those who want a bit more control. You can define email domains and routing rules with Terraform so that email becomes a managed part of your infrastructure.
SES requires a bit of setup but once enabled can be easily used with NodeJS. All AWS infrastructure can be enable by hand in the console but a better way to manage cloud resources is with Terraform.
Here is how to set up an SES domain with Terraform. This will allow us to send emails from a specific address.
So, now that SES is set up we can call it from Node. First we need the AWS SDK.
Now let's create a script to send the email.
While transactional mail services and SES are pretty straight forward if you need ultimate control deploying your own SMTP server is the way to go.
You can deploy an SMTP server on Kubernetes with a bit of setup and configuring. I won't go into that here as it is a rather big job. Note SMTP solutions are only suggested if you have some unusual email sending requirements.
Once that's done you can easily interact with your SMTP server using the NPM package .
And here's an example of sending an email with nodemailer.
So we've just seen three different ways to send emails with Node. But how do we test it? We could test our scripts manually and use our own email addresses as te recipient but this process is slow and unreliable. Ideally we want to test email sending automatically as part of an end-to-end or smoke test.
With MailSlurp you can create new email inboxes on the fly, send emails to them using your service, and then verify the results - all from within a test suite.
First you want to sign up for MailSlurp - it's free to start. Then let's install the MailSlurp SDK and the Jest test framework (you can use any framework you like).
Let's write a simple test that:
Using the SendGrid example as a base our test might look something like this:
That's it! Now you can test your email sending functions with real email addresses so you know for sure that your emails are working.
Sending emails is easy these days. There are tons of services available each with different features, pricing and control. For a quickstart try MailSlurp, SendGrid, MailJet or MailGun. For more control try Amazon SES. For even further control try running an SMTP server and use Nodemailer.
With every emailing solution it's important to test that messages are really sent and received. Many email providers apply spam filters so the only way to know for sure that your emails are working is with an end-to-end test using real addresses. Luckily, MailSlurp has you covered. Check it out!
Check out our tutorial video series.
Email and SMS guides for automation and testing.
View github project code for multiple languages.
Latest posts from the MailSlurp team.
Test, build, and automate messaging with a free MailSlurp account.