@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,441 @@
1
+ 'use strict';
2
+
3
+ const EventEmitter = require('events');
4
+ const shared = require('../shared');
5
+ const mimeTypes = require('../mime-funcs/mime-types');
6
+ const MailComposer = require('../mail-composer');
7
+ const DKIM = require('../dkim');
8
+ const httpProxyClient = require('../smtp-connection/http-proxy-client');
9
+ const util = require('util');
10
+ const urllib = require('url');
11
+ const packageData = require('../../package.json');
12
+ const MailMessage = require('./mail-message');
13
+ const net = require('net');
14
+ const dns = require('dns');
15
+ const crypto = require('crypto');
16
+
17
+ /**
18
+ * Creates an object for exposing the Mail API
19
+ *
20
+ * @constructor
21
+ * @param {Object} transporter Transport object instance to pass the mails to
22
+ */
23
+ class Mail extends EventEmitter {
24
+ constructor(transporter, options, defaults) {
25
+ super();
26
+
27
+ this.options = options || {};
28
+ this._defaults = defaults || {};
29
+
30
+ this._defaultPlugins = {
31
+ compile: [(...args) => this._convertDataImages(...args)],
32
+ stream: []
33
+ };
34
+
35
+ this._userPlugins = {
36
+ compile: [],
37
+ stream: []
38
+ };
39
+
40
+ this.meta = new Map();
41
+
42
+ this.dkim = this.options.dkim ? new DKIM(this.options.dkim) : false;
43
+
44
+ this.transporter = transporter;
45
+ this.transporter.mailer = this;
46
+
47
+ this.logger = shared.getLogger(this.options, {
48
+ component: this.options.component || 'mail'
49
+ });
50
+
51
+ this.logger.debug(
52
+ {
53
+ tnx: 'create'
54
+ },
55
+ 'Creating transport: %s',
56
+ this.getVersionString()
57
+ );
58
+
59
+ // setup emit handlers for the transporter
60
+ if (typeof this.transporter.on === 'function') {
61
+ // deprecated log interface
62
+ this.transporter.on('log', log => {
63
+ this.logger.debug(
64
+ {
65
+ tnx: 'transport'
66
+ },
67
+ '%s: %s',
68
+ log.type,
69
+ log.message
70
+ );
71
+ });
72
+
73
+ // transporter errors
74
+ this.transporter.on('error', err => {
75
+ this.logger.error(
76
+ {
77
+ err,
78
+ tnx: 'transport'
79
+ },
80
+ 'Transport Error: %s',
81
+ err.message
82
+ );
83
+ this.emit('error', err);
84
+ });
85
+
86
+ // indicates if the sender has became idle
87
+ this.transporter.on('idle', (...args) => {
88
+ this.emit('idle', ...args);
89
+ });
90
+
91
+ // indicates if the sender has became idle and all connections are terminated
92
+ this.transporter.on('clear', (...args) => {
93
+ this.emit('clear', ...args);
94
+ });
95
+ }
96
+
97
+ /**
98
+ * Optional methods passed to the underlying transport object
99
+ */
100
+ ['close', 'isIdle', 'verify'].forEach(method => {
101
+ this[method] = (...args) => {
102
+ if (typeof this.transporter[method] === 'function') {
103
+ if (method === 'verify' && typeof this.getSocket === 'function') {
104
+ this.transporter.getSocket = this.getSocket;
105
+ this.getSocket = false;
106
+ }
107
+ return this.transporter[method](...args);
108
+ } else {
109
+ this.logger.warn(
110
+ {
111
+ tnx: 'transport',
112
+ methodName: method
113
+ },
114
+ 'Non existing method %s called for transport',
115
+ method
116
+ );
117
+ return false;
118
+ }
119
+ };
120
+ });
121
+
122
+ // setup proxy handling
123
+ if (this.options.proxy && typeof this.options.proxy === 'string') {
124
+ this.setupProxy(this.options.proxy);
125
+ }
126
+ }
127
+
128
+ use(step, plugin) {
129
+ step = (step || '').toString();
130
+ if (!this._userPlugins.hasOwnProperty(step)) {
131
+ this._userPlugins[step] = [plugin];
132
+ } else {
133
+ this._userPlugins[step].push(plugin);
134
+ }
135
+
136
+ return this;
137
+ }
138
+
139
+ /**
140
+ * Sends an email using the preselected transport object
141
+ *
142
+ * @param {Object} data E-data description
143
+ * @param {Function?} callback Callback to run once the sending succeeded or failed
144
+ */
145
+ sendMail(data, callback = null) {
146
+ let promise;
147
+
148
+ if (!callback) {
149
+ promise = new Promise((resolve, reject) => {
150
+ callback = shared.callbackPromise(resolve, reject);
151
+ });
152
+ }
153
+
154
+ if (typeof this.getSocket === 'function') {
155
+ this.transporter.getSocket = this.getSocket;
156
+ this.getSocket = false;
157
+ }
158
+
159
+ let mail = new MailMessage(this, data);
160
+
161
+ this.logger.debug(
162
+ {
163
+ tnx: 'transport',
164
+ name: this.transporter.name,
165
+ version: this.transporter.version,
166
+ action: 'send'
167
+ },
168
+ 'Sending mail using %s/%s',
169
+ this.transporter.name,
170
+ this.transporter.version
171
+ );
172
+
173
+ this._processPlugins('compile', mail, err => {
174
+ if (err) {
175
+ this.logger.error(
176
+ {
177
+ err,
178
+ tnx: 'plugin',
179
+ action: 'compile'
180
+ },
181
+ 'PluginCompile Error: %s',
182
+ err.message
183
+ );
184
+ return callback(err);
185
+ }
186
+
187
+ mail.message = new MailComposer(mail.data).compile();
188
+
189
+ mail.setMailerHeader();
190
+ mail.setPriorityHeaders();
191
+ mail.setListHeaders();
192
+
193
+ this._processPlugins('stream', mail, err => {
194
+ if (err) {
195
+ this.logger.error(
196
+ {
197
+ err,
198
+ tnx: 'plugin',
199
+ action: 'stream'
200
+ },
201
+ 'PluginStream Error: %s',
202
+ err.message
203
+ );
204
+ return callback(err);
205
+ }
206
+
207
+ if (mail.data.dkim || this.dkim) {
208
+ mail.message.processFunc(input => {
209
+ let dkim = mail.data.dkim ? new DKIM(mail.data.dkim) : this.dkim;
210
+ this.logger.debug(
211
+ {
212
+ tnx: 'DKIM',
213
+ messageId: mail.message.messageId(),
214
+ dkimDomains: dkim.keys.map(key => key.keySelector + '.' + key.domainName).join(', ')
215
+ },
216
+ 'Signing outgoing message with %s keys',
217
+ dkim.keys.length
218
+ );
219
+ return dkim.sign(input, mail.data._dkim);
220
+ });
221
+ }
222
+
223
+ this.transporter.send(mail, (...args) => {
224
+ if (args[0]) {
225
+ this.logger.error(
226
+ {
227
+ err: args[0],
228
+ tnx: 'transport',
229
+ action: 'send'
230
+ },
231
+ 'Send Error: %s',
232
+ args[0].message
233
+ );
234
+ }
235
+ callback(...args);
236
+ });
237
+ });
238
+ });
239
+
240
+ return promise;
241
+ }
242
+
243
+ getVersionString() {
244
+ return util.format(
245
+ '%s (%s; +%s; %s/%s)',
246
+ packageData.name,
247
+ packageData.version,
248
+ packageData.homepage,
249
+ this.transporter.name,
250
+ this.transporter.version
251
+ );
252
+ }
253
+
254
+ _processPlugins(step, mail, callback) {
255
+ step = (step || '').toString();
256
+
257
+ if (!this._userPlugins.hasOwnProperty(step)) {
258
+ return callback();
259
+ }
260
+
261
+ let userPlugins = this._userPlugins[step] || [];
262
+ let defaultPlugins = this._defaultPlugins[step] || [];
263
+
264
+ if (userPlugins.length) {
265
+ this.logger.debug(
266
+ {
267
+ tnx: 'transaction',
268
+ pluginCount: userPlugins.length,
269
+ step
270
+ },
271
+ 'Using %s plugins for %s',
272
+ userPlugins.length,
273
+ step
274
+ );
275
+ }
276
+
277
+ if (userPlugins.length + defaultPlugins.length === 0) {
278
+ return callback();
279
+ }
280
+
281
+ let pos = 0;
282
+ let block = 'default';
283
+ let processPlugins = () => {
284
+ let curplugins = block === 'default' ? defaultPlugins : userPlugins;
285
+ if (pos >= curplugins.length) {
286
+ if (block === 'default' && userPlugins.length) {
287
+ block = 'user';
288
+ pos = 0;
289
+ curplugins = userPlugins;
290
+ } else {
291
+ return callback();
292
+ }
293
+ }
294
+ let plugin = curplugins[pos++];
295
+ plugin(mail, err => {
296
+ if (err) {
297
+ return callback(err);
298
+ }
299
+ processPlugins();
300
+ });
301
+ };
302
+
303
+ processPlugins();
304
+ }
305
+
306
+ /**
307
+ * Sets up proxy handler for a Nodemailer object
308
+ *
309
+ * @param {String} proxyUrl Proxy configuration url
310
+ */
311
+ setupProxy(proxyUrl) {
312
+ let proxy = urllib.parse(proxyUrl);
313
+
314
+ // setup socket handler for the mailer object
315
+ this.getSocket = (options, callback) => {
316
+ let protocol = proxy.protocol.replace(/:$/, '').toLowerCase();
317
+
318
+ if (this.meta.has('proxy_handler_' + protocol)) {
319
+ return this.meta.get('proxy_handler_' + protocol)(proxy, options, callback);
320
+ }
321
+
322
+ switch (protocol) {
323
+ // Connect using a HTTP CONNECT method
324
+ case 'http':
325
+ case 'https':
326
+ httpProxyClient(proxy.href, options.port, options.host, (err, socket) => {
327
+ if (err) {
328
+ return callback(err);
329
+ }
330
+ return callback(null, {
331
+ connection: socket
332
+ });
333
+ });
334
+ return;
335
+ case 'socks':
336
+ case 'socks5':
337
+ case 'socks4':
338
+ case 'socks4a': {
339
+ if (!this.meta.has('proxy_socks_module')) {
340
+ return callback(new Error('Socks module not loaded'));
341
+ }
342
+ let connect = ipaddress => {
343
+ let proxyV2 = !!this.meta.get('proxy_socks_module').SocksClient;
344
+ let socksClient = proxyV2 ? this.meta.get('proxy_socks_module').SocksClient : this.meta.get('proxy_socks_module');
345
+ let proxyType = Number(proxy.protocol.replace(/\D/g, '')) || 5;
346
+ let connectionOpts = {
347
+ proxy: {
348
+ ipaddress,
349
+ port: Number(proxy.port),
350
+ type: proxyType
351
+ },
352
+ [proxyV2 ? 'destination' : 'target']: {
353
+ host: options.host,
354
+ port: options.port
355
+ },
356
+ command: 'connect'
357
+ };
358
+
359
+ if (proxy.auth) {
360
+ let username = decodeURIComponent(proxy.auth.split(':').shift());
361
+ let password = decodeURIComponent(proxy.auth.split(':').pop());
362
+ if (proxyV2) {
363
+ connectionOpts.proxy.userId = username;
364
+ connectionOpts.proxy.password = password;
365
+ } else if (proxyType === 4) {
366
+ connectionOpts.userid = username;
367
+ } else {
368
+ connectionOpts.authentication = {
369
+ username,
370
+ password
371
+ };
372
+ }
373
+ }
374
+
375
+ socksClient.createConnection(connectionOpts, (err, info) => {
376
+ if (err) {
377
+ return callback(err);
378
+ }
379
+ return callback(null, {
380
+ connection: info.socket || info
381
+ });
382
+ });
383
+ };
384
+
385
+ if (net.isIP(proxy.hostname)) {
386
+ return connect(proxy.hostname);
387
+ }
388
+
389
+ return dns.resolve(proxy.hostname, (err, address) => {
390
+ if (err) {
391
+ return callback(err);
392
+ }
393
+ connect(Array.isArray(address) ? address[0] : address);
394
+ });
395
+ }
396
+ }
397
+ callback(new Error('Unknown proxy configuration'));
398
+ };
399
+ }
400
+
401
+ _convertDataImages(mail, callback) {
402
+ if ((!this.options.attachDataUrls && !mail.data.attachDataUrls) || !mail.data.html) {
403
+ return callback();
404
+ }
405
+ mail.resolveContent(mail.data, 'html', (err, html) => {
406
+ if (err) {
407
+ return callback(err);
408
+ }
409
+ let cidCounter = 0;
410
+ html = (html || '')
411
+ .toString()
412
+ .replace(/(<img\b[^<>]{0,1024} src\s{0,20}=[\s"']{0,20})(data:([^;]+);[^"'>\s]+)/gi, (match, prefix, dataUri, mimeType) => {
413
+ let cid = crypto.randomBytes(10).toString('hex') + '@localhost';
414
+ if (!mail.data.attachments) {
415
+ mail.data.attachments = [];
416
+ }
417
+ if (!Array.isArray(mail.data.attachments)) {
418
+ mail.data.attachments = [].concat(mail.data.attachments || []);
419
+ }
420
+ mail.data.attachments.push({
421
+ path: dataUri,
422
+ cid,
423
+ filename: 'image-' + ++cidCounter + '.' + mimeTypes.detectExtension(mimeType)
424
+ });
425
+ return prefix + 'cid:' + cid;
426
+ });
427
+ mail.data.html = html;
428
+ callback();
429
+ });
430
+ }
431
+
432
+ set(key, value) {
433
+ return this.meta.set(key, value);
434
+ }
435
+
436
+ get(key) {
437
+ return this.meta.get(key);
438
+ }
439
+ }
440
+
441
+ module.exports = Mail;