diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c63a098..908f93f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 4.3.0 2017-10-23 + +- Added new mail object method `mail.normalize(cb)` that should make creating HTTP API based transports much easier + ## 4.2.0 2017-10-13 - Expose streamed messages size and timers in info response diff --git a/lib/json-transport/index.js b/lib/json-transport/index.js index 1d0969ce..d00597ed 100644 --- a/lib/json-transport/index.js +++ b/lib/json-transport/index.js @@ -57,7 +57,7 @@ class JSONTransport { ); setImmediate(() => { - mail.resolveAll((err, data) => { + mail.normalize((err, data) => { if (err) { this.logger.error( { @@ -72,43 +72,11 @@ class JSONTransport { return done(err); } - data.messageId = messageId; - - ['html', 'text', 'watchHtml'].forEach(key => { - if (data[key] && data[key].content) { - if (typeof data[key].content === 'string') { - data[key] = data[key].content; - } else if (Buffer.isBuffer(data[key].content)) { - data[key] = data[key].content.toString(); - } - } - }); - - if (data.icalEvent && Buffer.isBuffer(data.icalEvent.content)) { - data.icalEvent.content = data.icalEvent.content.toString('base64'); - data.icalEvent.encoding = 'base64'; - } - - if (data.alternatives && data.alternatives.length) { - data.alternatives.forEach(alternative => { - if (alternative && alternative.content && Buffer.isBuffer(alternative.content)) { - alternative.content = alternative.content.toString('base64'); - alternative.encoding = 'base64'; - } - }); - } - - if (data.attachments && data.attachments.length) { - data.attachments.forEach(attachment => { - if (attachment && attachment.content && Buffer.isBuffer(attachment.content)) { - attachment.content = attachment.content.toString('base64'); - attachment.encoding = 'base64'; - } - }); - } + delete data.envelope; + delete data.normalizedHeaders; return done(null, { - envelope: mail.data.envelope || mail.message.getEnvelope(), + envelope, messageId, message: JSON.stringify(data) }); diff --git a/lib/mailer/mail-message.js b/lib/mailer/mail-message.js index 6bfb443f..a51a970f 100644 --- a/lib/mailer/mail-message.js +++ b/lib/mailer/mail-message.js @@ -135,6 +135,82 @@ class MailMessage { setImmediate(() => resolveNext()); } + normalize(callback) { + let envelope = this.data.envelope || this.message.getEnvelope(); + let messageId = this.message.messageId(); + + this.resolveAll((err, data) => { + if (err) { + return callback(err); + } + + data.envelope = envelope; + data.messageId = messageId; + + ['html', 'text', 'watchHtml'].forEach(key => { + if (data[key] && data[key].content) { + if (typeof data[key].content === 'string') { + data[key] = data[key].content; + } else if (Buffer.isBuffer(data[key].content)) { + data[key] = data[key].content.toString(); + } + } + }); + + if (data.icalEvent && Buffer.isBuffer(data.icalEvent.content)) { + data.icalEvent.content = data.icalEvent.content.toString('base64'); + data.icalEvent.encoding = 'base64'; + } + + if (data.alternatives && data.alternatives.length) { + data.alternatives.forEach(alternative => { + if (alternative && alternative.content && Buffer.isBuffer(alternative.content)) { + alternative.content = alternative.content.toString('base64'); + alternative.encoding = 'base64'; + } + }); + } + + if (data.attachments && data.attachments.length) { + data.attachments.forEach(attachment => { + if (attachment && attachment.content && Buffer.isBuffer(attachment.content)) { + attachment.content = attachment.content.toString('base64'); + attachment.encoding = 'base64'; + } + }); + } + + data.normalizedHeaders = {}; + Object.keys(data.headers || {}).forEach(key => { + let value = [].concat(data.headers[key] || []).shift(); + value = (value && value.value) || value; + if (value) { + if (['references', 'in-reply-to', 'message-id', 'content-id'].includes(key)) { + value = this.message._encodeHeaderValue(key, value); + } + data.normalizedHeaders[key] = value; + } + }); + + if (data.list && typeof data.list === 'object') { + let listHeaders = this._getListHeaders(data.list); + listHeaders.forEach(entry => { + data.normalizedHeaders[entry.key] = entry.value.map(val => (val && val.value) || val).join(', '); + }); + } + + if (data.references) { + data.normalizedHeaders.references = this.message._encodeHeaderValue('references', data.references); + } + + if (data.inReplyTo) { + data.normalizedHeaders['in-reply-to'] = this.message._encodeHeaderValue('in-reply-to', data.inReplyTo); + } + + return callback(null, data); + }); + } + setMailerHeader() { if (!this.message || !this.data.xMailer) { return; diff --git a/package.json b/package.json index d75e8500..04707fdd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nodemailer", - "version": "4.2.0", + "version": "4.3.0", "description": "Easy as cake e-mail sending from your Node.js applications", "main": "lib/nodemailer.js", "scripts": {