Skip to content

Commit

Permalink
Add feature to parse email subject (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxwell2022 authored and takkaria committed Aug 7, 2016
1 parent f5cb12d commit fd1b272
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 3.1.0
* Add support for email subject.

### 3.0.0

* Upgrade to Juice 2. This changes various defaults for inlining styles.
Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ var context = {
meatballCount: 9001
};

templates.render('meatball-sandwich.html', context, function(err, html, text) {
templates.render('meatball-sandwich.html', context, function(err, html, text, subject) {

// Send email
transporter.sendMail({
from: 'sender@address',
to: 'receiver@address',
subject: 'Meatball delivery',
subject: subject,
html: html,
text: text
});
Expand Down Expand Up @@ -81,15 +81,15 @@ Path to template files. Defaults to ```path.join(__dirname, 'templates')```

#### swig (object)

Swig options. Gets passed to swig.setDefaults(). [See swig documention for more information](http://paularmstrong.github.io/swig/docs/api/#SwigOpts).
Swig options. Gets passed to swig.setDefaults(). [See swig documentation for more information](http://paularmstrong.github.io/swig/docs/api/#SwigOpts).

#### filters (object)

An object of Swig filters to set. Format: { name1: method1, name2: method2 }. For more information [see Swig documentation for setFilter()](http://paularmstrong.github.io/swig/docs/api/#setFilter).

#### juice (object)

Juice options. [See juice documentation for more inforation](https://github.com/Automattic/juice#options).
Juice options. [See juice documentation for more information](https://github.com/Automattic/juice#options).

#### rewrite (function(cheerio instance))

Expand Down Expand Up @@ -135,16 +135,17 @@ new EmailTemplates({

### render(templateName, context, callback)

Render a template with templateName, with the context provided. Callback takes three parameters: (error, html, text).
Render a template with templateName, with the context provided. Callback takes three parameters: (error, html, text, subject).

Example:

```js
var EmailTemplates = require('swig-email-templates');
var templates = new EmailTemplates();
templates.render('template.html', { user: 55 }, function (err, html, text) {
templates.render('template.html', { user: 55 }, function (err, html, text, subject) {
// html is inlined html
// text is text equivalent
// subject is parsed subject template or null if not found
})
```

Expand All @@ -156,7 +157,11 @@ You can provide your own text template to override this behaviour. This should

If the 'text' option is false, then no text alternative will be generated and the callback passed to the EmailTemplate.render() function will receive a falsy value instead of text as its third argument.

#### Behaviour of subject templates

swig-email-templates will attempt to create a text equivalent as well as your HTML. This template should have the same basename as your HTML template but end in `.subject.txt`. This will receive the same context as the HTML template.

Using subject templates, you can generate subject that contains variables.

## Command Line

Expand Down
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ var EmailTemplates = function(options) {
});
}

/*
* (Internal) Generate text counterpart to HTML template
*/
this.generateSubject = function(templatePath, context, cb) {

var textFilename = path.basename(templatePath, path.extname(templatePath)) + '.subject.txt';
var textPath = path.resolve(path.dirname(templatePath), textFilename);

fs.exists(textPath, function(exists) {
if (exists) {
cb(null, self.useTemplate(textPath, context));
} else {
cb(null, null);
}
});
}

/*
* (Internal) Rewrite URLs in a Cheerio doc using a given function
*/
Expand Down Expand Up @@ -90,7 +107,11 @@ var EmailTemplates = function(options) {
self.generateText(templatePath, context, html, function(err, text) {
if (err) return cb(err);

cb(null, inlinedHTML, text);
self.generateSubject(templatePath, context, function(err, subject) {
if (err) return cb(err);

cb(null, inlinedHTML, text, subject);
});
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('EmailTemplates', function() {
});

templates.render('no_text_file_alternative.html', null, function(err, html, text) {
assert.equal(text, 'This is a message with bold , just a tester.');
assert.equal(text, 'This is a message with bold, just a tester.');
done(err);
});
})
Expand Down
14 changes: 13 additions & 1 deletion test/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('EmailTemplates output', function() {

var expectedHtml, actualHtml;
var expectedText, actualText;
var expectedSubject, actualSubject;

var pend = new Pend();

Expand All @@ -65,9 +66,10 @@ describe('EmailTemplates output', function() {
if (err) return cb(err);
if (data) context = JSON.parse(data);

templates.render(testName + '.html', context, function(err, html, text) {
templates.render(testName + '.html', context, function(err, html, text, subject) {
actualHtml = html;
actualText = text;
actualSubject = subject;
cb(err);
});
});
Expand All @@ -89,6 +91,14 @@ describe('EmailTemplates output', function() {
});
});

// Try loading the expected text (may not exist)
pend.go(function(cb) {
attemptReadFile(testPath + '.out.subject.txt', 'utf8', function(err, subject) {
expectedSubject = subject;
cb(err);
});
});

// And when they're all done...
pend.wait(function(err) {
if (err) return done(err);
Expand All @@ -97,6 +107,8 @@ describe('EmailTemplates output', function() {
assert.strictEqual(actualHtml.trim(), expectedHtml.trim());
if (expectedText)
assert.strictEqual(actualText.trim(), expectedText.trim());
if (expectedSubject)
assert.strictEqual(actualText.trim(), expectedText.trim());

done();
});
Expand Down
3 changes: 2 additions & 1 deletion test/templates/simple_vars.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"hello": "hello"
"hello": "hello",
"name": "John Doe"
}
1 change: 1 addition & 0 deletions test/templates/simple_vars.out.subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello John Doe
2 changes: 1 addition & 1 deletion test/templates/simple_vars.out.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
hello
hello
1 change: 1 addition & 0 deletions test/templates/simple_vars.subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello {{ name }}

0 comments on commit fd1b272

Please sign in to comment.