core/lib/module/sendmail.js

47 lines
944 B
JavaScript

/**
*
* @authors yutent (yutent@doui.cc)
* @date 2015-11-27 10:50:16
*
*/
'use strict'
const mailx = require('mailx')
export default class Sendmail {
constructor({ host, port, mail, passwd } = {}) {
if (!host || !port || !mail || !passwd) {
throw new Error('smtp options [host, port, mail, passwd] is required.')
}
this.smtp = mailx.transport(host, port, mail, passwd)
this.mail = mailx.message()
}
// 发件人
from(info) {
this.mail.setFrom(info.name, info.mail)
return this
}
// 收件人
to(info) {
this.mail.addTo(info.name, info.mail)
return this
}
// 发送正文
send(mail) {
this.mail.setSubject(mail.subject)
this.mail.setHtml(mail.content)
var defer = Promise.defer()
this.smtp.send(this.mail, function(err, res) {
if (err) {
defer.reject(err)
} else {
defer.resolve(res)
}
})
return defer.promise
}
}
一个轻量级的,易学的,拓展性灵活的 nodejs MVC 框架, 5 分钟即可上手。取自"Give me five"之意, 一切就是这么简单
JavaScript 100%