@depup/nodemailer 7.0.12-depup.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.
Files changed (47) hide show
  1. package/.gitattributes +6 -0
  2. package/.ncurc.js +9 -0
  3. package/.prettierignore +8 -0
  4. package/.prettierrc +12 -0
  5. package/.prettierrc.js +10 -0
  6. package/.release-please-config.json +9 -0
  7. package/CHANGELOG.md +929 -0
  8. package/CODE_OF_CONDUCT.md +76 -0
  9. package/LICENSE +16 -0
  10. package/README.md +86 -0
  11. package/SECURITY.txt +22 -0
  12. package/eslint.config.js +88 -0
  13. package/lib/addressparser/index.js +383 -0
  14. package/lib/base64/index.js +139 -0
  15. package/lib/dkim/index.js +253 -0
  16. package/lib/dkim/message-parser.js +155 -0
  17. package/lib/dkim/relaxed-body.js +154 -0
  18. package/lib/dkim/sign.js +117 -0
  19. package/lib/fetch/cookies.js +281 -0
  20. package/lib/fetch/index.js +280 -0
  21. package/lib/json-transport/index.js +82 -0
  22. package/lib/mail-composer/index.js +629 -0
  23. package/lib/mailer/index.js +441 -0
  24. package/lib/mailer/mail-message.js +316 -0
  25. package/lib/mime-funcs/index.js +625 -0
  26. package/lib/mime-funcs/mime-types.js +2113 -0
  27. package/lib/mime-node/index.js +1316 -0
  28. package/lib/mime-node/last-newline.js +33 -0
  29. package/lib/mime-node/le-unix.js +43 -0
  30. package/lib/mime-node/le-windows.js +52 -0
  31. package/lib/nodemailer.js +157 -0
  32. package/lib/punycode/index.js +460 -0
  33. package/lib/qp/index.js +227 -0
  34. package/lib/sendmail-transport/index.js +210 -0
  35. package/lib/ses-transport/index.js +234 -0
  36. package/lib/shared/index.js +754 -0
  37. package/lib/smtp-connection/data-stream.js +108 -0
  38. package/lib/smtp-connection/http-proxy-client.js +143 -0
  39. package/lib/smtp-connection/index.js +1865 -0
  40. package/lib/smtp-pool/index.js +652 -0
  41. package/lib/smtp-pool/pool-resource.js +259 -0
  42. package/lib/smtp-transport/index.js +421 -0
  43. package/lib/stream-transport/index.js +135 -0
  44. package/lib/well-known/index.js +47 -0
  45. package/lib/well-known/services.json +611 -0
  46. package/lib/xoauth2/index.js +427 -0
  47. package/package.json +47 -0
