emailjs 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.json CHANGED
@@ -30,10 +30,11 @@
30
30
  "ignoreRestSiblings": true
31
31
  }
32
32
  ],
33
- "mocha/handle-done-callback": "error",
34
- "mocha/no-exclusive-tests": "error",
35
- "mocha/no-global-tests": "error",
36
- "mocha/no-mocha-arrows": "error",
37
- "mocha/no-skipped-tests": "error"
33
+ "valid-jsdoc": "error",
34
+ "mocha/handle-done-callback": "error",
35
+ "mocha/no-exclusive-tests": "error",
36
+ "mocha/no-global-tests": "error",
37
+ "mocha/no-mocha-arrows": "error",
38
+ "mocha/no-skipped-tests": "error"
38
39
  }
39
40
  }
package/email.js CHANGED
@@ -1,4 +1,5 @@
1
1
  exports.server = require('./smtp/client');
2
2
  exports.message = require('./smtp/message');
3
+ exports.date = require('./smtp/date');
3
4
  exports.SMTP = require('./smtp/smtp');
4
5
  exports.error = require('./smtp/error');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "emailjs",
3
3
  "description": "send text/html emails and attachments (files, streams and strings) from node.js to any smtp server",
4
- "version": "2.1.0",
4
+ "version": "2.2.0",
5
5
  "author": "eleith",
6
6
  "contributors": [
7
7
  "izuzak",
@@ -16,20 +16,18 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "addressparser": "^0.3.2",
19
- "emailjs-mime-codec": "^2.0.5",
20
- "moment": "2.20.1",
21
- "starttls": "1.0.1"
19
+ "emailjs-mime-codec": "^2.0.7"
22
20
  },
23
21
  "devDependencies": {
24
- "chai": "1.1.0",
22
+ "chai": "^4.1.2",
25
23
  "eslint": "^4.19.1",
26
24
  "eslint-config-prettier": "^2.9.0",
27
25
  "eslint-plugin-mocha": "^5.0.0",
28
26
  "eslint-plugin-prettier": "^2.6.0",
29
- "mailparser": "2.2.0",
30
- "mocha": "5.0.0",
31
- "prettier": "^1.12.1",
32
- "smtp-server": "^3.4.1"
27
+ "mailparser": "^2.2.0",
28
+ "mocha": "^5.2.0",
29
+ "prettier": "^1.13.5",
30
+ "smtp-server": "^3.4.6"
33
31
  },
