DEV Community

Aditya Rawas
Aditya Rawas

Posted on β€’ Edited on

2 2

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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay