/
nodemailer.js
285 lines (240 loc) · 8.48 KB
/
nodemailer.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
var Transport = require("./transport").Transport,
MailComposer = require("mailcomposer").MailComposer,
XOAuthGenerator = require("./xoauth").XOAuthGenerator,
helpers = require("./helpers"),
packageData;
try{
packageData = require("../package.json");
}catch(E){
// probably node 0.4 which doesn't support loading json files as objects
packageData = JSON.parse(
require("fs").
readFileSync(
require("path").
join(
__dirname,
"..",
"package.json"
)
)
);
}
/*
* Version constants
*/
var X_MAILER_NAME = "Nodemailer",
X_MAILER_HOMEPAGE = "http://andris9.github.com/Nodemailer/";
module.exports.X_MAILER_NAME = X_MAILER_NAME;
module.exports.X_MAILER_HOMEPAGE = X_MAILER_HOMEPAGE;
// Export createTransport method
module.exports.createTransport = function(type, options){
var transport = new Transport(type, options);
transport.sendMail = function(options, callback){
options = options || {};
options.transport = options.transport || transport;
sendMail(options, callback);
};
return transport;
};
//Export createXOAuthGenerator function
module.exports.createXOAuthGenerator = function(options){
return new XOAuthGenerator(options);
};
// Export Transport constructor
module.exports.Transport = Transport;
// Export Nodemailer constructor
module.exports.Nodemailer = Nodemailer;
// Export sendMail function (and the alias send_mail for legacy)
module.exports.sendMail = module.exports.send_mail = sendMail;
function sendMail(options, callback){
var mailer = new Nodemailer(options);
mailer.validateSettings(function(err){
if(err){
// report validation error back to the client
return callback(err);
}else{
// try to send the e-mail message
mailer.sendMail(callback);
}
});
return mailer;
}
/**
* <p>Generates a Nodemailer object which is the main 'hub' for managing the
* send process</p>
*
* @constructor
* @param {Object} options Message options object, see README for the complete list of possible options
*/
function Nodemailer(options){
this.options = options || {};
this.transport = this.options.transport;
if( this.options.encoding ){
this.mailcomposer = new MailComposer({encoding: this.options.encoding});
} else {
this.mailcomposer = new MailComposer();
}
if(!this.transport){
this.transport = this.getGlobalTransport();
}
}
/**
* <p>Generates an user agent string for Nodemailer with homepage, version etc.</p>
*
* @return {String} user agent string for X-Mailer value
*/
Nodemailer.prototype.generateUserAgentString = function(){
var details = [];
if(packageData.version){
details.push(packageData.version);
}
if(X_MAILER_HOMEPAGE){
details.push("+"+X_MAILER_HOMEPAGE);
}
return X_MAILER_NAME+ (details.length?" ("+details.join("; ")+")":"");
};
/**
* <p>Add support for legacy transport settings by checking for global
* variables SMTP, sendmail and SES</p>
*
* @return {Object} {@link Transport} object
*/
Nodemailer.prototype.getGlobalTransport = function(){
if(this.options.SMTP){
// cache the transport for SMTP as it is actually a connection pool
if(!this.options.SMTP._smtp_transport){
this.options.SMTP._smtp_transport = new Transport("SMTP", this.options.SMTP);
}
return this.options.SMTP._smtp_transport;
}else if(this.options.sendmail){
return new Transport("sendmail", this.options.sendmail);
}else if(this.options.SES){
return new Transport("SES", this.options.SES);
}else if(module.exports.SMTP){
// cache the transport for SMTP as it is actually a connection pool
if(!module.exports._smtp_transport){
module.exports._smtp_transport = new Transport("SMTP", module.exports.SMTP);
}
return module.exports._smtp_transport;
}else if(module.exports.sendmail){
return new Transport("sendmail", module.exports.sendmail);
}else if(module.exports.SES){
return new Transport("SES", module.exports.SES);
}
return false;
};
/**
* <p>Doesn't do much currently, if the future should link to transport
* validation methods. For example in case of SES should check that AWS
* keys are set up etc.</p>
*
* @param {Function} callback Callback function to run after validation
*/
Nodemailer.prototype.validateSettings = function(callback){
if(!this.transport || !this.transport.transport){
return callback(new Error("No transport method defined"));
}
callback(null);
};
/**
* <p>Send the e-mail message by using data from the original options object
* and selected transport</p>
*
* @param {Function} callback Callback function to run when the e-mail has been sent (or it failed)
*/
Nodemailer.prototype.sendMail = function(callback){
// compose the e-mail
this.generateMailObject();
// send the message using preselected transport method
this.transport.sendMailWithTransport(this.mailcomposer, callback);
};
/**
* <p>Uses the data from the original options object to compose a mailcomposer
* e-mail message that can be later streamed to the selected transport</p>
*/
Nodemailer.prototype.generateMailObject = function(){
// set envelope data, subject etc.
this.setGeneralOptions();
// set module defined headers (date, message-id, etc.)
this.setModuleHeaders();
// set user defined headers (if any)
this.setUserHeaders();
// set attachments (if any)
this.setAttachments();
};
/**
* <p>Uses the general options (message sender and receiver, subject body, etc.)
* to set mailcomposer properties. Includes support for legacy properties.</p>
*/
Nodemailer.prototype.setGeneralOptions = function(){
// generate plaintext if only HTML exists and generateTextFromHTML is true
if(!(this.options.text || this.options.body) && (this.options.html) &&
this.options.generateTextFromHTML){
this.options.text = helpers.stripHTML(this.options.html);
}
var acceptedFields = ["from", "sender", "to", "subject", "replyTo", "debug",
"reply_to", "cc", "bcc", "body", "text", "html",
"envelope", "inReplyTo", "references"],
mailOptions = {},
keys = Object.keys(this.options),
key;
for(var i=0, len=keys.length; i<len; i++){
key = keys[i];
if(acceptedFields.indexOf(key) >=0 && this.options[key]){
mailOptions[key] = this.options[key];
}
}
if(this.options.debug){
console.log(mailOptions);
}
this.mailcomposer.setMessageOption(mailOptions);
};
/**
* <p>If the 'headers' property was set on the options, add the values to the
* header of the e-mail message</p>
*/
Nodemailer.prototype.setUserHeaders = function(){
if(typeof this.options.headers != "object"){
return;
}
var keys = Object.keys(this.options.headers),
key;
for(var i=0, len=keys.length; i<len; i++){
key = keys[i];
if(this.options.headers[key]){
this.mailcomposer.addHeader(key, this.options.headers[key]);
}
}
};
/**
* <p>Add some required headers to the message, such as Date: and Message-Id:</p>
*/
Nodemailer.prototype.setModuleHeaders = function(){
// Mailer name + version
this.mailcomposer.addHeader("X-Mailer", this.generateUserAgentString());
// Date
this.mailcomposer.addHeader("Date", new Date().toUTCString());
// Message ID
if(this.options.messageId){
this.mailcomposer.addHeader("Message-Id", "<"+this.options.messageId+">");
}else if(this.options.messageId !== false){
this.mailcomposer.addHeader("Message-Id", "<"+
Date.now()+Math.random().toString(16).substr(1)+"@"+
X_MAILER_NAME+">");
}
};
/**
* <p>If attachment array is set on the options object, add these attachments
* to the mailcomposer object</p>
*/
Nodemailer.prototype.setAttachments = function(){
if(!Array.isArray(this.options.attachments)){
return;
}
var attachment;
for(var i=0, len=this.options.attachments.length; i<len; i++){
attachment = this.options.attachments[i];
attachment.userAgent = attachment.userAgent || this.generateUserAgentString();
this.mailcomposer.addAttachment(attachment);
}
};