Giter VIP home page Giter VIP logo

demo-nodemailer-gmail's Introduction

Nodemailer with Gmail

npm init -y
touch index.js .env .gitignore
npm i express nodemailer
mkdir utils
touch utils/nodemailer.js

Add rules to gitignore

add start script

"scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

.env

GMAIL_SENDER=
GMAIL_CLIENT_ID=
GMAIL_CLIENT_SECRET=
ACCESS_TOKEN=
REFRESH_TOKEN=

Setting up Google Cloud Platform

go to GCP console

create new project -> demo-nodemailer-gmail

API & Services -> OAuth consent screen
select External

click Create

Enter App name support email

Add scopes

add https://mail.google.com/ manually, update

save and continue

Add user (i.e. )

Create OAuth credentials

on GCP console go to

  1. Credentials -> Create credentials -> OAuth Client ID

  2. select web application

  3. JS authorised origins -> http://127.0.0.1:5000

  4. Authorized redirect URIs -> https://developers.google.com/oauthplayground

  5. Click create

Get Tokens

  1. go to https://developers.google.com/oauthplayground

  2. go to the gear icon on the upper right check use your own OAuth credentials type in your app Client ID and Client Secret, and close this menu.

  3. Look for Gmail API v1, click on the toggle and check at least the first ("https://mail.google.com")

  4. Click Authorize API

  5. Click Exchange Authorization code for tokens

  6. Save yor Refresh and Access tokens in your .env

Add nodemailer

Create transporter

in utils/nodemailer.js

"use strict";
const nodemailer = require("nodemailer");

let transporter = nodemailer.createTransport({
    host: "smtp.gmail.com",
    port: 465,
    secure: true,
    auth: {
        type: "OAuth2",
        clientId: process.env.GMAIL_CLIENT_ID,
        clientSecret: process.env.GMAIL_CLIENT_SECRET
    }
});

module.exports = transporter;

Send email

use the sendMail method of the transporter to send an email specifying auth credentials.

transporter.sendMail({
        from: process.env.GMAIL_SENDER,
        to: emailrecipient,
        subject: "Nodemailer from gmail",
        text: "I hope this message gets through!",
        auth: {
            user: process.env.GMAIL_SENDER,//gmail address
            refreshToken: process.env.REFRESH_TOKEN,
            accessToken: process.env.ACCESS_TOKEN,
            expires: 1484314697598,
        },
    });

run your app with npm start and visit your endpoint.

Thanks to Alex: How to send emails using NodeMailer, gmail and OAuth2

demo-nodemailer-gmail's People

Contributors

guille-rubio avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.