emailjs 1.0.11 → 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/.editorconfig ADDED
@@ -0,0 +1,10 @@
1
+ root = true
2
+
3
+ [*]
4
+ charset = utf-8
5
+ indent_style = tab
6
+ indent_size = 2
7
+ tab_width = 2
8
+ end_of_line = lf
9
+ insert_final_newline = true
10
+ trim_trailing_whitespace = true
package/.eslintrc.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "env": {
3
+ "es6": true,
4
+ "mocha": true,
5
+ "node": true
6
+ },
7
+ "plugins": [
8
+ "mocha"
9
+ ],
10
+ "extends": [
11
+ "eslint:recommended",
12
+ "plugin:prettier/recommended"
13
+ ],
14
+ "rules": {
15
+ "curly": [
16
+ "error",
17
+ "all"
18
+ ],
19
+ "linebreak-style": [
20
+ "error",
21
+ "unix"
22
+ ],
23
+ "no-case-declarations": "off",
24
+ "no-console": "off",
25
+ "no-unused-vars": [
26
+ "error",
27
+ {
28
+ "vars": "all",
29
+ "args": "none",
30
+ "ignoreRestSiblings": true
31
+ }
32
+ ],
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"
39
+ }
40
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [2.0.1] - 2018-02-11
8
+ ### Added
9
+ - a new changelog
package/email.js CHANGED
@@ -1,3 +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');
5
+ exports.error = require('./smtp/error');
package/package.json CHANGED
@@ -1,13 +1,14 @@
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": "1.0.11",
4
+ "version": "2.2.0",
5
5
  "author": "eleith",
6
6
  "contributors": [
7
7
  "izuzak",
8
8
  "Hiverness",
9
9
  "mscdex",
10
- "jimmybergman"
10
+ "jimmybergman",
11
+ "zackschuster"
11
12
  ],
12
13
  "repository": {
13
14
  "type": "git",
@@ -15,26 +16,30 @@
15
16
  },
16
17
  "dependencies": {
17
18
  "addressparser": "^0.3.2",
18
- "mimelib": "0.2.14",
19
- "moment": "2.11.2",
20
- "starttls": "1.0.1"
21
- },
22
- "optionalDependencies": {
23
- "bufferjs": "1.1.0"
19
+ "emailjs-mime-codec": "^2.0.7"
24
20
  },
25
21
  "devDependencies": {
26
- "mocha": "1.7.4",
27
- "chai": "1.1.0",
28
- "simplesmtp": "0.3.32",
29
- "mailparser": "0.4.1",
30
- "iconv": "2.2.1"
22
+ "chai": "^4.1.2",
23
+ "eslint": "^4.19.1",
24
+ "eslint-config-prettier": "^2.9.0",
25
+ "eslint-plugin-mocha": "^5.0.0",
26
+ "eslint-plugin-prettier": "^2.6.0",
27
+ "mailparser": "^2.2.0",
28
+ "mocha": "^5.2.0",
29
+ "prettier": "^1.13.5",
30
+ "smtp-server": "^3.4.6"
31
31
  },
32
32
  "engine": [
33
- "node >= 0.10"
33
+ "node >= 6"
34
34
  ],
35
35
  "main": "email.js",
36
36
  "scripts": {
37
- "test": "mocha -R spec -t 5000"
37
+ "test": "mocha -R spec"
38
38
  },
39
- "license" : "MIT"
39
+ "license": "MIT",
40
+ "prettier": {
41
+ "singleQuote": true,
42
+ "trailingComma": "es5",
43
+ "useTabs": true
44
+ }
40
45
  }
