-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (30 loc) · 1.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var EventEmitter = require('events').EventEmitter
var nodemailer = require('nodemailer')
var xtend = require('xtend')
module.exports = function loginMailer(core, options) {
if (!options || typeof options !== 'object') throw new Error('Expected options object')
if (typeof options.createHtmlEmail !== 'function') throw new Error('Expected options.createHtmlEmail')
if (typeof options.transport !== 'object') throw new Error('Expected options.transport')
var defaultFrom = options.transport.auth && options.transport.auth.user
var defaultMailOpts = xtend({
from: defaultFrom,
sender: defaultFrom,
subject: 'Login'
}, options.mail || {})
var emitter = new EventEmitter()
var transport = nodemailer.createTransport(options.transport)
core.on('authentication initiated', function (loginRequest) {
var mailOpts = xtend(defaultMailOpts, {
to: loginRequest.contactAddress,
html: options.createHtmlEmail(loginRequest.token)
})
transport.sendMail(mailOpts, function (err, info) {
if (err) {
emitter.emit('error', err)
} else {
emitter.emit('data', info)
}
})
})
return emitter
}