wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { addressparser } from './address';
import { Message } from './message';
import type { MessageAttachment, MessageHeaders } from './message';
import { SMTPConnection, SMTPState } from './connection';
import type { SMTPConnectionOptions } from './connection';
 
export interface MessageStack {
    callback: (error: Error | null, message: Message) => void;
    message: Message;
    attachment: MessageAttachment;
    text: string;
    returnPath: string;
    from: string;
    to: ReturnType<typeof addressparser>;
    cc: string[];
    bcc: string[];
}
 
export class SMTPClient {
    public readonly smtp: SMTPConnection;
    public readonly queue: MessageStack[] = [];
 
    protected sending = false;
    protected ready = false;
    protected timer: NodeJS.Timer | null = null;
 
    /**
     * Create a standard SMTP client backed by a self-managed SMTP connection.
     *
     * NOTE: `host` is trimmed before being used to establish a connection; however, the original untrimmed value will still be visible in configuration.
     *
     * @param {SMTPConnectionOptions} server smtp options
     */
    constructor(server: Partial<SMTPConnectionOptions>) {
        this.smtp = new SMTPConnection(server);
    }
 
    /**
     * @public
     * @param {Message} msg the message to send
     * @param {function(err: Error, msg: Message): void} callback .
     * @returns {void}
     */
    public send(
        msg: Message,
        callback: (err: Error | null, msg: Message) => void
    ) {
        const message: Message | null =
            msg instanceof Message
                ? msg
                : this._canMakeMessage(msg)
                ? new Message(msg)
                : null;
 
        if (message == null) {
            callback(new Error('message is not a valid Message instance'), msg);
            return;
        }
 
        message.valid((valid, why) => {
            if (valid) {
                const stack = this.createMessageStack(message, callback);
                if (stack.to.length === 0) {
                    return callback(new Error('No recipients found in message'), msg);
                }
                this.queue.push(stack);
                this._poll();
            } else {
                callback(new Error(why), msg);
            }
        });
    }
 
    /**
     * @public
     * @description Converts a message to the raw object used by the internal stack.
     * @param {Message} message message to convert
     * @param {function(err: Error, msg: Message): void} callback errback
     * @returns {MessageStack} raw message object
     */
    public createMessageStack(
        message: Message,
        callback: (err: Error | null, msg: Message) => void = function () {
            /* ø */
        }
    ) {
        const [{ address: from }] = addressparser(message.header.from);
        const stack = {
            message,
            to: [] as ReturnType<typeof addressparser>,
            from,
            callback: callback.bind(this),
        } as MessageStack;
 
        const {
            header: { to, cc, bcc, 'return-path': returnPath },
        } = message;
 
        if ((typeof to === 'string' || Array.isArray(to)) && to.length > 0) {
            stack.to = addressparser(to);
        }
 
        if ((typeof cc === 'string' || Array.isArray(cc)) && cc.length > 0) {
            stack.to = stack.to.concat(
                addressparser(cc).filter(
                    (x) => stack.to.some((y) => y.address === x.address) === false
                )
            );
        }
 
        if ((typeof bcc === 'string' || Array.isArray(bcc)) && bcc.length > 0) {
            stack.to = stack.to.concat(
                addressparser(bcc).filter(
                    (x) => stack.to.some((y) => y.address === x.address) === false
                )
            );
        }
 
        if (typeof returnPath === 'string' && returnPath.length > 0) {
            const parsedReturnPath = addressparser(returnPath);
            if (parsedReturnPath.length > 0) {
                const [{ address: returnPathAddress }] = parsedReturnPath;
                stack.returnPath = returnPathAddress as string;
            }
        }
 
        return stack;
    }
 
    /**
     * @protected
     * @returns {void}
     */
    protected _poll() {
        if (this.timer != null) {
            clearTimeout(this.timer);
        }
 
        if (this.queue.length) {
            if (this.smtp.state() == SMTPState.NOTCONNECTED) {
                this._connect(this.queue[0]);
            } else if (
                this.smtp.state() == SMTPState.CONNECTED &&
                !this.sending &&
                this.ready
            ) {
                this._sendmail(this.queue.shift() as MessageStack);
            }
        }
        // wait around 1 seconds in case something does come in,
        // otherwise close out SMTP connection if still open
        else if (this.smtp.state() == SMTPState.CONNECTED) {
            this.timer = setTimeout(() => this.smtp.quit(), 1000);
        }
    }
 
