Test Emails in Your Node/Express Apps using Mailtrap.io
Tags: javascript, node, intermediate, testing
Published: 2018-04-16 01:53:44

---
title: Test Emails in Your Node/Express Apps using Mailtrap.io
published: true
description: A fake SMTP server to test your emails during development
tags: javascript, node, intermediate, testing
---


One feature usually present in almost all software applications is an emailing feature, as it's one of the surest ways to get in touch and communicate with your users. There are many email delivery services that make the development of such a feature a breeze, such as [Sendgrid](https://sendgrid.com/), [Postmark](https://postmarkapp.com/), [Mailgun](https://www.mailgun.com/) and [Sparkpost](https://www.sparkpost.com/). My favourite is Sendgrid. :)

Anyhow, the most common problem when developing an email feature in the pre-production stage is spamming, since you will definitely be using real email addresses when using any of the email delivery services listed above. Hence its neccessary to do sandbox testing. 

Enter [Mailtrap](https://mailtrap.io/). Mailtrap is a fake SMTP server developed by [railsware](https://railsware.com/) and its very useful for pre-production testing. Ideally, you should do email testing with a service like mailtrap and switch to sendgrid and the likes when rolling to production.

In this short example, I will show you how i do sandbox email testing in Node. If you want to follow along, kindly download or clone the [project](https://github.com/KwabenBerko/mailtrap-tutorial) from github. Open up the project in your terminal, and install the required npm packages using `npm install`. Once installed, `cd` into the start folder and open the `app.js` file in your favorite editor. If you haven't done so already, head on over to the [mailtrap](https://mailtrap.io/) website and create a free account. Once done navigate to the [inbox](https://mailtrap.io/inboxes) and you should see both your SMTP and POP3 credentials.

![Mailtrap inbox](https://i.imgur.com/rxiLkdI.png)

We are going to use the [nodemailer](https://www.npmjs.com/package/nodemailer) module to send our email. Lets create a nodemailer SMTP transporter and configure it for use.

```javascript
  //Start Here
  const transporter = nodemailer.createTransport({
    host: "smtp.mailtrap.io",
    port: 2525,
    auth: {
        user: "MAILTRAP_SMTP_USERNAME",
        pass: "MAILTRAP_SMTP_PASSWORD"
    }
  });

```

Now, let's setup our message options:

```javascript
  const mailOptions= {
    from: '"Test Server" <test@example.com>',
    to: req.body.email,
    subject: "Email Test",
    text: "This is an email test using Mailtrap.io"
  };
```

Then we send our message using the `sendMail()` method of our transporter object:

```javascript


  transporter.sendMail(mailOptions, (err, info) => {
    if(err){
        console.log(err);
        return next(err);
    }
    console.log("Info: ", info);
    res.json({
      message: "Email successfully sent."
    });
  });

```
Let us test to see if this works. Fire up the server by typing `node app.js`. Now lets send a POST request to `http://localhost:8001/email`. I am going to use [cURL](https://en.wikipedia.org/wiki/CURL) for this. You can use [Postman](https://www.getpostman.com/) or any REST client of your choice.

```curl

curl -X POST -H "Content-Type:application/json" -d "{"""email""":"""johndoe@gmail.com"""}" http://localhost:8001/email                    

```

Hopefully, you get a response like the one below, which should mean the code worked:

```json

{"message":"Email successfully sent."}

```

Let's check to see if this is the case by checking our mailtrap [inbox](https://mailtrap.io/inboxes).

![Mailtrap inbox](https://i.imgur.com/Y6S6Ixi.png)

![Mailtrap inbox 2](https://i.imgur.com/tZdD3y1.png)

As you can see, Mailtrap allows you to easily test sending emails, as was described above. The Node.js app used in this example makes use of  [Nodemailer](https://nodemailer.com) to send emails, but there are other transport options and email packages out there. If you are looking for Nodemailer alternatives, kindly check out this article: [Sending Emails with Node.js](https://blog.mailtrap.io/send-emails-with-nodejs/). This article goes in depth about some email-oriented packages like [Email.js](https://www.emailjs.com/) and how to use Gmail to send messages from your Node.js app.

Happy Coding!