Skip to content

Commit 9857ada

Browse files
Merge pull request #3 from randytarampi/template-api-support
Add the ability for `mailmason` to generate and upload/update templates.
2 parents 445a7a4 + 2cfb716 commit 9857ada

File tree

11 files changed

+459
-37
lines changed

11 files changed

+459
-37
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.idea
3-
secrets.json
3+
config.json
4+
secrets.json

Gruntfile.js

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,76 @@
33
* https://github.com/wildbit/grunt-postmark.git
44
*/
55

6-
module.exports = function(grunt) {
6+
module.exports = function (grunt) {
7+
var secret = grunt.file.readJSON('secrets.json');
8+
var config = grunt.file.readJSON('config.json');
79

810
grunt.initConfig({
9-
secrets: grunt.file.readJSON('secrets.json'),
11+
secret: secret,
12+
config: config,
1013

1114
/* Postmark
12-
------------------------------------------------- */
15+
------------------------------------------------- */
1316
postmark: {
1417
options: {
15-
serverToken: '<%= secrets.serverToken %>'
18+
serverToken: "<%= secret.postmark.server_token %>"
1619
},
1720
email: {
18-
19-
20-
subject: 'Yo',
21-
src: ['test/email.html']
21+
from: "<%= config.postmark.from %>",
22+
to: "<%= config.postmark.to %>",
23+
subject: "<%= config.postmark.subject %>",
24+
src: ["test/email.html"]
2225
},
2326
bulk: {
24-
25-
26-
subject: 'Hey',
27-
src: ['test/*.html']
27+
from: "<%= config.postmark.from %>",
28+
to: "<%= config.postmark.to %>",
29+
subject: "<%= config.postmark.subject %>",
30+
src: ["test/*.html"]
2831
}
29-
}
32+
},
33+
34+
"postmark-templates-upload": {
35+
options: {
36+
ephemeralUploadResultsProperty: "<%= config.templates && config.templates.ephemeralUploadResultsProperty %>",
37+
serverToken: "<%= secret.postmark.server_token %>"
38+
},
39+
test_email: {
40+
name: "testing-postmark-templates-js1-" + new Date().valueOf(),
41+
subject: "Testing grunt-postmark-templates",
42+
htmlSrc: "test/email.html",
43+
textSrc: "test/email.txt"
44+
},
45+
test_email_again: {
46+
name: "testing-postmark-templates-js2-" + new Date().valueOf(),
47+
subject: "Testing grunt-postmark-templates (again)",
48+
htmlSrc: "test/email.html",
49+
textSrc: "test/email.txt"
50+
},
51+
test_email_inline_body: {
52+
name: "testing-postmark-templates-js3-" + new Date().valueOf(),
53+
subject: "Testing grunt-postmark-templates (inline body)",
54+
htmlBody: "<html><body><h1>Another email test</h1></body></html>",
55+
textBody: "Hello from grunt-postmark-templates\n"
56+
}
57+
},
3058

59+
"postmark-templates-output": {
60+
options: {
61+
cleanOutput: "<%= config.templates && config.templates.cleanOutput %>",
62+
outputFile: "<%= config.templates && config.templates.outputFile || config.templates && config.templates.file %>",
63+
ephemeralUploadResultsProperty: "<%= config.templates && config.templates.ephemeralUploadResultsProperty %>"
64+
}
65+
},
66+
67+
"postmark-templates-parse": {
68+
options: {
69+
inputFile: "<%= config.templates && config.templates.inputFile || config.templates && config.templates.file %>"
70+
}
71+
}
3172
});
3273

33-
grunt.loadTasks('tasks');
74+
grunt.loadTasks("tasks");
3475

35-
grunt.registerTask('default', ['postmark']);
76+
grunt.registerTask("default", ["postmark", "postmark-templates"]);
3677

3778
};

README.md

