I won’t get into the specifics here as it is not necessary, but if you want to understand more about it, go here.įirst we will have to configure our OAuth Consent Screen: If you aren’t familiar with OAuth, it is a protocol for authentication. In order to be able to use Nodemailer and Gmail we will have to use OAuth2. Open up the navigation menu by clicking the three dashed lines in the top left corner and select APIs and Services: It may take a few seconds to for the project to be set up, but after that you will be able to see this screen: For the location property, you can leave it as No organization. Pick whatever you like, but we will continue with out NodemailerProject name. In the next window, we will have to give our project a name. Once you have that set up, create a new project by clicking on the dropdown menu in the upper left corner. If you don’t have a Google Cloud Platform account, be sure to set one up as a prerequisite. Photo by Daniel McCullough / Unsplash Google Cloud Platform Configurations The next steps will require some configurations instead of coding, so brace yourselves. 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: Install nodemailer using the following command: npm install nodemailer 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. To target a Message Transfer Agent (MTA), omit all options.Nodemailer is a Node.js module that allows you to send emails from your server with ease. Import // ssl/tls objects are an abbreviated form of ()'s options // the missing items are: `port`, `host`, `path`, `socket`, `timeout` and `secureContext` // NOTE: `host` is trimmed before being used to establish a connection // however, the original untrimmed value will still be visible in configuration.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |