-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
154 lines (129 loc) · 3.56 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var Resource = require('deployd/lib/resource'),
util = require('util'),
nodemailer = require('nodemailer'),
smtpTransport = require('nodemailer-smtp-transport')
function Mail(name, options) {
Resource.apply(this, arguments);
var authParams = null;
//If an empty string needs to be passed as username, use the environment variable
if (this.config.username !== '') {
authParams = {
user: this.config.username,
pass: this.config.password
};
}
this.transport = nodemailer.createTransport(smtpTransport({
host: this.config.host || "localhost",
port: parseInt(this.config.port, 10) || 25,
secure: this.config.ssl,
auth: authParams,
requireTLS: true,
tls: {
rejectUnauthorized: false
}
}));
//this.transport.use('compile', htmlToText({}) );
}
util.inherits(Mail, Resource); //aplicar herencia al recurso
module.exports = Mail; //generar el recurso
Mail.prototype.clientGeneration = true; //habilitar al cliente para generar el recurso
//Se crea el panel de control con los campos para guardar en configuracion
Mail.basicDashboard = {
settings: [{
name: 'host',
type: 'text',
description: 'Host name of your SMTP provider. Defaults to \'localhost\'.'
}, {
name: 'port',
type: 'number',
description: 'Port number of your SMTP provider. Defaults to 25'
}, {
name: 'ssl',
type: 'checkbox',
description: 'Use SSL.'
}, {
name: 'username',
type: 'text',
description: 'SMTP username.'
}, {
name: 'password',
type: 'text',
description: 'SMTP password.'
}, {
name: 'defaultFromAddress',
type: 'text',
description: 'Optional; if not provided you will need to provide a \'from\' address in every request'
}, {
name: 'internalOnly',
type: 'checkbox',
description: 'Only allow internal scripts to send email'
}, {
name: 'secret',
type: 'text',
description: 'secret key or password to make external requests, only for external calls, will be ignored for internal calls'
}, {
name: 'async',
type: 'checkbox',
description: 'make the email request asynchronous'
}]
};
Mail.prototype.handle = function(ctx, next) { //Comportamiento al llamar funcion
if (ctx.req && ctx.req.method !== 'POST') {
return next();
}
//check for internal only flag
if (!ctx.req.internal && this.config.internalOnly) {
return ctx.done({
statusCode: 403,
message: 'Forbidden'
});
}
//check for secretKey
if (this.config.secret !== ctx.body.secret && !ctx.req.internal) {
{
return ctx.done({
statusCode: 401,
message: 'Unauthorized: Access is denied due to invalid credentials'
});
}
}
var options = ctx.body;
options.from = options.from || this.config.defaultFromAddress;
var errors = {};
if (!options.to) {
errors.to = '\'to\' is required';
}
if (!options.from) {
errors.from = '\'from\' is required';
}
if (!options.text && !options.html) {
errors.text = '\'text\' or \'html\' is required';
}
if (Object.keys(errors).length) {
return ctx.done({
statusCode: 400,
errors: errors
});
}
// trim
options.subject = options.subject.trim();
options.text = options.text.trim();
var that = this;
that.transport.sendMail(
options,
function(err, response) {
if (err) {
console.log(err)
return ctx.done(err);
}
ctx.done(null, {
message: response.message
});
}
);
if(this.config.async) {
ctx.done(null, {
message: 'email is queued for sending'
});
}
}