6 Replies to “How to send email in nodejs”

  1. Hello Rohit,

    I have just try your code but I am getting error states :”nodemailer,mail is not a function” as I am using nodemailer 2.6.4. and when I print nodemailer then it just print only function createTransport.

  2. Thanks For code.
    I had also got error of nodemail.mail is not a function

    I resolve all error now my code is working properly.

    I have used following code for app.js

    var http=require(‘http’);
    var express=require(‘express’);
    var nodemailer = require(“nodemailer”);
    var bodyParser = require(‘body-parser’)
    var app=express();

    var port = Number(process.env.PORT || 5000);
    app.use(bodyParser.json()); // to support JSON-encoded bodies
    app.use(bodyParser.urlencoded({
    extended: true
    }));

    // Home page
    app.get(‘/’,function(req,res){
    // res.sendFile(‘index.html’);
    res.sendFile(__dirname + ‘/index.html’);
    });

    // sending mail function
    app.post(‘/send’, function(req, res){
    if(req.body.email == “” || req.body.subject == “”) {
    res.send(“Error: Email & Subject should not blank”);
    return false;
    }
    // Sending Email Without SMTP
    nodemailer.mail({
    from: req.body.email, // sender address
    to:”[email protected]”, // list of receivers
    subject: req.body.subject+” ✔”, // Subject line
    //text: “Hello world ✔”, // plaintext body
    html: ““+req.body.description+”” // html body
    });
    res.send(“Email has been sent successfully”);

    // Sending Emails with SMTP, Configuring SMTP settings
    /*
    var smtpTransport = nodemailer.createTransport(“SMTP”,{
    host: “imap.gmail.com”, // hostname
    secureConnection: true, // use SSL
    port: 993, // port for secure SMTP
    auth: {
    user: “[email protected]”,
    pass: “password”
    }
    });
    var mailOptions = {
    from: req.body.email, // sender address
    to: ‘[email protected]’, // list of receivers
    subject: req.body.subject+” ✔”, // Subject line
    //text: “Hello world ✔”, // plaintext body
    html: ““+req.body.description+”” // html body
    }
    smtpTransport.sendMail(mailOptions, function(error, response){
    if(error){
    res.send(“Email could not sent due to error: “+error);
    }else{
    res.send(“Email has been sent successfully”);
    }
    }); */
    });

    // Starting server
    var server = http.createServer(app).listen(port, function() {
    console.log(“Listening on ” + port);
    });

    But
    mails are receiving in spam.
    what to do for receive mail in inbox????

  3. sir,

    I make node js program on windows installer.But for hosting that node js web application on cpanel ,it should be support for linux.

    How can I make node js program in windows installer which support to linux also??

    Please guide me sir

    Thanks Sir,
    Prerana

  4. When I run the code I get the following error in the console :

    Listening on 5000
    events.js:160
    throw er; // Unhandled ‘error’ event
    ^

    Error: read ECONNRESET
    at exports._errnoException (util.js:1022:11)
    at TCP.onread (net.js:569:26)

    What is the problem?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.