

If you want to read more about the complexities of using Gmail with nodemailer, go here. We need to do that in order to have credentials for the OAuth security enabled by Gmail. There are a number of ways we can overcome this obstacle (some better than others), and we will choose the one that requires us to set up a project in the Google Cloud Platform. As you may have guessed, Gmail has a high level of security when it comes to mail sent by/to a user’s account. To create a transporter object, we do the following: let transporter = nodemailer.createTransport() ✋ Pay attention, as apart from the user and the pass keys, which are your own credentials for your gmail account, the other three keys need to be retrieved after setting up OAuth.Īs we stated in the beginning of this article, we will be using Gmail for our mail sending needs. Nodemailer’s API is pretty simple and requires us to do the following:
NODEJS SEND EMAIL INSTALL
Install nodemailer using the following command: npm install nodemailer
NODEJS SEND EMAIL HOW TO
You can see that it is working properly by running: node index.js How to Install Nodemailer Next, we will need to install Express using: npm install expressĭepending which file you pointed to as your entry point (the default is index.js), open it and paste the following code: const express = require('express')Ĭonsole.log(`nodemailerProject is listening at }) index.jsĪbove is what is needed to start a simple server using Express. This will initialize our project with a pacakge.json file. Go inside the newly created directory and run npm init Otherwise, install what is missing.Ĭreate a directory for your project. If both of these commands show a version, you are good to go. To make sure you have Node and npm installed, you can run the following commands: node -v How to Get Started with Nodemailerįirst, we need to set up our Node.js boilerplate using Express. Here, I will show the most common practice of sending an email from your Node.js backend using Nodemailer and Gmail. There are many articles out there explaining how to use Nodemailer in barebones form, but this article is not one of them. Whether you want to communicate with your users or just notify yourself when something has gone wrong, one of the options for doing so is through mail. Let transport = nodemailer.Nodemailer is a Node.js module that allows you to send emails from your server with ease. There are countless reasons why you might want to include an attachment in your email, like sending pictures, spreadsheets for work, or videos.Īs another example, if you run an e-commerce website, for example, you might want to send receipts to your customers: const nodemailer = require( "nodemailer") With both versions in your email message, the recipient's email client can then choose which one to display. Keep in mind that you can also send both HTML and plain text in a single email by providing both parameters. Let's go ahead and create a new folder and a new package.json file with the npm init command: It is a module that gives you the ability to easily send emails without hassle. Amongst them, nodemailer is the most popular choice.

There are many Node.js modules for sending e-mails. To test sending emails from a local development machine, without having to configure a server, we will be using Mailtrap.

To follow along, you will need to have Node.js and npm (Node Package Manager) installed locally. In this tutorial you'll learn how to send emails with HTML content and attachments using the nodemailer module, as well as set up Mailtrap, a fake SMTP server, for testing your code. So, you are thinking about sending emails from your next great Node.js application. Email is one of the most used tools for communication in web applications because it helps you reach your users directly, build your brand, or send general notifications.
