1. sudo npm install nodemailer
2. Import the following class to your source code
var nodemailer = require("nodemailer");
function MailPoster( to, subject, body){
this.from = 'XXXXXXX@gmail.com';
this.to = to;
this.subject = subject;
this.body = body;
}
MailPoster.prototype.send = function(){
//create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "XXXXX@gmail.com",
pass: "XXXXX"
}
});
// send mail with defined transport object
smtpTransport.sendMail(this, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
// if you don't want to use this transport object anymore, uncomment following line
//smtpTransport.close(); // shut down the connection pool, no more messages
});
}
module.exports = MailPoster;
3. Usage
var MailPoster = require("./MailPoster.js");
var poster = new MailPoster("89959595@qq.com","hello mailposter","this is body of mail poster");
poster.send();