package/smtp/client.js CHANGED
@@ -1,209 +1,326 @@
1
- var smtp = require('./smtp');
2
- var smtpError = require('./error');
3
- var message = require('./message');
4
- var addressparser= require('addressparser');
5
-
6
- var Client = function(server)
7
- {
8
- this.smtp = new smtp.SMTP(server);
9
- //this.smtp.debug(1);
10
-
11
- this.queue = [];
12
- this.timer = null;
13
- this.sending = false;
14
- this.ready = false;
15
- };
16
-
17
- Client.prototype =
18
- {
19
- _poll: function()
20
- {
21
- var self = this;
22
-
23
- clearTimeout(self.timer);
24
-
25
- if(self.queue.length)
26
- {
27
- if(self.smtp.state() == smtp.state.NOTCONNECTED)
28
- self._connect(self.queue[0]);
29
-
30
- else if(self.smtp.state() == smtp.state.CONNECTED && !self.sending && self.ready)
31
- self._sendmail(self.queue.shift());
32
- }
33
- // wait around 1 seconds in case something does come in, otherwise close out SMTP connection if still open
34
- else if(self.smtp.state() == smtp.state.CONNECTED)
35
- self.timer = setTimeout(function() { self.smtp.quit(); }, 1000);
36
- },
37
-
38
- _connect: function(stack)
39
- {
40
- var self = this,
41
-
42
- connect = function(err)
43
- {
44
- if(!err)
45
- {
46
- var begin = function(err)
47
- {
48
- if(!err)
49
- {
50
- self.ready = true;
51
- self._poll();
52
- }
53
- else {
54
- stack.callback(err, stack.message);
55
-
56
- // clear out the queue so all callbacks can be called with the same error message
57
- self.queue.shift();
58
- self._poll();
59
- }
60
- };
61
-
62
- if(!self.smtp.authorized())
63
- self.smtp.login(begin);
64
-
65
- else
66
- self.smtp.ehlo_or_helo_if_needed(begin);
67
- }
68
- else {
69
- stack.callback(err, stack.message);
70
-
71
- // clear out the queue so all callbacks can be called with the same error message
72
- self.queue.shift();
73
- self._poll();
74
- }
75
- };
76
-
77
- self.ready = false;
78
- self.smtp.connect(connect);
79
- },
80
-
81
- send: function(msg, callback)
82
- {
83
- var self = this;
84
-
85
- if(!(msg instanceof message.Message)
86
- && msg.from
87
- && (msg.to || msg.cc || msg.bcc)
88
- && (msg.text !== undefined || this._containsInlinedHtml(msg.attachment)))
89
- msg = message.create(msg);
90
-
91
- if(msg instanceof message.Message)
92
- {
93
- msg.valid(function(valid, why)
94
- {
95
- if(valid)
96
- {
97
- var stack =
98
- {
99
- message: msg,
100
- to: addressparser(msg.header.to),
101
- from: addressparser(msg.header.from)[0].address,
102
- callback: callback || function() {}
103
- };
104
-
105
- if(msg.header.cc)
106
- stack.to = stack.to.concat(addressparser(msg.header.cc));
107
-
108
- if(msg.header.bcc)
109
- stack.to = stack.to.concat(addressparser(msg.header.bcc));
110
-
111
- if(msg.header['return-path'] && addressparser(msg.header['return-path']).length)
112
- stack.returnPath = addressparser(msg.header['return-path'])[0].address;
113
-
114
- self.queue.push(stack);
115
- self._poll();
116
- }
117
- else
118
- callback(new Error(why), msg);
119
- });
120
- }
121
- else
122
- callback(new Error("message is not a valid Message instance"), msg);
123
- },
124
-
125
- _containsInlinedHtml: function(attachment) {
126
- if (Array.isArray(attachment)) {
127
- return attachment.some((function(ctx) {
128
- return function(att) {
129
- return ctx._isAttachmentInlinedHtml(att);
130
- };
131
- })(this));
132
- } else {
133
- return this._isAttachmentInlinedHtml(attachment);
134
- }
135
- },
136
-
137
- _isAttachmentInlinedHtml: function(attachment) {
138
- return attachment &&
139
- (attachment.data || attachment.path) &&
140
- attachment.alternative === true;
141
- },
142
-
143
- _sendsmtp: function(stack, next)
144
- {
145
- var self = this;
146
- var check= function(err)
147
- {
148
- if(!err && next)
149
- {
150
- next.apply(self, [stack]);
151
- }
152
- else
153
- {
154
- // if we snag on SMTP commands, call done, passing the error
155
- // but first reset SMTP state so queue can continue polling
156
- self.smtp.rset(function() { self._senddone(err, stack); });
157
- }
158
- };
159
-
160
- return check;
161
- },
162
-
163
- _sendmail: function(stack)
164
- {
165
- var self = this;
166
- var from = stack.returnPath || stack.from;
167
- self.sending = true;
168
- self.smtp.mail(self._sendsmtp(stack, self._sendrcpt), '<' + from + '>');
169
- },
170
-
171
- _sendrcpt: function(stack)
172
- {
173
- var self = this, to = stack.to.shift().address;
174
- self.smtp.rcpt(self._sendsmtp(stack, stack.to.length ? self._sendrcpt : self._senddata), '<'+ to +'>');
175
- },
176
-
177
- _senddata: function(stack)
178
- {
179
- var self = this;
180
- self.smtp.data(self._sendsmtp(stack, self._sendmessage));
181
- },
182
-
183
- _sendmessage: function(stack)
184
- {
185
- var self = this, stream = stack.message.stream();
186
-
187
- stream.on('data', function(data) { self.smtp.message(data); });
188
- stream.on('end', function() { self.smtp.data_end(self._sendsmtp(stack, function() { self._senddone(null, stack) })); });
189
-
190
- // there is no way to cancel a message while in the DATA portion, so we have to close the socket to prevent
191
- // a bad email from going out
192
- stream.on('error', function(err) { self.smtp.close(); self._senddone(err, stack); });
193
- },
194
-
195
- _senddone: function(err, stack)
196
- {
197
- var self = this;
198
- self.sending = false;
199
- stack.callback(err, stack.message);
200
- self._poll();
201
- }
202
- };
1
+ const { SMTP, state } = require('./smtp');
2
+ const { Message, create } = require('./message');
3
+ const addressparser = require('addressparser');
4
+
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
+ */
33
+ constructor(server) {
34
+ this.smtp = new SMTP(server);
35
+ //this.smtp.debug(1);
36
+
37
+ /**
38
+ * @type {MessageStack[]}
39
+ */
40
+ this.queue = [];
41
+
42
+ /**
43
+ * @type {NodeJS.Timer}
44
+ */
45
+ this.timer = null;
46
+
47
+ /**
48
+ * @type {boolean}
49
+ */
50
+ this.sending = false;
51
+
52
+ /**
53
+ * @type {boolean}
54
+ */
55
+ this.ready = false;
56
+ }
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
+ */
120
+ _poll() {
121
+ clearTimeout(this.timer);
122
+
123
+ if (this.queue.length) {
124
+ if (this.smtp.state() == state.NOTCONNECTED) {
125
+ this._connect(this.queue[0]);
126
+ } else if (
127
+ this.smtp.state() == state.CONNECTED &&
128
+ !this.sending &&
129
+ this.ready
130
+ ) {
131
+ this._sendmail(this.queue.shift());
132
+ }
133
+ }
134
+ // wait around 1 seconds in case something does come in,
135
+ // otherwise close out SMTP connection if still open
136
+ else if (this.smtp.state() == state.CONNECTED) {
137
+ this.timer = setTimeout(() => this.smtp.quit(), 1000);
138
+ }
139
+ }
140
+
141
+ /**
142
+ * @private
143
+ * @param {MessageStack} stack stack
144
+ * @returns {void}
145
+ */
146
+ _connect(stack) {
147
+ /**
148
+ * @param {Error} err callback error
149
+ * @returns {void}
150
+ */
151
+ const connect = err => {
152
+ if (!err) {
153
+ const begin = err => {
154
+ if (!err) {
155
+ this.ready = true;
156
+ this._poll();
157
+ } else {
158
+ stack.callback(err, stack.message);
159
+
160
+ // clear out the queue so all callbacks can be called with the same error message
161
+ this.queue.shift();
162
+ this._poll();
163
+ }
164
+ };
165
+
166
+ if (!this.smtp.authorized()) {
167
+ this.smtp.login(begin);
168
+ } else {
169
+ this.smtp.ehlo_or_helo_if_needed(begin);
170
+ }
171
+ } else {
172
+ stack.callback(err, stack.message);
173
+
174
+ // clear out the queue so all callbacks can be called with the same error message
175
+ this.queue.shift();
176
+ this._poll();
177
+ }
178
+ };
179
+
180
+ this.ready = false;
181
+ this.smtp.connect(connect);
182
+ }
183
+
184
+ /**
185
+ * @private
186
+ * @param {MessageStack} msg message stack
187
+ * @returns {boolean} can make message
188
+ */
189
+ _canMakeMessage(msg) {
190
+ return (
191
+ msg.from &&
192
+ (msg.to || msg.cc || msg.bcc) &&
193
+ (msg.text !== undefined || this._containsInlinedHtml(msg.attachment))
194
+ );
195
+ }
196
+
197
+ /**
198
+ * @private
199
+ * @param {*} attachment attachment
200
+ * @returns {boolean} does contain
201
+ */
202
+ _containsInlinedHtml(attachment) {
203
+ if (Array.isArray(attachment)) {
204
+ return attachment.some(att => {
205
+ return this._isAttachmentInlinedHtml(att);
206
+ });
207
+ } else {
208
+ return this._isAttachmentInlinedHtml(attachment);
209
+ }
210
+ }
211
+
212
+ /**
213
+ * @private
214
+ * @param {*} attachment attachment
215
+ * @returns {boolean} is inlined
216
+ */
217
+ _isAttachmentInlinedHtml(attachment) {
218
+ return (
219
+ attachment &&
220
+ (attachment.data || attachment.path) &&
221
+ attachment.alternative === true
222
+ );
223
+ }
224
+
225
+ /**
226
+ * @private
227
+ * @param {MessageStack} stack stack
228
+ * @param {function(MessageStack): void} next next
229
+ * @returns {function(Error): void} callback
230
+ */
231
+ _sendsmtp(stack, next) {
232
+ /**
233
+ * @param {Error} [err] error
234
+ * @returns {void}
235
+ */
236
+ return err => {
237
+ if (!err && next) {
238
+ next.apply(this, [stack]);
239
+ } else {
240
+ // if we snag on SMTP commands, call done, passing the error
241
+ // but first reset SMTP state so queue can continue polling
242
+ this.smtp.rset(() => this._senddone(err, stack));
243
+ }
244
+ };
245
+ }
246
+
247
+ /**
248
+ * @private
249
+ * @param {MessageStack} stack stack
250
+ * @returns {void}
251
+ */
252
+ _sendmail(stack) {
253
+ const from = stack.returnPath || stack.from;
254
+ this.sending = true;
255
+ this.smtp.mail(this._sendsmtp(stack, this._sendrcpt), '<' + from + '>');
256
+ }
257
+
258
+ /**
259
+ * @private
260
+ * @param {MessageStack} stack stack
261
+ * @returns {void}
262
+ */
263
+ _sendrcpt(stack) {
264
+ if (stack.to == null || typeof stack.to === 'string') {
265
+ throw new TypeError('stack.to must be array');
266
+ }
267
+
268
+ const to = stack.to.shift().address;
269
+ this.smtp.rcpt(
270
+ this._sendsmtp(stack, stack.to.length ? this._sendrcpt : this._senddata),
271
+ `<${to}>`
272
+ );
273
+ }
274
+
275
+ /**
276
+ * @private
277
+ * @param {MessageStack} stack stack
278
+ * @returns {void}
279
+ */
280
+ _senddata(stack) {
281
+ this.smtp.data(this._sendsmtp(stack, this._sendmessage));
282
+ }
283
+
284
+ /**
285
+ * @private
286
+ * @param {MessageStack} stack stack
287
+ * @returns {void}
288
+ */
289
+ _sendmessage(stack) {
290
+ const stream = stack.message.stream();
291
+
292
+ stream.on('data', data => this.smtp.message(data));
293
+ stream.on('end', () => {
294
+ this.smtp.data_end(
295
+ this._sendsmtp(stack, () => this._senddone(null, stack))
296
+ );
297
+ });
298
+
299
+ // there is no way to cancel a message while in the DATA portion,
300
+ // so we have to close the socket to prevent a bad email from going out
301
+ stream.on('error', err => {
302
+ this.smtp.close();
303
+ this._senddone(err, stack);
304
+ });
305
+ }
306
+
307
+ /**
308
+ * @private
309
+ * @param {Error} err err
310
+ * @param {MessageStack} stack stack
311
+ * @returns {void}
312
+ */
313
+ _senddone(err, stack) {
314
+ this.sending = false;
315
+ stack.callback(err, stack.message);
316
+ this._poll();
317
+ }
318
+ }
203
319
 
204
320
  exports.Client = Client;
205
321
 
206
- exports.connect = function(server)
207
- {
208
- return new Client(server);
209
- };
322
+ /**
323
+ * @param {SMTPOptions} server smtp options
324
+ * @returns {Client} the client
325
+ */
326
+ exports.connect = server => new Client(server);