nodemailer
nodemailer
套件資訊
Installation
npm install nodemailer
Sample Usage
官網的操作說明:
/** * 官網提供的操作方式 */ var nodemailer = require("nodemailer"); // create reusable transport method (opens pool of SMTP connections) var smtpTransport = nodemailer.createTransport("SMTP",{ service: "Gmail", auth: { user: "[email protected]", pass: "userpass" } }); // setup e-mail data with unicode symbols var mailOptions = { from: "Fred Foo ✔ < [email protected] >", // sender address to: "[email protected], [email protected]", // list of receivers subject: "Hello ✔", // Subject line text: "Hello world ✔", // plaintext body html: "Hello world ✔" // html body } // send mail with defined transport object smtpTransport.sendMail(mailOptions, 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 });
作者將nodemailer包裝在常用的套件nodeuitl中,並且實作template的寄件方式,讓操作上更加方便:
範例:
/** * 包裝於nodeutil的使用方式 */ var nu = require('nodeutil'); var mailer = nu.mailutil; mailer.init( {"smtpOptions":{"service":"Gmail", "auth": {"user": "your-account","pass": "your-password"}}, "sender": "NO-REPLY"} ); mailer.sendTemplateMail({ sender:"Simon Su ", receivers:["[email protected]"], subject:"Test template mail", contentTemplate:__dirname + "/test-nmail.tpl", contentValues:{ USER:"Simon", DATE:new Date().toString() } }, function(res){ console.log(res); });
樣板;
<h2>Hello $USER</h2> <hr/> This is a test mail from nodeutil!<br/> Thanks for use. <br/> <br/> 新年快樂! <hr/> <h3>Send from NodeUtil@$DATE</h3>