Node.js mail module
之前介紹過node.js的一個mail module
但是當嘗試寄送html base的信件時候
發現支援有些問題....冏
這邊介紹另一個好用的套件nodemailer
安裝:npm install nodemailer
這邊直接將nodemailer包裝一下,使用者可以稍加修改後給專案使用
自製module: (file: sendMyMail.js)
var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "my_mail@gmail.com",
pass: "**password**"
}
});
var mailOptions = {
from: "MyMail <my_mail@gmail.com>", // sender address
to: "receiver@mycompany.com", // list of receivers
subject: "Hello", // Subject line
text: "Hello world ", // plaintext body
html: "<h1>MAIL...</h1><br/><b>Hello world </b>" // html body
}
exports.sendNodeMail = function(receivers, subject, msg) {
mailOptions.to = receivers;
mailOptions.subject = subject;
mailOptions.html = msg;
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close(); // shut down the connection pool, no more messages
});
}
使用方式:
var mail = require('./sendMyMail');
var receivers = ['receiver1@xmail.com','receiver2@xmail.com'];
var msg = '<h1>Title...</h1><h2>messages...</h2>';
mail.sendNodeMail(receivers, 'Sample subject...', msg);
該套件還包含添加附件功能
下次再介紹 :D