Skip to content

Commit

Permalink
Added new message option watchHtml. Bumped version to v1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Jun 27, 2015
1 parent 97f8f16 commit f81053e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v1.4.0 2015-06-27

Added new message option `watchHtml` to specify Apple Watch specific HTML part of the message. See [this post](https://litmus.com/blog/how-to-send-hidden-version-email-apple-watch) for details

## v1.3.4 2015-04-25

Maintenance release, bumped buildmail version to get fixed format=flowed handling
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ transporter.sendMail(mailOptions, function(error, info){
return console.log(error);
}
console.log('Message sent: ' + info.response);

});
```

Expand Down Expand Up @@ -226,6 +226,7 @@ The following are the possible fields of an e-mail message:
- **subject** - The subject of the e-mail
- **text** - The plaintext version of the message as an Unicode string, Buffer, Stream or an object *{path: '...'}*
- **html** - The HTML version of the message as an Unicode string, Buffer, Stream or an object *{path: '...'}*
- **watchHtml** - Apple Watch specific HTML version of the message (*experimental*)
- **headers** - An object or array of additional header fields (e.g. *{"X-Key-Name": "key value"}* or *[{key: "X-Key-Name", value: "val1"}, {key: "X-Key-Name", value: "val2"}]*)
- **attachments** - An array of attachment objects (see [below](#attachments) for details)
- **alternatives** - An array of alternative text contents (in addition to text and html parts) (see [below](#alternatives) for details)
Expand Down
5 changes: 4 additions & 1 deletion examples/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ var message = {
html: '<p><b>Hello</b> to myself <img src="cid:[email protected]"/></p>' +
'<p>Here\'s a nyan cat for you as an embedded attachment:<br/><img src="cid:[email protected]"/></p>',

// Apple Watch specific HTML body
watchHtml: '<b>Hello</b> to myself',

// An array of attachments
attachments: [

Expand Down Expand Up @@ -74,4 +77,4 @@ transporter.sendMail(message, function(error, info) {
}
console.log('Message sent successfully!');
console.log('Server responded with "%s"', info.response);
});
});
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodemailer",
"version": "1.3.4",
"version": "1.4.0",
"description": "Easy as cake e-mail sending from your Node.js applications",
"main": "src/nodemailer.js",
"scripts": {
Expand All @@ -26,24 +26,24 @@
},
"homepage": "http://www.nodemailer.com",
"devDependencies": {
"chai": "^2.2.0",
"chai": "^3.0.0",
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.11.1",
"grunt-contrib-jshint": "^0.11.2",
"grunt-mocha-test": "^0.12.7",
"nodemailer-dkim": "^1.0.2",
"nodemailer-dkim": "^1.0.3",
"nodemailer-markdown": "^1.0.0",
"nodemailer-stub-transport": "^1.0.0",
"smtp-server": "^1.2.0",
"sinon": "^1.14.1"
"smtp-server": "^1.4.0",
"sinon": "^1.15.4"
},
"dependencies": {
"buildmail": "^1.2.4",
"hyperquest": "^1.2.0",
"libmime": "^1.0.0",
"nodemailer-direct-transport": "^1.0.2",
"nodemailer-smtp-transport": "^1.0.2"
"nodemailer-smtp-transport": "^1.0.3"
},
"engines": {
"node": ">=0.10.0"
}
}
}
17 changes: 14 additions & 3 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ Compiler.prototype._getAttachments = function(findRelated) {
*/
Compiler.prototype._getAlternatives = function() {
var alternatives = [],
text, html;
text, html, watchHtml;

if (this.mail.text) {
if (typeof this.mail.text === 'object' && this.mail.text.content || this.mail.text.path || this.mail.text.href) {
Expand All @@ -304,6 +304,17 @@ Compiler.prototype._getAlternatives = function() {
text.contentType = 'text/plain' + (!text.encoding && libmime.isPlainText(text.content) ? '' : '; charset=utf-8');
}

if (this.mail.watchHtml) {
if (typeof this.mail.watchHtml === 'object' && this.mail.watchHtml.content || this.mail.watchHtml.path || this.mail.watchHtml.href) {
watchHtml = this.mail.watchHtml;
} else {
watchHtml = {
content: this.mail.watchHtml
};
}
watchHtml.contentType = 'text/watch-html' + (!watchHtml.encoding && libmime.isPlainText(watchHtml.content) ? '' : '; charset=utf-8');
}

if (this.mail.html) {
if (typeof this.mail.html === 'object' && this.mail.html.content || this.mail.html.path || this.mail.html.href) {
html = this.mail.html;
Expand All @@ -315,7 +326,7 @@ Compiler.prototype._getAlternatives = function() {
html.contentType = 'text/html' + (!html.encoding && libmime.isPlainText(html.content) ? '' : '; charset=utf-8');
}

[].concat(text || []).concat(html || []).concat(this.mail.alternatives || []).forEach(function(alternative) {
[].concat(text || []).concat(watchHtml || []).concat(html || []).concat(this.mail.alternatives || []).forEach(function(alternative) {
var data;

if (/^data:/i.test(alternative.path || alternative.href)) {
Expand Down Expand Up @@ -387,4 +398,4 @@ Compiler.prototype._processDataUrl = function(element) {
});

return element;
};
};
25 changes: 24 additions & 1 deletion test/compiler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ describe('Compiler unit tests', function() {
sinon.stub(compiler, '_createAlternative');
compiler.compile();
expect(compiler._createAlternative.callCount).to.equal(1);

expect(compiler._alternatives.length).to.equal(2);
expect(compiler._alternatives[0].contentType).to.equal('text/plain');
expect(compiler._alternatives[1].contentType).to.equal('text/html');

compiler._createAlternative.restore();
});

it('should create Alternative structure with text, watchHtml and html', function() {
var data = {
text: 'abc',
html: 'def',
watchHtml: 'ghi'
};

var compiler = new Compiler(data);
sinon.stub(compiler, '_createAlternative');
compiler.compile();
expect(compiler._createAlternative.callCount).to.equal(1);
expect(compiler._alternatives.length).to.equal(3);
expect(compiler._alternatives[0].contentType).to.equal('text/plain');
expect(compiler._alternatives[1].contentType).to.equal('text/watch-html');
expect(compiler._alternatives[2].contentType).to.equal('text/html');
compiler._createAlternative.restore();
});

Expand Down Expand Up @@ -151,4 +174,4 @@ describe('Compiler unit tests', function() {
expect(compiler.mail.attachments[0].contentType).to.equal('image/png');
});
});
});
});
2 changes: 1 addition & 1 deletion test/fixtures/message.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p>Tere, tere</p><p>vana kere!</p>
<p>Tere, tere</p><p>vana kere!</p>
21 changes: 11 additions & 10 deletions test/nodemailer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('Nodemailer unit tests', function() {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('<p>Tere, tere</p><p>vana kere!</p>');
res.end('<p>Tere, tere</p><p>vana kere!</p>\n');
});

server.listen(port, done);
Expand All @@ -143,20 +143,20 @@ describe('Nodemailer unit tests', function() {
it('should set text from html string', function(done) {
var mail = {
data: {
html: '<p>Tere, tere</p><p>vana kere!</p>'
html: '<p>Tere, tere</p><p>vana kere!</p>\n'
}
};
nm.resolveContent(mail.data, 'html', function(err, value) {
expect(err).to.not.exist;
expect(value).to.equal('<p>Tere, tere</p><p>vana kere!</p>');
expect(value).to.equal('<p>Tere, tere</p><p>vana kere!</p>\n');
done();
});
});

it('should set text from html buffer', function(done) {
var mail = {
data: {
html: new Buffer('<p>Tere, tere</p><p>vana kere!</p>')
html: new Buffer('<p>Tere, tere</p><p>vana kere!</p>\n')
}
};
nm.resolveContent(mail.data, 'html', function(err, value) {
Expand All @@ -176,7 +176,7 @@ describe('Nodemailer unit tests', function() {
};
nm.resolveContent(mail.data, 'html', function(err, value) {
expect(err).to.not.exist;
expect(value).to.deep.equal(new Buffer('<p>Tere, tere</p><p>vana kere!</p>'));
expect(value).to.deep.equal(new Buffer('<p>Tere, tere</p><p>vana kere!</p>\n'));
done();
});
});
Expand All @@ -191,7 +191,7 @@ describe('Nodemailer unit tests', function() {
};
nm.resolveContent(mail.data, 'html', function(err, value) {
expect(err).to.not.exist;
expect(value).to.deep.equal(new Buffer('<p>Tere, tere</p><p>vana kere!</p>'));
expect(value).to.deep.equal(new Buffer('<p>Tere, tere</p><p>vana kere!</p>\n'));
done();
});
});
Expand All @@ -206,10 +206,10 @@ describe('Nodemailer unit tests', function() {
expect(err).to.not.exist;
expect(mail).to.deep.equal({
data: {
html: new Buffer('<p>Tere, tere</p><p>vana kere!</p>')
html: new Buffer('<p>Tere, tere</p><p>vana kere!</p>\n')
}
});
expect(value).to.deep.equal(new Buffer('<p>Tere, tere</p><p>vana kere!</p>'));
expect(value).to.deep.equal(new Buffer('<p>Tere, tere</p><p>vana kere!</p>\n'));
done();
});
});
Expand All @@ -229,7 +229,7 @@ describe('Nodemailer unit tests', function() {
});

it('should return encoded string as buffer', function(done) {
var str = '<p>Tere, tere</p><p>vana kere!</p>';
var str = '<p>Tere, tere</p><p>vana kere!</p>\n';
var mail = {
data: {
html: {
Expand Down Expand Up @@ -311,6 +311,7 @@ describe('Nodemailer unit tests', function() {
});

describe('Nodemailer integration tests', function() {
this.timeout(10000);
var server;

beforeEach(function(done) {
Expand Down Expand Up @@ -501,4 +502,4 @@ describe('Nodemailer integration tests', function() {
done();
});
});
});
});

0 comments on commit f81053e

Please sign in to comment.