GithubHelp home page GithubHelp logo

mahendran-balaji / send-email-from-nodejs-using-nodemailer Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 4 KB

Send email from node js application using nodemailer package

JavaScript 100.00%
emailjs gmail gmail-smtp mail mailchimp node-js nodejs nodemailer

send-email-from-nodejs-using-nodemailer's Introduction

send-email-from-nodejs-using-nodemailer

Send email from node js application using nodemailer package

Step 1: Setup new npm package

  npm init

Step 2 : Install the following packages: express, dotenv, nodemailer

 npm install express
 npm install dotenv
 npm install nodemailer

Step 3: Create a new .env file in the root directory

Step 4: Add Following Code into the .env file

PORT_NUMBER = 3001
MAIL_HOST=**************    /* Mail Host Name (Ex: smtp.gmail.com) */
MAIL_USERNAME=************  /* Mail username */
MAIL_PASSWORD=**********    /* Mail password or app password */
MAIL_PORT=465               /* 465 0r 587 */
MAIL_SERVICE=gmail          /* Email Service : (Ex: gmail) */
MAIL_SECURE=false           /* True or False */
MAIL_FROM=**************    /* From Email Address */

Step 5 : To create index.js file add following code

const path = require('path');
const express = require('express');
const app = express();
const dotenv = require('dotenv').config({ path: __dirname+'/.env' })
const env = dotenv.parsed;
const mail = require('./mail');  /* Add External mail.js File */
const portNumber = env.PORT_NUMBER;

    app.get('/',(req,res) => {
        try {
            mail.sendMail();
            res.send("Hello Friends.");
        }
        catch (e) {
            console.log('Error :'+e);
        }
    });

    app.listen(portNumber,() => {
        console.log(`listening on port ${portNumber}!`);
    });

Step 6 : To create index.js file add following code

const dotenv = require('dotenv').config({ path: __dirname+'/.env' })
const env = dotenv.parsed;
const nodemailer = require('nodemailer');

    function sendMail()
    {
        console.log('inside send mail');
        const mailOptions = {
            to: '[email protected]',
            from: env.MAIL_FROM,
            subject: 'Test Local Node JS Email',
            text: 'Hi Buddy',
            html: "<b>Hello world?</b>",
        };


        const transporter = nodemailer.createTransport({
            host: env.MAIL_HOST,
            port: env.MAIL_PORT,
            secure: env.MAIL_SECURE,
            auth: {
                user: env.MAIL_USERNAME,
                pass: env.MAIL_PASSWORD
            },
        });

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

        }catch (e) {
            console.log(e);
        }
}

module.exports = { sendMail };

Step 7: Run the application

  node index.js

Author

send-email-from-nodejs-using-nodemailer's People

Contributors

mahendran-balaji avatar

Stargazers

 avatar

Watchers

 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.