Tuesday, July 14, 2015

Contact form with Node.js

Let's see that how to make simple contact form with Node.Js nodemailer module


  • install nodemailer - npm install nodemailer

server.js

var express=require('express');
var nodemailer = require("nodemailer");
var app=express();
var smtpTransport = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "bandara.susantha@gmail.com",
pass: "bankaya86"
}

});
app.get('/',function(req,res){
console.log('works');
res.sendfile('index.html');
});
app.get('/send',function(req,res){
var mailOptions={
to : req.query.to,
subject : req.query.subject,
text : req.query.text
}
console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
res.end("error");
}else{
console.log("Message sent: " + response.message);
res.end("sent");
}
});
});
app.listen(3000,function(){
console.log("Express Started on Port 3000");
});


index.html(html file)

"












"




  • when you use this inside the Apache (LAMP or Xampp ) you should enable the proxypass 
  • for that you can add this into end of your virtual host fileProxyPass /send http://localhost:3000/send


No comments:

Post a Comment