DEV Community

Aditya Rawas
Aditya Rawas

Posted on • Edited on

Send Email using Node.js

Want to send email using node.js ??,
Its simple here are some steps that will allow you to send email using node.js using nodemailer.

STEP 1 - Install nodemailer

Use following command to install nodemailer

npm install nodemailer
Enter fullscreen mode Exit fullscreen mode

STEP 2 - Create Transporter

import nodemailer and create transporter

var nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    service: 'gmail' // here I am using gmail service
    auth:{
       user: 'emailid@gmail.com,
       passoword: 'your gmail app passsword'

    }
});
Enter fullscreen mode Exit fullscreen mode

Remember the password you are going to use is not the password for email rather you will have to generate application password provided by gmail. Click here to see how to generate application password

STEP 3 - Create Options

let mailOptions = {
  to: 'myfriend@yahoo.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
};
Enter fullscreen mode Exit fullscreen mode

STEP 4 - Send Email

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});
Enter fullscreen mode Exit fullscreen mode

IF YOU LIKED THIS ARTICLE MAKE SURE TO FOLLOW ME ON DEV.TO AND ON INSTAGRAM

Aditya Rawas Coffee

Top comments (2)

Collapse
 
steve-lebleu profile image
Steve Lebleu

Thanks for sharing ! Here a package which can help with email sending in node.js environment. One library for many providers.

GitHub logo konfer-be / cliam

Agnostic transactional email sending in Node.js environment

Cliam

Build Status Coverage Status CodeFactor Grade Requires.io (branch) GPL Licence

Transactional emails with a kick

Agnostic transactional email sending in Node.js environment 💥 💪 💊

> Why ?

To improve and facilitate the implementation, flexibility and maintenance of transactional emailing tasks.

> Features

  • Agnostic transactional email sending using web API or SMTP server. One input, one output.
  • Configuration based, not implementation based : easy switch between different modes.
  • Normalized transactions events.
  • Securized payloads.
  • Customisable default templates.

> Table of contents

> Requirements

  • Node.js >= 14.16.0
  • NPM >= 6.14.11

> Getting started

Install

> npm i cliam --save
Enter fullscreen mode Exit fullscreen mode

Configure

Create a .cliamrc.json file on the root of your project.

> touch .cliamrc.json
Enter fullscreen mode Exit fullscreen mode

Define a minimalist configuration in .cliamrc.json newly created:

{
  "consumer": {
    "domain": "https://www.john-doe.com"
  }
  "mode": {
    "api": {
      "name": "YOUR_PROVIDER",
      "credentials": {
        "apiKey": 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rawas_aditya profile image
Aditya Rawas

Thanks for sharing