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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
"use strict";
var FileHandler = require('./file_handler');
var smtpapi = require('smtpapi');
var _ = require('lodash');
var request = require('request');
var fs = require('fs');
/**
* Class to handle storing the properties relative to an email.
*
* @param {Object} params
* @param {string|array} params.to The to address(es) of the email
* @param {string|array} params.toname The display name of the email recipients
* @param {string} params.from The from address of the email
* @param {string} params.fromname The display name of the email sender
* @param {SmtpapiHeaders} params.smtpapi The SendGrid x-smtpapi headers object
* @param {string} params.subject The subject of the email
* @param {string} params.text The text/plain content of an email
* @param {string} params.html The text/html content of an email
* @param {string|array} params.bcc The bcc address(es) of the email
* @param {Date} params.date The date of the email
* @param {object} params.headers The custom headers on an email
*/
function Email(params) {
params = params || {};
this.to = params.to || [];
this.from = params.from || '';
this.smtpapi = params.smtpapi || new smtpapi.Header();
this.subject = params.subject || '';
this.text = params.text || '';
this.html = params.html || '';
this.bcc = params.bcc || [];
this.replyto = params.replyto || '';
this.date = params.date || new Date();
this.headers = params.headers || {};
if(params.toname !== null) {
this.toname = params.toname;
}
if(params.fromname !== null) {
this.fromname = params.fromname;
}
// auto handle calling the constructor for the file handler
this.files = [];
if (params.files) {
params.files.forEach(function(file) {
this.files.push(new FileHandler(file));
}, this);
}
}
Email.prototype.addHeader = function(object_literal_or_key, value) {
if (typeof value === "undefined" || value === null) {
value = "";
}
if (_.isObject(object_literal_or_key)) {
_.extend(this.headers, object_literal_or_key);
} else {
var object_to_add = {}
object_to_add[object_literal_or_key] = value;
_.extend(this.headers, object_to_add);
}
};
Email.prototype.setHeaders = function(object_literal) {
if (_.isObject(object_literal)) {
this.headers = object_literal;
}
};
Email.prototype.addTo = function(to) {
this.smtpapi.addTo(to);
};
Email.prototype.setTos = function(tos) {
this.smtpapi.setTos(tos);
};
Email.prototype.setFrom = function(from) {
this.from = from;
};
Email.prototype.setSubject = function(subject) {
this.subject = subject;
};
Email.prototype.setText = function(text) {
this.text = text;
};
Email.prototype.setHtml = function(html) {
this.html = html;
};
Email.prototype.addSubstitution = function(key, val) {
this.smtpapi.addSubstitution(key, val);
};
Email.prototype.setSubstitutions = function(substitutions) {
this.smtpapi.setSubstitutions(substitutions);
};
Email.prototype.addUniqueArg = function (object_literal) {
this.smtpapi.addUniqueArg(object_literal);
};
Email.prototype.setUniqueArgs = function(object_literal) {
this.smtpapi.setUniqueArgs(object_literal);
};
Email.prototype.addCategory = function(val) {
this.smtpapi.addCategory(val);
};
Email.prototype.setCategories = function(val) {
this.smtpapi.setCategories(val);
};
Email.prototype.addSection = function(object_literal) {
this.smtpapi.addSection(object_literal);
};
Email.prototype.setSections = function(object_literal) {
this.smtpapi.setSections(object_literal);
};
Email.prototype.addFilter = function(filter, setting, val) {
this.smtpapi.addFilter(filter, setting, val);
};
Email.prototype.setFilters = function(filters) {
this.smtpapi.setFilters(filters);
};
Email.prototype.addFile = function(file_object) {
this.files.push(new FileHandler(file_object));
};
Email.prototype.toWebFormat = function() {
var web = {
to : this.to,
from : this.from,
'x-smtpapi' : this.smtpapi.jsonString(),
subject : this.subject,
text : this.text,
html : this.html,
headers : JSON.stringify(this.headers)
};
if (this.bcc) { web.bcc = this.bcc; }
if (this.html) { web.html = this.html; }
if (this.toname) { web.toname = this.toname; }
if (this.fromname) { web.fromname = this.fromname; }
if (this.replyto) { web.replyto = this.replyto; }
if (this.smtpapi.to && this.smtpapi.to.length) { web.to = ""; }
this.updateMissingTo(web);
if (this.files) {
this.files.forEach(function(file) {
if (file.url) {
file.content = request(file.url);
} else if (file.path) {
file.content = fs.createReadStream(file.path);
}
web['files[' + file.filename + ']'] = {
filename : file.filename,
content : file.content || " ",
contentType : file.contentType,
cid : file.cid
};
});
}
return web;
};
Email.prototype._formatFilesForNodeMailer = function(files) {
var self = this;
if (files && files.length > 0) {
files.forEach(function(file) {
self._formatFileForNodeMailer(file);
});
}
};
Email.prototype._formatFileForNodeMailer = function(file) {
if (file.filename) {
file.fileName = file.filename;
}
if (file.path) {
file.filePath = file.path;
}
if (file.url) {
file.filePath = file.url;
}
if (file.content) {
file.contents = file.content;
}
if (!file.filePath && !file.content && file.filename) {
file.contents = " ";
}
};
/**
* There needs to be at least 1 to address, or else the mail won't send.
* This method modifies the data that will be sent via either Rest
*
* @param {object} data The data parameter to send via Rest
*/
Email.prototype.updateMissingTo = function(data) {
if (this.smtpapi.to && this.smtpapi.to.length > 0) {
data.to = this.from;
}
};
/**
* This method is used to show if there are files on this email object
*
* @return boolean
*/
Email.prototype.hasFiles = function() {
return this.files.length > 0;
};
// export the object as the only object in this module
module.exports = Email;
|