notificator
Version:
Library for handling custom notifications, emails, APS, GCM...
61 lines (47 loc) • 1.72 kB
text/coffeescript
nodemailer = require('nodemailer')
validator = require('validator')
util = require('util')
NotificatorChannel = require('../NotificatorChannel')
class EmailTemplate extends NotificatorChannel.ChannelTemplate
constructor:(,,)->
class EmailChannel extends NotificatorChannel
constructor:(options)->
super(options)
getTransport:()->
if not
= nodemailer.createTransport()
return
sendMessage:(message,destination,callback)->
messageOptions = {
from: .sender
to: destination.destination
subject: message.subject
text: message.text
html: message.html
}
(messageOptions)
().sendMail(messageOptions,callback)
validateTemplate:(template)->
super(template)
if template not instanceof EmailTemplate
throw new Error('template must be type of EmailTemplate')
if not template.subject
throw new Error('email template must have subject')
if not template.text and not template.html
throw new Error('email template must have text or html')
validateDestination:(destination)->
super(destination)
if not validator.isEmail(destination.destination)
throw new Error(util.format(destination.destination) + ' is not valid email')
return yes
wrappedDestination:(destination)->
if destination?.email
destination.destination = destination.email
delete destination.email
return super(destination)
transformTemplate:(template)->
return new EmailTemplate(template.subject,template.text,template.html)
name:()->
return 'Email'
EmailChannel.Template = EmailTemplate
module.exports = EmailChannel