forked from outbounder/organic-emailsender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (56 loc) · 1.58 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var path = require("path")
var _ = require('lodash')
module.exports = function(plasma, dna){
this.plasma = plasma
this.dna = dna
if(dna.email.transport == "console.log") {
this.transport = {
sendMail: function(data, next) {
console.log(data)
next()
}
}
} else
if(dna.email.transport == "devnull") {
this.transport = {
sendMail: function(data, next) {
next()
}
}
} else
if(dna.email.transport == "plasma") {
this.transport = {
sendMail: function(data, next) {
var chemical = _.extend({}, data, {type: dna.email.options.emitAs })
if (dna.waitForDelivery) {
plasma.emit(chemical, next)
} else {
plasma.emit(chemical)
next()
}
}
}
} else {
var transportInitPath = dna.email.transport
if (!isPathFull(transportInitPath)) {
transportInitPath = path.join(process.cwd(), transportInitPath)
}
this.transport = require(transportInitPath)(dna.email.options)
if (!this.transport || !this.transport.sendMail) {
throw new Error(dna.email.transport + ' is not valid sendMail transport')
}
}
if(dna.log)
console.log("sending emails using", dna.email.transport, dna.email.options)
var self = this
plasma.on(dna.reactOn || "deliverEmail", function(c, next){
c.to = c.to || dna.to
c.from = c.from || dna.from
self.transport.sendMail(c, next || function (err) {
if (err) console.error(err)
})
})
}
var isPathFull = function (value) {
return value.indexOf("/") === 0 || value.indexOf(":\\")===1
}