@@ -0,0 +1,82 @@
1
+ 'use strict';
2
+
3
+ const packageData = require('../../package.json');
4
+ const shared = require('../shared');
5
+
6
+ /**
7
+ * Generates a Transport object to generate JSON output
8
+ *
9
+ * @constructor
10
+ * @param {Object} optional config parameter
11
+ */
12
+ class JSONTransport {
13
+ constructor(options) {
14
+ options = options || {};
15
+
16
+ this.options = options || {};
17
+
18
+ this.name = 'JSONTransport';
19
+ this.version = packageData.version;
20
+
21
+ this.logger = shared.getLogger(this.options, {
22
+ component: this.options.component || 'json-transport'
23
+ });
24
+ }
25
+
26
+ /**
27
+ * <p>Compiles a mailcomposer message and forwards it to handler that sends it.</p>
28
+ *
29
+ * @param {Object} emailMessage MailComposer object
30
+ * @param {Function} callback Callback function to run when the sending is completed
31
+ */
32
+ send(mail, done) {
33
+ // Sendmail strips this header line by itself
34
+ mail.message.keepBcc = true;
35
+
36
+ let envelope = mail.data.envelope || mail.message.getEnvelope();
37
+ let messageId = mail.message.messageId();
38
+
39
+ let recipients = [].concat(envelope.to || []);
40
+ if (recipients.length > 3) {
41
+ recipients.push('...and ' + recipients.splice(2).length + ' more');
42
+ }
43
+ this.logger.info(
44
+ {
45
+ tnx: 'send',
46
+ messageId
47
+ },
48
+ 'Composing JSON structure of %s to <%s>',
49
+ messageId,
50
+ recipients.join(', ')
51
+ );
52
+
53
+ setImmediate(() => {
54
+ mail.normalize((err, data) => {
55
+ if (err) {
56
+ this.logger.error(
57
+ {
58
+ err,
59
+ tnx: 'send',
60
+ messageId
61
+ },
62
+ 'Failed building JSON structure for %s. %s',
63
+ messageId,
64
+ err.message
65
+ );
66
+ return done(err);
67
+ }
68
+
69
+ delete data.envelope;
70
+ delete data.normalizedHeaders;
71
+
72
+ return done(null, {
73
+ envelope,
74
+ messageId,
75
+ message: this.options.skipEncoding ? data : JSON.stringify(data)
76
+ });
77
+ });
78
+ });
79
+ }
80
+ }
81
+
82
+ module.exports = JSONTransport;
@@ -0,0 +1,629 @@
1
+ /* eslint no-undefined: 0 */
2
+
3
+ 'use strict';
4
+
5
+ const MimeNode = require('../mime-node');
6
+ const mimeFuncs = require('../mime-funcs');
7
+ const parseDataURI = require('../shared').parseDataURI;
8
+
9
+ /**
10
+ * Creates the object for composing a MimeNode instance out from the mail options
11
+ *
12
+ * @constructor
13
+ * @param {Object} mail Mail options
14
+ */
15
+ class MailComposer {
16
+ constructor(mail) {
17
+ this.mail = mail || {};
18
+ this.message = false;
19
+ }
20
+
21
+ /**
22
+ * Builds MimeNode instance
23
+ */
24
+ compile() {
25
+ this._alternatives = this.getAlternatives();
26
+ this._htmlNode = this._alternatives.filter(alternative => /^text\/html\b/i.test(alternative.contentType)).pop();
27
+ this._attachments = this.getAttachments(!!this._htmlNode);
28
+
29
+ this._useRelated = !!(this._htmlNode && this._attachments.related.length);
30
+ this._useAlternative = this._alternatives.length > 1;
31
+ this._useMixed = this._attachments.attached.length > 1 || (this._alternatives.length && this._attachments.attached.length === 1);
32
+
33
+ // Compose MIME tree
34
+ if (this.mail.raw) {
35
+ this.message = new MimeNode('message/rfc822', { newline: this.mail.newline }).setRaw(this.mail.raw);
36
+ } else if (this._useMixed) {
37
+ this.message = this._createMixed();
38
+ } else if (this._useAlternative) {
39
+ this.message = this._createAlternative();
40
+ } else if (this._useRelated) {
41
+ this.message = this._createRelated();
42
+ } else {
43
+ this.message = this._createContentNode(
44
+ false,
45
+ []
46
+ .concat(this._alternatives || [])
47
+ .concat(this._attachments.attached || [])
48
+ .shift() || {
49
+ contentType: 'text/plain',
50
+ content: ''
51
+ }
52
+ );
53
+ }
54
+
55
+ // Add custom headers
56
+ if (this.mail.headers) {
57
+ this.message.addHeader(this.mail.headers);
58
+ }
59
+
60
+ // Add headers to the root node, always overrides custom headers
61
+ ['from', 'sender', 'to', 'cc', 'bcc', 'reply-to', 'in-reply-to', 'references', 'subject', 'message-id', 'date'].forEach(header => {
62
+ let key = header.replace(/-(\w)/g, (o, c) => c.toUpperCase());
63
+ if (this.mail[key]) {
64
+ this.message.setHeader(header, this.mail[key]);
65
+ }
66
+ });
67
+
68
+ // Sets custom envelope
69
+ if (this.mail.envelope) {
70
+ this.message.setEnvelope(this.mail.envelope);
71
+ }
72
+
73
+ // ensure Message-Id value
74
+ this.message.messageId();
75
+
76
+ return this.message;
77
+ }
78
+
79
+ /**
80
+ * List all attachments. Resulting attachment objects can be used as input for MimeNode nodes
81
+ *
82
+ * @param {Boolean} findRelated If true separate related attachments from attached ones
83
+ * @returns {Object} An object of arrays (`related` and `attached`)
84
+ */
85
+ getAttachments(findRelated) {
86
+ let icalEvent, eventObject;
87
+ let attachments = [].concat(this.mail.attachments || []).map((attachment, i) => {
88
+ let data;
89
+
90
+ if (/^data:/i.test(attachment.path || attachment.href)) {
91
+ attachment = this._processDataUrl(attachment);
92
+ }
93
+
94
+ let contentType =
95
+ attachment.contentType || mimeFuncs.detectMimeType(attachment.filename || attachment.path || attachment.href || 'bin');
96
+
97
+ let isImage = /^image\//i.test(contentType);
98
+ let isMessageNode = /^message\//i.test(contentType);
99
+
100
+ let contentDisposition =
101
+ attachment.contentDisposition || (isMessageNode || (isImage && attachment.cid) ? 'inline' : 'attachment');
102
+
103
+ let contentTransferEncoding;
104
+ if ('contentTransferEncoding' in attachment) {
105
+ // also contains `false`, to set
106
+ contentTransferEncoding = attachment.contentTransferEncoding;
107
+ } else if (isMessageNode) {
108
+ // the content might include non-ASCII bytes but at this point we do not know it yet
109
+ contentTransferEncoding = '8bit';
110
+ } else {
111
+ contentTransferEncoding = 'base64'; // the default
112
+ }
113
+
114
+ data = {
115
+ contentType,
116
+ contentDisposition,
117
+ contentTransferEncoding
118
+ };
119
+
120
+ if (attachment.filename) {
121
+ data.filename = attachment.filename;
122
+ } else if (!isMessageNode && attachment.filename !== false) {
123
+ data.filename = (attachment.path || attachment.href || '').split('/').pop().split('?').shift() || 'attachment-' + (i + 1);
124
+ if (data.filename.indexOf('.') < 0) {
125
+ data.filename += '.' + mimeFuncs.detectExtension(data.contentType);
126
+ }
127
+ }
128
+
129
+ if (/^https?:\/\//i.test(attachment.path)) {
130
+ attachment.href = attachment.path;
131
+ attachment.path = undefined;
132
+ }
133
+
134
+ if (attachment.cid) {
135
+ data.cid = attachment.cid;
136
+ }
137
+
138
+ if (attachment.raw) {
139
+ data.raw = attachment.raw;
140
+ } else if (attachment.path) {
141
+ data.content = {
142
+ path: attachment.path
143
+ };
144
+ } else if (attachment.href) {
145
+ data.content = {
146
+ href: attachment.href,
147
+ httpHeaders: attachment.httpHeaders
148
+ };
149
+ } else {
150
+ data.content = attachment.content || '';
151
+ }
152
+
153
+ if (attachment.encoding) {
154
+ data.encoding = attachment.encoding;
155
+ }
156
+
157
+ if (attachment.headers) {
158
+ data.headers = attachment.headers;
159
+ }
160
+
161
+ return data;
162
+ });
163
+
164
+ if (this.mail.icalEvent) {
165
+ if (
166
+ typeof this.mail.icalEvent === 'object' &&
167
+ (this.mail.icalEvent.content || this.mail.icalEvent.path || this.mail.icalEvent.href || this.mail.icalEvent.raw)
168
+ ) {
169
+ icalEvent = this.mail.icalEvent;
170
+ } else {
171
+ icalEvent = {
172
+ content: this.mail.icalEvent
173
+ };
174
+ }
175
+
176
+ eventObject = {};
177
+ Object.keys(icalEvent).forEach(key => {
178
+ eventObject[key] = icalEvent[key];
179
+ });
180
+
181
+ eventObject.contentType = 'application/ics';
182
+ if (!eventObject.headers) {
183
+ eventObject.headers = {};
184
+ }
185
+ eventObject.filename = eventObject.filename || 'invite.ics';
186
+ eventObject.headers['Content-Disposition'] = 'attachment';
187
+ eventObject.headers['Content-Transfer-Encoding'] = 'base64';
188
+ }
189
+
190
+ if (!findRelated) {
191
+ return {
192
+ attached: attachments.concat(eventObject || []),
193
+ related: []
194
+ };
195
+ } else {
196
+ return {
197
+ attached: attachments.filter(attachment => !attachment.cid).concat(eventObject || []),
198
+ related: attachments.filter(attachment => !!attachment.cid)
199
+ };
200
+ }
201
+ }
202
+
203
+ /**
204
+ * List alternatives. Resulting objects can be used as input for MimeNode nodes
205
+ *
206
+ * @returns {Array} An array of alternative elements. Includes the `text` and `html` values as well
207
+ */
208
+ getAlternatives() {
209
+ let alternatives = [],
210
+ text,
211
+ html,
212
+ watchHtml,
213
+ amp,
214
+ icalEvent,
215
+ eventObject;
216
+
217
+ if (this.mail.text) {
218
+ if (
219
+ typeof this.mail.text === 'object' &&
220
+ (this.mail.text.content || this.mail.text.path || this.mail.text.href || this.mail.text.raw)
221
+ ) {
222
+ text = this.mail.text;
223
+ } else {
224
+ text = {
225
+ content: this.mail.text
226
+ };
227
+ }
228
+ text.contentType = 'text/plain; charset=utf-8';
229
+ }
230
+
231
+ if (this.mail.watchHtml) {
232
+ if (
233
+ typeof this.mail.watchHtml === 'object' &&
234
+ (this.mail.watchHtml.content || this.mail.watchHtml.path || this.mail.watchHtml.href || this.mail.watchHtml.raw)
235
+ ) {
236
+ watchHtml = this.mail.watchHtml;
237
+ } else {
238
+ watchHtml = {
239
+ content: this.mail.watchHtml
240
+ };
241
+ }
242
+ watchHtml.contentType = 'text/watch-html; charset=utf-8';
243
+ }
244
+
245
+ if (this.mail.amp) {
246
+ if (
247
+ typeof this.mail.amp === 'object' &&
248
+ (this.mail.amp.content || this.mail.amp.path || this.mail.amp.href || this.mail.amp.raw)
249
+ ) {
250
+ amp = this.mail.amp;
251
+ } else {
252
+ amp = {
253
+ content: this.mail.amp
254
+ };
255
+ }
256
+ amp.contentType = 'text/x-amp-html; charset=utf-8';
257
+ }
258
+
259
+ // NB! when including attachments with a calendar alternative you might end up in a blank screen on some clients
260
+ if (this.mail.icalEvent) {
261
+ if (
262
+ typeof this.mail.icalEvent === 'object' &&
263
+ (this.mail.icalEvent.content || this.mail.icalEvent.path || this.mail.icalEvent.href || this.mail.icalEvent.raw)
264
+ ) {
265
+ icalEvent = this.mail.icalEvent;
266
+ } else {
267
+ icalEvent = {
268
+ content: this.mail.icalEvent
269
+ };
270
+ }
271
+
272
+ eventObject = {};
273
+ Object.keys(icalEvent).forEach(key => {
274
+ eventObject[key] = icalEvent[key];
275
+ });
276
+
277
+ if (eventObject.content && typeof eventObject.content === 'object') {
278
+ // we are going to have the same attachment twice, so mark this to be
279
+ // resolved just once
280
+ eventObject.content._resolve = true;
281
+ }
282
+
283
+ eventObject.filename = false;
284
+ eventObject.contentType =
285
+ 'text/calendar; charset=utf-8; method=' + (eventObject.method || 'PUBLISH').toString().trim().toUpperCase();
286
+ if (!eventObject.headers) {
287
+ eventObject.headers = {};
288
+ }
289
+ }
290
+
291
+ if (this.mail.html) {
292
+ if (
293
+ typeof this.mail.html === 'object' &&
294
+ (this.mail.html.content || this.mail.html.path || this.mail.html.href || this.mail.html.raw)
295
+ ) {
296
+ html = this.mail.html;
297
+ } else {
298
+ html = {
299
+ content: this.mail.html
300
+ };
301
+ }
302
+ html.contentType = 'text/html; charset=utf-8';
303
+ }
304
+
305
+ []
306
+ .concat(text || [])
307
+ .concat(watchHtml || [])
308
+ .concat(amp || [])
309
+ .concat(html || [])
310
+ .concat(eventObject || [])
311
+ .concat(this.mail.alternatives || [])
312
+ .forEach(alternative => {
313
+ let data;
314
+
315
+ if (/^data:/i.test(alternative.path || alternative.href)) {
316
+ alternative = this._processDataUrl(alternative);
317
+ }
318
+
319
+ data = {
320
+ contentType:
321
+ alternative.contentType ||
322
+ mimeFuncs.detectMimeType(alternative.filename || alternative.path || alternative.href || 'txt'),
323
+ contentTransferEncoding: alternative.contentTransferEncoding
324
+ };
325
+
326
+ if (alternative.filename) {
327
+ data.filename = alternative.filename;
328
+ }
329
+
330
+ if (/^https?:\/\//i.test(alternative.path)) {
331
+ alternative.href = alternative.path;
332
+ alternative.path = undefined;
333
+ }
334
+
335
+ if (alternative.raw) {
336
+ data.raw = alternative.raw;
337
+ } else if (alternative.path) {
338
+ data.content = {
339
+ path: alternative.path
340
+ };
341
+ } else if (alternative.href) {
342
+ data.content = {
343
+ href: alternative.href
344
+ };
345
+ } else {
346
+ data.content = alternative.content || '';
347
+ }
348
+
349
+ if (alternative.encoding) {
350
+ data.encoding = alternative.encoding;
351
+ }
352
+
353
+ if (alternative.headers) {
354
+ data.headers = alternative.headers;
355
+ }
356
+
357
+ alternatives.push(data);
358
+ });
359
+
360
+ return alternatives;
361
+ }
362
+
363
+ /**
364
+ * Builds multipart/mixed node. It should always contain different type of elements on the same level
365
+ * eg. text + attachments
366
+ *
367
+ * @param {Object} parentNode Parent for this note. If it does not exist, a root node is created
368
+ * @returns {Object} MimeNode node element
369
+ */
370
+ _createMixed(parentNode) {
371
+ let node;
372
+
373
+ if (!parentNode) {
374
+ node = new MimeNode('multipart/mixed', {
375
+ baseBoundary: this.mail.baseBoundary,
376
+ textEncoding: this.mail.textEncoding,
377
+ boundaryPrefix: this.mail.boundaryPrefix,
378
+ disableUrlAccess: this.mail.disableUrlAccess,
379
+ disableFileAccess: this.mail.disableFileAccess,
380
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
381
+ newline: this.mail.newline
382
+ });
383
+ } else {
384
+ node = parentNode.createChild('multipart/mixed', {
385
+ disableUrlAccess: this.mail.disableUrlAccess,
386
+ disableFileAccess: this.mail.disableFileAccess,
387
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
388
+ newline: this.mail.newline
389
+ });
390
+ }
391
+
392
+ if (this._useAlternative) {
393
+ this._createAlternative(node);
394
+ } else if (this._useRelated) {
395
+ this._createRelated(node);
396
+ }
397
+
398
+ []
399
+ .concat((!this._useAlternative && this._alternatives) || [])
400
+ .concat(this._attachments.attached || [])
401
+ .forEach(element => {
402
+ // if the element is a html node from related subpart then ignore it
403
+ if (!this._useRelated || element !== this._htmlNode) {
404
+ this._createContentNode(node, element);
405
+ }
406
+ });
407
+
408
+ return node;
409
+ }
410
+
411
+ /**
412
+ * Builds multipart/alternative node. It should always contain same type of elements on the same level
413
+ * eg. text + html view of the same data
414
+ *
415
+ * @param {Object} parentNode Parent for this note. If it does not exist, a root node is created
416
+ * @returns {Object} MimeNode node element
417
+ */
418
+ _createAlternative(parentNode) {
419
+ let node;
420
+
421
+ if (!parentNode) {
422
+ node = new MimeNode('multipart/alternative', {
423
+ baseBoundary: this.mail.baseBoundary,
424
+ textEncoding: this.mail.textEncoding,
425
+ boundaryPrefix: this.mail.boundaryPrefix,
426
+ disableUrlAccess: this.mail.disableUrlAccess,
427
+ disableFileAccess: this.mail.disableFileAccess,
428
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
429
+ newline: this.mail.newline
430
+ });
431
+ } else {
432
+ node = parentNode.createChild('multipart/alternative', {
433
+ disableUrlAccess: this.mail.disableUrlAccess,
434
+ disableFileAccess: this.mail.disableFileAccess,
435
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
436
+ newline: this.mail.newline
437
+ });
438
+ }
439
+
440
+ this._alternatives.forEach(alternative => {
441
+ if (this._useRelated && this._htmlNode === alternative) {
442
+ this._createRelated(node);
443
+ } else {
444
+ this._createContentNode(node, alternative);
445
+ }
446
+ });
447
+
448
+ return node;
449
+ }
450
+
451
+ /**
452
+ * Builds multipart/related node. It should always contain html node with related attachments
453
+ *
454
+ * @param {Object} parentNode Parent for this note. If it does not exist, a root node is created
455
+ * @returns {Object} MimeNode node element
456
+ */
457
+ _createRelated(parentNode) {
458
+ let node;
459
+
460
+ if (!parentNode) {
461
+ node = new MimeNode('multipart/related; type="text/html"', {
462
+ baseBoundary: this.mail.baseBoundary,
463
+ textEncoding: this.mail.textEncoding,
464
+ boundaryPrefix: this.mail.boundaryPrefix,
465
+ disableUrlAccess: this.mail.disableUrlAccess,
466
+ disableFileAccess: this.mail.disableFileAccess,
467
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
468
+ newline: this.mail.newline
469
+ });
470
+ } else {
471
+ node = parentNode.createChild('multipart/related; type="text/html"', {
472
+ disableUrlAccess: this.mail.disableUrlAccess,
473
+ disableFileAccess: this.mail.disableFileAccess,
474
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
475
+ newline: this.mail.newline
476
+ });
477
+ }
478
+
479
+ this._createContentNode(node, this._htmlNode);
480
+
481
+ this._attachments.related.forEach(alternative => this._createContentNode(node, alternative));
482
+
483
+ return node;
484
+ }
485
+
486
+ /**
487
+ * Creates a regular node with contents
488
+ *
489
+ * @param {Object} parentNode Parent for this note. If it does not exist, a root node is created
490
+ * @param {Object} element Node data
491
+ * @returns {Object} MimeNode node element
492
+ */
493
+ _createContentNode(parentNode, element) {
494
+ element = element || {};
495
+ element.content = element.content || '';
496
+
497
+ let node;
498
+ let encoding = (element.encoding || 'utf8')
499
+ .toString()
500
+ .toLowerCase()
501
+ .replace(/[-_\s]/g, '');
502
+
503
+ if (!parentNode) {
504
+ node = new MimeNode(element.contentType, {
505
+ filename: element.filename,
506
+ baseBoundary: this.mail.baseBoundary,
507
+ textEncoding: this.mail.textEncoding,
508
+ boundaryPrefix: this.mail.boundaryPrefix,
509
+ disableUrlAccess: this.mail.disableUrlAccess,
510
+ disableFileAccess: this.mail.disableFileAccess,
511
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
512
+ newline: this.mail.newline
513
+ });
514
+ } else {
515
+ node = parentNode.createChild(element.contentType, {
516
+ filename: element.filename,
517
+ textEncoding: this.mail.textEncoding,
518
+ disableUrlAccess: this.mail.disableUrlAccess,
519
+ disableFileAccess: this.mail.disableFileAccess,
520
+ normalizeHeaderKey: this.mail.normalizeHeaderKey,
521
+ newline: this.mail.newline
522
+ });
523
+ }
524
+
525
+ // add custom headers
526
+ if (element.headers) {
527
+ node.addHeader(element.headers);
528
+ }
529
+
530
+ if (element.cid) {
531
+ node.setHeader('Content-Id', '<' + element.cid.replace(/[<>]/g, '') + '>');
532
+ }
533
+
534
+ if (element.contentTransferEncoding) {
535
+ node.setHeader('Content-Transfer-Encoding', element.contentTransferEncoding);
536
+ } else if (this.mail.encoding && /^text\//i.test(element.contentType)) {
537
+ node.setHeader('Content-Transfer-Encoding', this.mail.encoding);
538
+ }
539
+
540
+ if (!/^text\//i.test(element.contentType) || element.contentDisposition) {
541
+ node.setHeader(
542
+ 'Content-Disposition',
543
+ element.contentDisposition || (element.cid && /^image\//i.test(element.contentType) ? 'inline' : 'attachment')
544
+ );
545
+ }
546
+
547
+ if (typeof element.content === 'string' && !['utf8', 'usascii', 'ascii'].includes(encoding)) {
548
+ element.content = Buffer.from(element.content, encoding);
549
+ }
550
+
551
+ // prefer pregenerated raw content
552
+ if (element.raw) {
553
+ node.setRaw(element.raw);
554
+ } else {
555
+ node.setContent(element.content);
556
+ }
557
+
558
+ return node;
559
+ }
560
+
561
+ /**
562
+ * Parses data uri and converts it to a Buffer
563
+ *
564
+ * @param {Object} element Content element
565
+ * @return {Object} Parsed element
566
+ */
567
+ _processDataUrl(element) {
568
+ const dataUrl = element.path || element.href;
569
+
570
+ // Early validation to prevent ReDoS
571
+ if (!dataUrl || typeof dataUrl !== 'string') {
572
+ return element;
573
+ }
574
+
575
+ if (!dataUrl.startsWith('data:')) {
576
+ return element;
577
+ }
578
+
579
+ if (dataUrl.length > 52428800) {
580
+ // 52428800 chars = 50MB limit for data URL string (~37.5MB decoded image)
581
+ // Extract content type before rejecting to preserve MIME type
582
+ let detectedType = 'application/octet-stream';
583
+ const commaPos = dataUrl.indexOf(',');
584
+
585
+ if (commaPos > 0 && commaPos < 200) {
586
+ // Parse header safely with size limit
587
+ const header = dataUrl.substring(5, commaPos); // skip 'data:'
588
+ const parts = header.split(';');
589
+ if (parts[0] && parts[0].includes('/')) {
590
+ detectedType = parts[0].trim();
591
+ }
592
+ }
593
+
594
+ // Return empty content for excessively long data URLs
595
+ return Object.assign({}, element, {
596
+ path: false,
597
+ href: false,
598
+ content: Buffer.alloc(0),
599
+ contentType: element.contentType || detectedType
600
+ });
601
+ }
602
+
603
+ let parsedDataUri;
604
+ try {
605
+ parsedDataUri = parseDataURI(dataUrl);
606
+ } catch (_err) {
607
+ return element;
608
+ }
609
+
610
+ if (!parsedDataUri) {
611
+ return element;
612
+ }
613
+
614
+ element.content = parsedDataUri.data;
615
+ element.contentType = element.contentType || parsedDataUri.contentType;
616
+
617
+ if ('path' in element) {
618
+ element.path = false;
619
+ }
620
+
621
+ if ('href' in element) {
622
+ element.href = false;
623
+ }
624
+
625
+ return element;
626
+ }
627
+ }
628
+
629
+ module.exports = MailComposer;