forked from senecajs/seneca-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.js
133 lines (112 loc) · 3.31 KB
/
mail.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Copyright (c) 2013-2014 Richard Rodger, MIT License */
'use strict';
var _ = require( 'underscore' )
var nodemailer = require( 'nodemailer' )
var transports = {
'smtp': 'nodemailer-smtp-transport',
'ses': 'nodemailer-ses-transport',
'smtpPool': 'nodemailer-smtp-pool',
'sendmail': 'nodemailer-sendmail-transport',
'stub': 'nodemailer-stub-transport',
'pickup': 'nodemailer-pickup-transport'
}
module.exports = function( options ) {
var seneca = this
var plugin = 'mail'
var transport
options = this.util.deepextend( {
content: {},
mail: {},
transport: 'smtp',
config: {}
}, options )
var sendMail = function( args, done ) {
var seneca = this
if( args.code ) {
this.act(
{
role: plugin, hook: 'content',
code: args.code,
content: _.extend(
{},
options.content,
options.content[args.code] || {},
args.content
)
}, function( err, content ) {
if( err ) {
return done( err )
}
seneca.act( {role: plugin, cmd: 'generate', code: args.code, content: content}, function( err, out ) {
if( err ) {
return done( err )
}
do_send( out )
} )
} )
}
else {
do_send( {html: args.html, text: args.text} )
}
function do_send( body ) {
var sendargs = _.extend(
{},
options.mail,
args,
{
cmd: null,
hook: 'send',
text: body.text,
html: body.html
}
)
if( body.subject ) {
sendargs.subject = body.subject
}
seneca.log.debug( 'send', sendargs.code || '-', sendargs.to )
seneca.act( sendargs, done )
}
}
var getContent = function( args, done ) {
var code = args.code // template identifier
var content = this.util.deepextend( {}, options.content[code] || {}, args.content || {} )
done( null, content )
}
seneca.add( {role: plugin, hook: 'send'}, function( args, done ) {
transport.sendMail( args, function( err, response ) {
if( err ) {
return done( err );
}
done( null, {ok: true, details: response} )
} )
} )
function initTransport( options, callback ) {
var transportPluginName = transports[options.transport] || options.transportPluginName || options.transport
var transportPlugin
seneca.log.debug( 'Loading specified transport definition: ' + transportPluginName )
transportPlugin = require( transportPluginName )
transport = nodemailer.createTransport( transportPlugin( options.config ) )
callback( null, {} )
}
var close = function( args, done ) {
if( transport && transport.close && _.isFunction( transport.close ) ) {
transport.close( done )
}
this.prior( args, done )
}
if( options.folder ) {
require( './lib/templateActions' )( seneca, options )
}
else {
require( './lib/defaultActions' )( seneca, options )
}
seneca.add( {role: plugin, hook: 'init', sub: 'transport'}, function( args, done ) {
initTransport( args.options, done )
} )
seneca.add( {role: plugin, cmd: 'send'}, sendMail )
seneca.add( {role: plugin, hook: 'content'}, getContent )
seneca.add( {role: 'seneca', cmd: 'close'}, close )
return {
name: plugin
}
}