    /**
     * @protected
     * @param {MessageStack} stack stack
     * @returns {void}
     */
    protected _connect(stack: MessageStack) {
        /**
         * @param {Error} err callback error
         * @returns {void}
         */
        const connect = (err: Error) => {
            if (!err) {
                const begin = (err: Error) => {
                    if (!err) {
                        this.ready = true;
                        this._poll();
                    } else {
                        stack.callback(err, stack.message);
 
                        // clear out the queue so all callbacks can be called with the same error message
                        this.queue.shift();
                        this._poll();
                    }
                };
 
                if (!this.smtp.authorized()) {
                    this.smtp.login(begin);
                } else {
                    this.smtp.ehlo_or_helo_if_needed(begin);
                }
            } else {
                stack.callback(err, stack.message);
 
                // clear out the queue so all callbacks can be called with the same error message
                this.queue.shift();
                this._poll();
            }
        };
 
        this.ready = false;
        this.smtp.connect(connect);
    }
 
    /**
     * @protected
     * @param {MessageStack} msg message stack
     * @returns {boolean} can make message
     */
    protected _canMakeMessage(msg: MessageHeaders) {
        return (
            msg.from &&
            (msg.to || msg.cc || msg.bcc) &&
            (msg.text !== undefined || this._containsInlinedHtml(msg.attachment))
        );
    }
 
    /**
     * @protected
     * @param {*} attachment attachment
     * @returns {*} whether the attachment contains inlined html
     */
    protected _containsInlinedHtml(
        attachment: MessageAttachment | MessageAttachment[]
    ) {
        if (Array.isArray(attachment)) {
            return attachment.some((att) => {
                return this._isAttachmentInlinedHtml(att);
            });
        } else {
            return this._isAttachmentInlinedHtml(attachment);
        }
    }
 
    /**
     * @protected
     * @param {MessageAttachment} attachment attachment
     * @returns {boolean} whether the attachment is inlined html
     */
    protected _isAttachmentInlinedHtml(attachment: MessageAttachment) {
        return (
            attachment &&
            (attachment.data || attachment.path) &&
            attachment.alternative === true
        );
    }
 
    /**
     * @protected
     * @param {MessageStack} stack stack
     * @param {function(MessageStack): void} next next
     * @returns {function(Error): void} callback
     */
    protected _sendsmtp(stack: MessageStack, next: (msg: MessageStack) => void) {
        /**
         * @param {Error} [err] error
         * @returns {void}
         */
        return (err: Error) => {
            if (!err && next) {
                next.apply(this, [stack]);
            } else {
                // if we snag on SMTP commands, call done, passing the error
                // but first reset SMTP state so queue can continue polling
                this.smtp.rset(() => this._senddone(err, stack));
            }
        };
    }
 
    /**
     * @protected
     * @param {MessageStack} stack stack
     * @returns {void}
     */
    protected _sendmail(stack: MessageStack) {
        const from = stack.returnPath || stack.from;
        this.sending = true;
        this.smtp.mail(this._sendsmtp(stack, this._sendrcpt), '<' + from + '>');
    }
 
    /**
     * @protected
     * @param {MessageStack} stack stack
     * @returns {void}
     */
    protected _sendrcpt(stack: MessageStack) {
        if (stack.to == null || typeof stack.to === 'string') {
            throw new TypeError('stack.to must be array');
        }
 
        const to = stack.to.shift()?.address;
        this.smtp.rcpt(
            this._sendsmtp(stack, stack.to.length ? this._sendrcpt : this._senddata),
            `<${to}>`
        );
    }
 
    /**
     * @protected
     * @param {MessageStack} stack stack
     * @returns {void}
     */
    protected _senddata(stack: MessageStack) {
        this.smtp.data(this._sendsmtp(stack, this._sendmessage));
    }
 
    /**
     * @protected
     * @param {MessageStack} stack stack
     * @returns {void}
     */
    protected _sendmessage(stack: MessageStack) {
        const stream = stack.message.stream();
 
        stream.on('data', (data) => this.smtp.message(data));
        stream.on('end', () => {
            this.smtp.data_end(
                this._sendsmtp(stack, () => this._senddone(null, stack))
            );
        });
 
        // there is no way to cancel a message while in the DATA portion,
        // so we have to close the socket to prevent a bad email from going out
        stream.on('error', (err) => {
            this.smtp.close();
            this._senddone(err, stack);
        });
    }
 
    /**
     * @protected
     * @param {Error} err err
     * @param {MessageStack} stack stack
     * @returns {void}
     */
    protected _senddone(err: Error | null, stack: MessageStack) {
        this.sending = false;
        stack.callback(err, stack.message);
        this._poll();
    }
}