Skip to content

Commit

Permalink
v4.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Oct 23, 2017
1 parent 11121b8 commit 22fcff8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
40 changes: 4 additions & 36 deletions lib/json-transport/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class JSONTransport {
);

setImmediate(() => {
mail.resolveAll((err, data) => {
mail.normalize((err, data) => {
if (err) {
this.logger.error(
{
Expand All @@ -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)
});
Expand Down
76 changes: 76 additions & 0 deletions lib/mailer/mail-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 22fcff8

Please sign in to comment.