Lines changed: 187 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,39 @@ After the plugin is installed, it can be enabled in your Gruntfile:
2020
grunt.loadNpmTasks('grunt-postmark');
2121
```
2222

23+
You'll need to add a [`config.json`](https://github.com/wildbit/mailmason/wiki/Getting-Started#create-configjson-required) and a [`secrets.json`](https://github.com/wildbit/mailmason/wiki/Getting-Started#create-secretsjson-optional) per the `mailmason` configuration.
24+
2325
## Postmark task
2426
_Run this task with the `grunt postmark` command._
2527

26-
## Options
27-
28+
### Options
2829

29-
### serverToken
30+
#### serverToken
3031
Your server token can be found on your server’s credentials page on [Postmark’s](http://postmarkapp.com) app.
3132

3233
Type: `String`
3334

3435

35-
### from
36+
#### from
3637
This is the from address you are using to send the email. This must be a confirmed address that's set up on [Postmark’s](http://postmarkapp.com) sender signatures.
3738

3839
Type: `String`
3940

4041

41-
### to
42+
#### to
4243
The address you’re sending to.
4344

4445
Type: `String`
4546

4647

47-
### subject
48+
#### subject
4849

4950
Type: `String`
5051

5152

52-
## Examples
53+
### Examples
5354

54-
### Options specified through target
55+
#### Options specified through target
5556

5657
```javascript
5758
grunt.initConfig({
@@ -67,7 +68,7 @@ grunt.initConfig({
6768
});
6869
```
6970

70-
### Specify options through targets or globally
71+
#### Specify options through targets or globally
7172
Options specified through a target will always take precedence over global options.
7273

7374
```javascript
@@ -89,4 +90,181 @@ grunt.initConfig({
8990
}
9091
}
9192
});
93+
```
94+
95+
## Postmark templates task
96+
_Run this task with the `grunt postmark-templates` command._
97+
98+
The `postmark-templates` task is an alias of the `postmark-templates-from-targets` task which is itself a two stepped task – `postmark-templates-upload` followed by `postmark-templates-output`.
99+
100+
`postmark-templates` (`postmark-templates-from-targets`) is intended for programmatic usage from other grunt tasks.
101+
102+
### `postmark-templates-upload` Targets
103+
104+
#### name
105+
The name of your template.
106+
107+
Type: `String`
108+
109+
110+
#### subject
111+
The subject line of your template.
112+
113+
Type: `String`
114+
115+
#### htmlSrc
116+
A path to the generated HTML for your template. *Not used if `htmlBody` is specified.*
117+
118+
Type: `String`
119+
120+
121+
#### textSrc
122+
A path to the generated plain text for your template. *Not used if `textBody` is specified.*
123+
124+
Type: `String`
125+
126+
#### htmlBody
127+
The generated HTML content of your template. *Not required if `htmlSrc` is specified.*
128+
129+
Type: `String`
130+
131+
132+
#### textBody
133+
The generated plain text content of your template. *Not required if `textSrc` is specified.*
134+
135+
Type: `String`
136+
137+
138+
### `postmark-templates-upload` Options
139+
140+
#### serverToken
141+
Your server token can be found on your server’s credentials page on [Postmark’s](http://postmarkapp.com) app.
142+
143+
Type: `String`
144+
145+
146+
#### ephemeralUploadResultsProperty
147+
This is the name of a temporary grunt task configuration property used to communicate the upload results between `postmark-templates-upload` and `postmark-templates-output` without having to write a temporary file. **This should be the same value as `ephemeralUploadResultsProperty` for `postmark-templates-output`.**
148+
149+
Type: `String`
150+
151+
152+
### `postmark-templates-output` Options
153+
154+
#### outputFile
155+
The name of a file to output the results of the upload to Postmark.
156+
157+
Type: `String`
158+
159+
160+
#### cleanOutput
161+
If `true`, do not export `htmlBody`, `htmlSrc`, `textBody` or `textSrc` in the specified `outputFile`.
162+
163+
Type: `Boolean`
164+
165+
166+
#### ephemeralUploadResultsProperty
167+
This is the name of a temporary grunt task configuration property used to communicate the upload results between `postmark-templates-upload` and `postmark-templates-output` without having to write a temporary file. **This should be the same value as `ephemeralUploadResultsProperty` for `postmark-templates-upload`.**
168+
169+
Type: `String`
170+
171+
172+
### Example
173+
174+
```javascript
175+
grunt.initConfig({
176+
'postmark-templates-upload': {
177+
options: {
178+
serverToken: 'POSTMARK_API_TEST',
179+
ephemeralUploadResultsProperty: 'temp'
180+
},
181+
test_email: {
182+
name: 'testing-postmark-templates-js1',
183+
subject: 'Testing grunt-postmark-templates',
184+
htmlSrc: 'test/email.html',
185+
textSrc: 'test/email.txt'
186+
},
187+
test_email_inline_body: {
188+
name: 'testing-postmark-templates-js3',
189+
subject: 'Testing grunt-postmark-templates (inline body)',
190+
htmlBody: '<html><body><h1>Another email test</h1></body></html>',
191+
textBody: 'Hello from grunt-postmark-templates\n'
192+
}
193+
},
194+
'postmark-templates-output': {
195+
options: {
196+
cleanOutput: true,
197+
outputFile: 'templates.json',
198+
ephemeralUploadResultsProperty: 'temp'
199+
}
200+
}
201+
});
202+
```
203+
204+
## Postmark templates (from file) task
205+
_Run this task with the `grunt postmark-templates-from-file` command._
206+
207+
The `postmark-templates-from-file` task invokes the `postmark-templates` task with targets read from a JSON file (via `postmark-templates-parse`).
208+
209+
This task is intended for standalone, manual usage.
210+
211+
212+
### `postmark-templates-parse` Options
213+
214+
#### inputFile
215+
The name of a file that specifies templates for uploading to Postmark. These templates take the same shape as defined by `postmark-templates-upload`. This should usually be the same value as `outputFile` for `postmark-templates-output`.
216+
217+
Type: `String`
218+
219+
220+
### Example
221+
222+
In your `Gruntfile`:
223+
224+
```javascript
225+
grunt.initConfig({
226+
'postmark-templates-parse': {
227+
options: {
228+
inputFile: 'templates.json'
229+
}
230+
},
231+
'postmark-templates-upload': {
232+
options: {
233+
serverToken: 'POSTMARK_API_TEST',
234+
ephemeralUploadResultsProperty: 'temp'
235+
}
236+
},
237+
'postmark-templates-output': {
238+
options: {
239+
cleanOutput: true,
240+
outputFile: 'templates.json',
241+
ephemeralUploadResultsProperty: 'temp'
242+
}
243+
}
244+
});
245+
```
246+
247+
In the file specified by `inputFile`, in this case, `templates.json`:
248+
249+
```json
250+
{
251+
"test_email": {
252+
"name": "testing-postmark-templates-js1",
253+
"subject": "Testing grunt-postmark-templates",
254+
"htmlSrc": "test/email.html",
255+
"textSrc": "test/email.txt"
256+
},
257+
"test_email_again": {
258+
"name": "testing-postmark-templates-js2",
259+
"subject": "Testing grunt-postmark-templates (again)",
260+
"htmlSrc": "test/email.html",
261+
"textSrc": "test/email.txt"
262+
},
263+
"test_email_inline_body": {
264+
"name": "testing-postmark-templates-js3",
265+
"subject": "Testing grunt-postmark-templates (inline body)",
266+
"htmlBody": "<html><body><h1>Another email test</h1></body></html>",
267+
"textBody": "Hello from grunt-postmark-templates\n"
268+
}
269+
}
92270
```

example_config.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// 1. Copy this file to "config.json" for your own version that won't be
2+
// tracked in source control.
3+
//
4+
// 2. Delete these comments from the configuration so Grunt doesn't get confused
5+
{
6+
"postmark": {
7+
"from": "[email protected]",
8+
9+
"subject": "Test Email"
10+
},
11+
"templates": {
12+
"ephemeralUploadResultsProperty": "postmark-templates-upload-results",
13+
"cleanOutput": true,
14+
"file": "templates.json"
15+
}
16+
}

example_secrets.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// 1. Copy this file to "secrets.json" for your own version that won't be
2+
// tracked in source control.
3+
//
4+
// 2. Delete these comments from the configuration so Grunt doesn't get confused
5+
{
6+
"postmark": {
7+
"server_token": "YOUR_POSTMARK_SERVER_TOKEN_HERE"
8+
}
9+
}

example_templates.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"test_email": {
3+
"name": "testing-postmark-templates-js1",
4+
"subject": "Testing grunt-postmark-templates",
5+
"htmlSrc": "test/email.html",
6+
"textSrc": "test/email.txt"
7+
},
8+
"test_email_again": {
9+
"name": "testing-postmark-templates-js2",
10+
"subject": "Testing grunt-postmark-templates (again)",
11+
"htmlSrc": "test/email.html",
12+
"textSrc": "test/email.txt"
13+
},
14+
"test_email_inline_body": {
15+
"name": "testing-postmark-templates-js3",
16+
"subject": "Testing grunt-postmark-templates (inline body)",
17+
"htmlBody": "<html><body><h1>Another email test</h1></body></html>",
18+
"textBody": "Hello from grunt-postmark-templates\n"
19+
}
20+
}

0 commit comments

Comments
 (0)