-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
166 lines (142 loc) · 4.59 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
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Module dependencies
*/
var Resource = require('deployd/lib/resource'),
util = require('util'),
nodemailer = require('nodemailer'),
smtp = require('nodemailer-smtp-transport'),
htmlToText = require('nodemailer-html-to-text').htmlToText,
inlineBase64 = require('nodemailer-plugin-inline-base64');
/**
* Module setup.
*/
function Email( ) {
Resource.apply( this, arguments );
var authParams = null;
if (this.config.username !== '' || typeof process.env.DPD_EMAIL_USERNAME !== 'undefined') {
authParams = {
user: this.config.username || process.env.DPD_EMAIL_USERNAME,
pass: this.config.password || process.env.DPD_EMAIL_SMTP_PASSWORD
};
}
this.transport = nodemailer.createTransport(smtp({
host : this.config.host || process.env.DPD_EMAIL_HOST || 'localhost',
port : parseInt(this.config.port, 10) || process.env.DPD_EMAIL_PORT || 25,
secure : this.config.ssl,
//service: 'gmail',
auth : authParams
}))
this.transport.use('compile', htmlToText({}) );
}
util.inherits( Email, Resource );
Email.prototype.clientGeneration = true;
Email.basicDashboard = {
settings: [
{
name : 'host',
type : 'text',
description : 'Host name of your SMTP provider. Defaults to DPD_EMAIL_HOST env variable or \'localhost\'.'
}, {
name : 'port',
type : 'numeric',
description : 'Port number of your SMTP provider. Defaults to DPD_EMAIL_PORT env variable or 25'
}, {
name : 'ssl',
type : 'checkbox',
description : 'Use SSL.'
}, {
name : 'username',
type : 'text',
description : 'SMTP username. Leave blank to use the DPD_EMAIL_USERNAME env variable'
}, {
name : 'password',
type : 'text',
description : 'SMTP password. Leave this blank to use the DPD_EMAIL_SMTP_PASSWORD environment variable'
}, {
name : 'defaultFromAddress',
type : 'text',
description : 'Optional; if not provided will use the DPD_EMAIL_DEFAULT_FROM env var or you will need to provide a \'from\' address in every request'
}, {
name : 'internalOnly',
type : 'checkbox',
description : 'Only allow internal scripts to send email'
}, {
name : 'productionOnly',
type : 'checkbox',
description : 'If on development mode, print emails to console instead of sending them'
},{
name : 'base64',
type : 'checkbox',
description : 'If using base64 encrypted images, encode them properly'
}]
};
/**
* Module methodes
*/
Email.prototype.handle = function ( ctx, next ) {
if ( ctx.req && ctx.req.method !== 'POST' ) {
return next();
}
if ( !ctx.req.internal && this.config.internalOnly ) {
return ctx.done({ statusCode: 403, message: 'Forbidden' });
}
var options = ctx.body || {};
options.from = options.from || this.config.defaultFromAddress || process.env.DPD_EMAIL_DEFAULT_FROM;
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 ? options.subject.trim() : '';
options.text = options.text ? options.text.trim() : '';
var that = this;
var env = that.options.server.options.env;
if (that.config.productionOnly && env != 'production') {
console.log('_______________________________________________');
console.log('Sent email:');
console.log('From: ', options.from);
console.log('To: ', options.to);
if (options.cc) {
console.log('CC: ', options.cc);
}
if (options.bcc) {
console.log('BCC: ', options.bcc);
}
console.log('Subject: ', options.subject);
if (options.text) {
console.log('Text:');
console.log( options.text );
}
if (options.html) {
console.log('HTML:');
console.log( options.html );
}
console.log('```````````````````````````````````````````````');
return ctx.done( null, { message : 'Simulated sending' } );
}
if(options.html && that.config.base64){
that.transport.use('compile', inlineBase64);
}
that.transport.sendMail(
options,
function( err, response ) {
if ( err ) {
return ctx.done( err );
}
ctx.done( null, { message : response.message } );
}
);
}
/**
* Module export
*/
module.exports = Email;