351 |
351 |
|
|
352 |
352 |
|
processRecipient(options) { |
353 |
353 |
|
const { recipient, name, from, raw } = options; |
354 |
|
- |
const { address, addresses, port } = recipient; |
355 |
|
- |
return Promise.all( |
356 |
|
- |
addresses.map(({ to, host }) => { |
357 |
|
- |
return this.processAddress(address, { |
358 |
|
- |
host, |
359 |
|
- |
name, |
360 |
|
- |
envelope: { |
361 |
|
- |
from, |
362 |
|
- |
to |
363 |
|
- |
}, |
364 |
|
- |
raw, |
365 |
|
- |
port |
366 |
|
- |
}); |
367 |
|
- |
}) |
368 |
|
- |
); |
|
354 |
+ |
return this.processAddress(recipient.replacements, { |
|
355 |
+ |
host: recipient.host, |
|
356 |
+ |
name, |
|
357 |
+ |
envelope: { |
|
358 |
+ |
from, |
|
359 |
+ |
to: recipient.to.join(', ') |
|
360 |
+ |
}, |
|
361 |
+ |
raw, |
|
362 |
+ |
port: recipient.port |
|
363 |
+ |
}); |
369 |
364 |
|
} |
370 |
365 |
|
|
371 |
|
- |
async processAddress(address, options) { |
|
366 |
+ |
async processAddress(replacements, options) { |
372 |
367 |
|
try { |
373 |
368 |
|
const info = await this.sendEmail(options); |
374 |
|
- |
logger.log(info); |
|
369 |
+ |
this.config.logger.log(info); |
375 |
370 |
|
return info; |
376 |
371 |
|
} catch (err) { |
|
372 |
+ |
this.config.logger.error(err); |
377 |
373 |
|
// here we do some magic so that we push an error message |
378 |
374 |
|
// that has the end-recipient's email masked with the |
379 |
375 |
|
// original to address that we were trying to send to |
380 |
|
- |
err.message = err.message.replace( |
381 |
|
- |
new RegExp(options.envelope.to, 'gi'), |
382 |
|
- |
address |
383 |
|
- |
); |
384 |
|
- |
logger.error(err); |
|
376 |
+ |
for (const address of Object.keys(replacements)) { |
|
377 |
+ |
err.message = err.message.replace( |
|
378 |
+ |
new RegExp(address, 'gi'), |
|
379 |
+ |
replacements[address] |
|
380 |
+ |
); |
|
381 |
+ |
} |
|
382 |
+ |
|
385 |
383 |
|
return { |
386 |
384 |
|
accepted: [], |
387 |
|
- |
rejected: [address], |
|
385 |
+ |
// TODO: in future handle this `options.port` |
|
386 |
+ |
// and also handle it in `13) send email` |
|
387 |
+ |
rejected: [options.host], |
388 |
388 |
|
rejectedErrors: [err] |
389 |
389 |
|
}; |
390 |
390 |
|
} |
391 |
391 |
|
} |
392 |
392 |
|
|
393 |
|
- |
// TODO: eventually we can combine multiple recipients |
394 |
|
- |
// that have the same MX records in the same envelope `to` |
|
393 |
+ |
// we have already combined multiple recipients with same host+port mx combo |
395 |
394 |
|
sendEmail(options) { |
396 |
395 |
|
const { host, name, envelope, raw, port } = options; |
397 |
396 |
|
const transporter = nodemailer.createTransport({ |
398 |
397 |
|
...transporterConfig, |
399 |
398 |
|
...this.config.ssl, |
400 |
399 |
|
port: parseInt(port, 10), |
|
400 |
+ |
logger: this.config.logger, |
401 |
401 |
|
host, |
402 |
402 |
|
name |
403 |
403 |
|
}); |
404 |
|
- |
logger.debug('sendEmail', { envelope, port, host, name, raw }); |
405 |
404 |
|
return transporter.sendMail({ envelope, raw }); |
406 |
405 |
|
} |
407 |
406 |
|
|