34
32
  "engine": [
35
33
  "node >= 6"
package/smtp/client.js CHANGED
@@ -1,26 +1,130 @@
1
- const smtp = require('./smtp');
2
- const message = require('./message');
1
+ const { SMTP, state } = require('./smtp');
2
+ const { Message, create } = require('./message');
3
3
  const addressparser = require('addressparser');
4
4
 
5
5
  class Client {
6
+ /**
7
+ * @typedef {Object} SMTPOptions
8
+ * @property {number} [timeout]
9
+ * @property {string} [user]
10
+ * @property {string} [password]
11
+ * @property {string} [domain]
12
+ * @property {string} [host]
13
+ * @property {number} [port]
14
+ * @property {boolean} [ssl]
15
+ * @property {boolean} [tls]
16
+ * @property {string[]} [authentication]
17
+ *
18
+ * @typedef {Object} MessageStack
19
+ * @property {function(Error, Message): void} [callback]
20
+ * @property {Message} [message]
21
+ * @property {string} [returnPath]
22
+ * @property {string} [from]
23
+ * @property {string} [subject]
24
+ * @property {string|Array} [to]
25
+ * @property {Array} [cc]
26
+ * @property {Array} [bcc]
27
+ * @property {string} [text]
28
+ * @property {*} [attachment]
29
+ *
30
+ * @constructor
31
+ * @param {SMTPOptions} server smtp options
32
+ */
6
33
  constructor(server) {
7
- this.smtp = new smtp.SMTP(server);
34
+ this.smtp = new SMTP(server);
8
35
  //this.smtp.debug(1);
9
36
 
37
+ /**
38
+ * @type {MessageStack[]}
39
+ */
10
40
  this.queue = [];
41
+
42
+ /**
43
+ * @type {NodeJS.Timer}
44
+ */
11
45
  this.timer = null;
46
+
47
+ /**
48
+ * @type {boolean}
49
+ */
12
50
  this.sending = false;
51
+
52
+ /**
53
+ * @type {boolean}
54
+ */
13
55
  this.ready = false;
14
56
  }
15
57
 
58
+ /**
59
+ * @param {Message|MessageStack} msg msg
60
+ * @param {function(Error, MessageStack): void} callback callback
61
+ * @returns {void}
62
+ */
63
+ send(msg, callback) {
64
+ /**
65
+ * @type {Message}
66
+ */
67
+ const message =
68
+ msg instanceof Message
69
+ ? msg
70
+ : this._canMakeMessage(msg)
71
+ ? create(msg)
72
+ : null;
73
+
74
+ if (message == null) {
75
+ callback(
76
+ new Error('message is not a valid Message instance'),
77
+ /** @type {MessageStack} */ (msg)
78
+ );
79
+ return;
80
+ }
81
+
82
+ message.valid((valid, why) => {
83
+ if (valid) {
84
+ const stack = {
85
+ message,
86
+ to: addressparser(message.header.to),
87
+ from: addressparser(message.header.from)[0].address,
88
+ callback: (callback || function() {}).bind(this),
89
+ };
90
+
91
+ if (message.header.cc) {
92
+ stack.to = stack.to.concat(addressparser(message.header.cc));
93
+ }
94
+
95
+ if (message.header.bcc) {
96
+ stack.to = stack.to.concat(addressparser(message.header.bcc));
97
+ }
98
+
99
+ if (
100
+ message.header['return-path'] &&
101
+ addressparser(message.header['return-path']).length
102
+ ) {
103
+ stack.returnPath = addressparser(
104
+ message.header['return-path']
105
+ )[0].address;
106
+ }
107
+
108
+ this.queue.push(stack);
109
+ this._poll();
110
+ } else {
111
+ callback(new Error(why), /** @type {MessageStack} */ (msg));
112
+ }
113
+ });
114
+ }
115
+
116
+ /**
117
+ * @private
118
+ * @returns {void}
119
+ */
16
120
  _poll() {
17
121
  clearTimeout(this.timer);
18
122
 
19
123
  if (this.queue.length) {
20
- if (this.smtp.state() == smtp.state.NOTCONNECTED) {
124
+ if (this.smtp.state() == state.NOTCONNECTED) {
21
125
  this._connect(this.queue[0]);
22
126
  } else if (
23
- this.smtp.state() == smtp.state.CONNECTED &&
127
+ this.smtp.state() == state.CONNECTED &&
24
128
  !this.sending &&
25
129
  this.ready
26
130
  ) {
@@ -29,12 +133,21 @@ class Client {
29
133
  }
30
134
  // wait around 1 seconds in case something does come in,
31
135
  // otherwise close out SMTP connection if still open
32
- else if (this.smtp.state() == smtp.state.CONNECTED) {
136
+ else if (this.smtp.state() == state.CONNECTED) {
33
137
  this.timer = setTimeout(() => this.smtp.quit(), 1000);
34
138
  }
35
139
  }
36
140
 
141
+ /**
142
+ * @private
143
+ * @param {MessageStack} stack stack
144
+ * @returns {void}
145
+ */
37
146
  _connect(stack) {
147
+ /**
148
+ * @param {Error} err callback error
149
+ * @returns {void}
150
+ */
38
151
  const connect = err => {
39
152
  if (!err) {
40
153
  const begin = err => {
@@ -68,66 +181,39 @@ class Client {
68
181
  this.smtp.connect(connect);
69
182
  }
70
183
 
71
- send(msg, callback) {
72
- if (
73
- !(msg instanceof message.Message) &&
184
+ /**
185
+ * @private
186
+ * @param {MessageStack} msg message stack
187
+ * @returns {boolean} can make message
188
+ */
189
+ _canMakeMessage(msg) {
190
+ return (
74
191
  msg.from &&
75
192
  (msg.to || msg.cc || msg.bcc) &&
76
193
  (msg.text !== undefined || this._containsInlinedHtml(msg.attachment))
77
- ) {
78
- msg = message.create(msg);
79
- }
80
-
81
- if (msg instanceof message.Message) {
82
- msg.valid((valid, why) => {
83
- if (valid) {
84
- const stack = {
85
- message: msg,
86
- to: addressparser(msg.header.to),
87
- from: addressparser(msg.header.from)[0].address,
88
- callback: (callback || function() {}).bind(this),
89
- };
90
-
91
- if (msg.header.cc) {
92
- stack.to = stack.to.concat(addressparser(msg.header.cc));
93
- }
94
-
95
- if (msg.header.bcc) {
96
- stack.to = stack.to.concat(addressparser(msg.header.bcc));
97
- }
98
-
99
- if (
100
- msg.header['return-path'] &&
101
- addressparser(msg.header['return-path']).length
102
- ) {
103
- stack.returnPath = addressparser(
104
- msg.header['return-path']
105
- )[0].address;
106
- }
107
-
108
- this.queue.push(stack);
109
- this._poll();
110
- } else {
111
- callback(new Error(why), msg);
112
- }
113
- });
114
- } else {
115
- callback(new Error('message is not a valid Message instance'), msg);
116
- }
194
+ );
117
195
  }
118
196
 
197
+ /**
198
+ * @private
199
+ * @param {*} attachment attachment
200
+ * @returns {boolean} does contain
201
+ */
119
202
  _containsInlinedHtml(attachment) {
120
203
  if (Array.isArray(attachment)) {
121
- return attachment.some(() => {
122
- return att => {
123
- return this._isAttachmentInlinedHtml(att);
124
- };
204
+ return attachment.some(att => {
205
+ return this._isAttachmentInlinedHtml(att);
125
206
  });
126
207
  } else {
127
208
  return this._isAttachmentInlinedHtml(attachment);
128
209
  }
129
210
  }
130
211
 
212
+ /**
213
+ * @private
214
+ * @param {*} attachment attachment
215
+ * @returns {boolean} is inlined
216
+ */
131
217
  _isAttachmentInlinedHtml(attachment) {
132
218
  return (
133
219
  attachment &&
@@ -136,7 +222,17 @@ class Client {
136
222
  );
137
223
  }
138
224
 
225
+ /**
226
+ * @private
227
+ * @param {MessageStack} stack stack
228
+ * @param {function(MessageStack): void} next next
229
+ * @returns {function(Error): void} callback
230
+ */
139
231
  _sendsmtp(stack, next) {
232
+ /**
233
+ * @param {Error} [err] error
234
+ * @returns {void}
235
+ */
140
236
  return err => {
141
237
  if (!err && next) {
142
238
  next.apply(this, [stack]);
@@ -148,24 +244,48 @@ class Client {
148
244
  };
149
245
  }
150
246
 
247
+ /**
248
+ * @private
249
+ * @param {MessageStack} stack stack
250
+ * @returns {void}
251
+ */
151
252
  _sendmail(stack) {
152
253
  const from = stack.returnPath || stack.from;
153
254
  this.sending = true;
154
255
  this.smtp.mail(this._sendsmtp(stack, this._sendrcpt), '<' + from + '>');
155
256
  }
156
257
 
258
+ /**
259
+ * @private
260
+ * @param {MessageStack} stack stack
261
+ * @returns {void}
262
+ */
157
263
  _sendrcpt(stack) {
264
+ if (stack.to == null || typeof stack.to === 'string') {
265
+ throw new TypeError('stack.to must be array');
266
+ }
267
+
158
268
  const to = stack.to.shift().address;
159
269
  this.smtp.rcpt(
160
270
  this._sendsmtp(stack, stack.to.length ? this._sendrcpt : this._senddata),
161
- '<' + to + '>'
271
+ `<${to}>`
162
272
  );
163
273
  }
164
274
 
275
+ /**
276
+ * @private
277
+ * @param {MessageStack} stack stack
278
+ * @returns {void}
279
+ */
165
280
  _senddata(stack) {
166
281
  this.smtp.data(this._sendsmtp(stack, this._sendmessage));
167
282
  }
168
283
 
284
+ /**
285
+ * @private
286
+ * @param {MessageStack} stack stack
287
+ * @returns {void}
288
+ */
169
289
  _sendmessage(stack) {
170
290
  const stream = stack.message.stream();
171
291
 
@@ -184,6 +304,12 @@ class Client {
184
304
  });
185
305
  }
186
306
 
307
+ /**
308
+ * @private
309
+ * @param {Error} err err
310
+ * @param {MessageStack} stack stack
311
+ * @returns {void}
312
+ */
187
313
  _senddone(err, stack) {
188
314
  this.sending = false;
189
315
  stack.callback(err, stack.message);
@@ -192,4 +318,9 @@ class Client {
192
318
  }
193
319
 
194
320
  exports.Client = Client;
321
+
322
+ /**
323
+ * @param {SMTPOptions} server smtp options
324
+ * @returns {Client} the client
325
+ */
195
326
  exports.connect = server => new Client(server);
package/smtp/date.js ADDED
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @param {Date} [date] an optional date to convert to RFC2822 format
3
+ * @param {boolean} [useUtc=false] whether to parse the date as UTC (default: false)
4
+ * @returns {string} the converted date
5
+ */
6
+ function getRFC2822Date(date = new Date(), useUtc = false) {
7
+ if (useUtc) {
8
+ return getRFC2822DateUTC(date);
9
+ }
10
+
11
+ const dates = date
12
+ .toString()
13
+ .replace('GMT', '')
14
+ .replace(/\s\(.*\)$/, '')
15
+ .split(' ');
16
+
17
+ dates[0] = dates[0] + ',';
18
+
19
+ const day = dates[1];
20
+ dates[1] = dates[2];
21
+ dates[2] = day;
22
+
23
+ return dates.join(' ');
24
+ }
25
+
26
+ /**
27
+ * @param {Date} [date] an optional date to convert to RFC2822 format (UTC)
28
+ * @returns {string} the converted date
29
+ */
30
+ function getRFC2822DateUTC(date = new Date()) {
31
+ const dates = date.toUTCString().split(' ');
32
+ dates.pop(); // remove timezone
33
+ dates.push('+0000');
34
+ return dates.join(' ');
35
+ }
36
+
37
+ exports.getRFC2822Date = getRFC2822Date;
38
+ exports.getRFC2822DateUTC = getRFC2822DateUTC;
package/smtp/error.js CHANGED
@@ -1,6 +1,37 @@
1
+ class SMTPError extends Error {
2
+ /**
3
+ * @param {string} message the error message
4
+ */
5
+ constructor(message) {
6
+ super(message);
7
+
8
+ /**
9
+ * @type {number}
10
+ */
11
+ this.code = null;
12
+
13
+ /**
14
+ * @type {*}
15
+ */
16
+ this.smtp = null;
17
+
18
+ /**
19
+ * @type {Error}
20
+ */
21
+ this.previous = null;
22
+ }
23
+ }
24
+
25
+ /**
26
+ * @param {string} message the error message
27
+ * @param {number} code the error code
28
+ * @param {Error} [error] an error object
29
+ * @param {*} [smtp] smtp
30
+ * @returns {SMTPError} an smtp error object
31
+ */
1
32
  module.exports = function(message, code, error, smtp) {
2
- const err = new Error(
3
- error && error.message ? `${message} (${error.message})` : message
33
+ const err = new SMTPError(
34
+ error != null && error.message ? `${message} (${error.message})` : message
4
35
  );
5
36
 
6
37
  err.code = code;
@@ -13,13 +44,43 @@ module.exports = function(message, code, error, smtp) {
13
44
  return err;
14
45
  };
15
46
 
47
+ /**
48
+ * @type {1}
49
+ */
16
50
  module.exports.COULDNOTCONNECT = 1;
51
+ /**
52
+ * @type {2}
53
+ */
17
54
  module.exports.BADRESPONSE = 2;
55
+ /**
56
+ * @type {3}
57
+ */
18
58
  module.exports.AUTHFAILED = 3;
59
+ /**
60
+ * @type {4}
61
+ */
19
62
  module.exports.TIMEDOUT = 4;
63
+ /**
64
+ * @type {5}
65
+ */
20
66
  module.exports.ERROR = 5;
67
+ /**
68
+ * @type {6}
69
+ */
21
70
  module.exports.NOCONNECTION = 6;
71
+ /**
72
+ * @type {7}
73
+ */
22
74
  module.exports.AUTHNOTSUPPORTED = 7;
75
+ /**
76
+ * @type {8}
77
+ */
23
78
  module.exports.CONNECTIONCLOSED = 8;
79
+ /**
80
+ * @type {9}
81
+ */
24
82
  module.exports.CONNECTIONENDED = 9;
83
+ /**
84
+ * @type {10}
85
+ */
25
86
  module.exports.CONNECTIONAUTH = 10;