Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 368 lines (295 sloc) 9.576 kB
a340aa9 @niftylettuce Release 0.0.1
authored
1
897c58b @niftylettuce Removed requirement for style.css and text.ejs, closes #1
authored
2 # node-email-templates <sup>0.0.3</sup>
a340aa9 @niftylettuce Release 0.0.1
authored
3
4 Node.js module for rendering beautiful emails with [ejs][1] templates and email-friendly inline CSS using [juice][2].
5
897c58b @niftylettuce Removed requirement for style.css and text.ejs, closes #1
authored
6 **New in v0.0.3**:
7 * Removed requirement for `style.css` and `text.ejs` files with compatibility in `node` v0.6.x to v0.8.x (utilizes `path.exists` vs. `fs.exists` respectively).
bb43563 @niftylettuce Added installation line to Readme
authored
8
686ff86 @niftylettuce Added link to email template resources
authored
9 ## Email Templates
a340aa9 @niftylettuce Release 0.0.1
authored
10
686ff86 @niftylettuce Added link to email template resources
authored
11 For professional and customizable email templates, please visit <https://github.com/mailchimp/Email-Blueprints>.
a340aa9 @niftylettuce Release 0.0.1
authored
12
bb43563 @niftylettuce Added installation line to Readme
authored
13
14
15 ## Installation
16
0eb914a @niftylettuce Windows support
authored
17 ### Unix/OS X
18
bb43563 @niftylettuce Added installation line to Readme
authored
19 ```bash
20 npm install email-templates
21 ```
22
0eb914a @niftylettuce Windows support
authored
23 ### Windows
24
25 ```bash
26 npm install email-templates-windows
27 ```
bb43563 @niftylettuce Added installation line to Readme
authored
28
29
a340aa9 @niftylettuce Release 0.0.1
authored
30 ## Quick start
31
b4f4f22 @niftylettuce Fixed typo
authored
32 1. Install the module for your respective project `npm install email-templates`.
a340aa9 @niftylettuce Release 0.0.1
authored
33 2. Create a folder called `templates` inside your root directory (or elsewhere).
34 3. For each of your templates, respectively name and create a folder inside the `templates` folder.
35 4. Add the following files inside the template's folder:
897c58b @niftylettuce Removed requirement for style.css and text.ejs, closes #1
authored
36 * `html.ejs` - html + [ejs][1] version of your email template (**required**)
37 * `text.ejs` - text + [ejs][1] version of your email template (**optional**)
38 * `style.css` - stylesheet for the template, which will render `html.ejs` with inline CSS (**optional**)
a340aa9 @niftylettuce Release 0.0.1
authored
39 5. Utilize one of the examples below for your respective email module and start sending beautiful emails!
40
41
d30964b @niftylettuce Added documentation for overriding EJS defaults
authored
42 # EJS Custom Tags
43
44 Want to use different opening and closing tags instead of the EJS's default `<%` and `%>`?.
45
46 ```js
47 ...
bf5ac4a @niftylettuce Fixed typo in documentation
authored
48 emailTemplates(templatesDir, { open: '{{', close: '}}' }, function(err, template) {
d30964b @niftylettuce Added documentation for overriding EJS defaults
authored
49 ...
50 ```
a340aa9 @niftylettuce Release 0.0.1
authored
51
b5d466e @niftylettuce Added note about other options for EJS
authored
52 **NOTE**: You can also pass <a href="https://github.com/visionmedia/ejs#options" target="_blank">other options from EJS's documentation</a>.
53
a340aa9 @niftylettuce Release 0.0.1
authored
54 # Usage
55
0375445 @niftylettuce Fixed typo
authored
56 Render a template for a single email or render multiple (having only loaded the template once).
a340aa9 @niftylettuce Release 0.0.1
authored
57
58 ```js
59 var path = require('path')
5798487 @niftylettuce Fixed typo thanks to @samholmes tip
authored
60 , templatesDir = path.join(__dirname, 'templates')
a340aa9 @niftylettuce Release 0.0.1
authored
61 , emailTemplates = require('email-templates');
62
87837bd @niftylettuce typo
authored
63 emailTemplates(templatesDir, function(err, template) {
a340aa9 @niftylettuce Release 0.0.1
authored
64
65 // Render a single email with one template
66 var locals = { pasta: 'Spaghetti' };
87837bd @niftylettuce typo
authored
67 template('pasta-dinner', locals, function(err, html, text) {
a340aa9 @niftylettuce Release 0.0.1
authored
68 // ...
69 });
70
71 // Render multiple emails with one template
72 var locals = [
73 { pasta: 'Spaghetti' },
74 { pasta: 'Rigatoni' }
75 ];
76 var Render = function(locals) {
77 this.locals = locals;
78 this.send = function(err, html, text) {
79 // ...
80 };
81 this.batch = function(batch) {
82 batch(this.locals, this.send);
83 };
84 };
85 template('pasta-dinner', true, function(err, batch) {
86 for(var user in users) {
87 var render = new Render(users[user]);
88 render.batch(batch);
89 }
90 });
91
92 });
93 ```
94
95
96
670942e @niftylettuce Fixed typos in Readme
authored
97 ## Example with [Nodemailer][3]
a340aa9 @niftylettuce Release 0.0.1
authored
98
99 ```js
100 var path = require('path')
101 , templatesDir = path.resolve(__dirname, '..', 'templates')
102 , emailTemplates = require('email-templates')
103 , nodemailer = require('nodemailer');
104
105 emailTemplates(templatesDir, function(err, template) {
106
107 if (err) {
108 console.log(err);
109 } else {
110
111 // ## Send a single email
112
113 // Prepare nodemailer transport object
114 var transport = nodemailer.createTransport("SMTP", {
115 service: "Gmail",
116 auth: {
117 user: "some-user@gmail.com",
118 pass: "some-password"
119 }
120 });
121
122 // An example users object with formatted email function
123 var locals = {
124 email: 'mamma.mia@spaghetti.com',
125 name: {
126 first: 'Mamma',
127 last: 'Mia'
128 }
129 };
130
131 // Send a single email
132 template('newsletter', locals, function(err, html, text) {
133 if (err) {
134 console.log(err);
135 } else {
be53bd1 @niftylettuce Fixed a minor typo
authored
136 transport.sendMail({
a340aa9 @niftylettuce Release 0.0.1
authored
137 from: 'Spicy Meatball <spicy.meatball@spaghetti.com>',
138 to: locals.email,
139 subject: 'Mangia gli spaghetti con polpette!',
140 html: html,
141 // generateTextFromHTML: true,
142 text: text
143 }, function(err, responseStatus) {
144 if (err) {
145 console.log(err);
146 } else {
147 console.log(responseStatus.message);
148 }
149 });
150 }
151 });
152
153
154 // ## Send a batch of emails and only load the template once
155
156 // Prepare nodemailer transport object
157 var transportBatch = nodemailer.createTransport("SMTP", {
158 service: "Gmail",
159 auth: {
160 user: "some-user@gmail.com",
161 pass: "some-password"
162 }
163 });
164
165 // An example users object
166 var users = [
167 {
168 email: 'pappa.pizza@spaghetti.com',
169 name: {
170 first: 'Pappa',
171 last: 'Pizza'
172 }
173 },
174 {
175 email: 'mister.geppetto@spaghetti.com',
176 name: {
177 first: 'Mister',
178 last: 'Geppetto'
179 }
180 }
181 ];
182
183 // Custom function for sending emails outside the loop
184 //
185 // NOTE:
186 // We need to patch postmark.js module to support the API call
187 // that will let us send a batch of up to 500 messages at once.
188 // (e.g. <https://github.com/diy/trebuchet/blob/master/lib/index.js#L160>)
189 var Render = function(locals) {
190 this.locals = locals;
191 this.send = function(err, html, text) {
192 if (err) {
193 console.log(err);
194 } else {
195 transportBatch.sendMail({
196 from: 'Spicy Meatball <spicy.meatball@spaghetti.com>',
197 to: locals.email,
198 subject: 'Mangia gli spaghetti con polpette!',
199 html: html,
200 // generateTextFromHTML: true,
201 text: text
202 }, function(err, responseStatus) {
203 if (err) {
204 console.log(err);
205 } else {
206 console.log(responseStatus.message);
207 }
208 });
209 }
210 };
211 this.batch = function(batch) {
212 batch(this.locals, this.send);
213 };
214 };
215
216 // Load the template and send the emails
217 template('newsletter', true, function(err, batch) {
218 for(var user in users) {
219 var render = new Render(users[user]);
220 render.batch(batch);
221 }
222 });
223
224 }
225 });
226 ```
227
228
229
670942e @niftylettuce Fixed typos in Readme
authored
230 ## Example with [Postmark App][4] (utilizing [Postmark.js][5])
a340aa9 @niftylettuce Release 0.0.1
authored
231
232 **NOTE**: Did you know `nodemailer` can also be used to send SMTP email through Postmark? See [this section][6] of their Readme for more info.
233
234 For more message format options, see [this section][7] of Postmark's developer documentation section.
235
236 ```js
237 var path = require('path')
238 , templatesDir = path.resolve(__dirname, '..', 'templates')
239 , emailTemplates = require('email-templates')
240 , postmark = require('postmark')('your-api-key');
241
242 emailTemplates(templatesDir, function(err, template) {
243
244 if (err) {
245 console.log(err);
246 } else {
247
248 // ## Send a single email
249
250 // An example users object with formatted email function
251 var locals = {
252 email: 'mamma.mia@spaghetti.com',
253 name: {
254 first: 'Mamma',
255 last: 'Mia'
256 }
257 };
258
259 // Send a single email
260 template('newsletter', locals, function(err, html, text) {
261 if (err) {
262 console.log(err);
263 } else {
264 postmark.send({
265 From: 'Spicy Meatball <spicy.meatball@spaghetti.com>',
266 To: locals.email,
267 Subject: 'Mangia gli spaghetti con polpette!',
268 HtmlBody: html,
269 TextBody: text
270 }, function(err, response) {
271 if (err) {
272 console.log(err.status);
273 console.log(err.message);
274 } else {
275 console.log(response);
276 }
277 });
278 }
279 });
280
281
282 // ## Send a batch of emails and only load the template once
283
284 // An example users object
285 var users = [
286 {
287 email: 'pappa.pizza@spaghetti.com',
288 name: {
289 first: 'Pappa',
290 last: 'Pizza'
291 }
292 },
293 {
294 email: 'mister.geppetto@spaghetti.com',
295 name: {
296 first: 'Mister',
297 last: 'Geppetto'
298 }
299 }
300 ];
301
302 // Custom function for sending emails outside the loop
303 //
304 // NOTE:
305 // We need to patch postmark.js module to support the API call
306 // that will let us send a batch of up to 500 messages at once.
307 // (e.g. <https://github.com/diy/trebuchet/blob/master/lib/index.js#L160>)
308 var Render = function(locals) {
309 this.locals = locals;
310 this.send = function(err, html, text) {
311 if (err) {
312 console.log(err);
313 } else {
314 postmark.send({
315 From: 'Spicy Meatball <spicy.meatball@spaghetti.com>',
316 To: locals.email,
317 Subject: 'Mangia gli spaghetti con polpette!',
318 HtmlBody: html,
319 TextBody: text
320 }, function(err, response) {
321 if (err) {
322 console.log(err.status);
323 console.log(err.message);
324 } else {
325 console.log(response);
326 }
327 });
328 }
329 };
330 this.batch = function(batch) {
331 batch(this.locals, this.send);
332 };
333 };
334
335 // Load the template and send the emails
336 template('newsletter', true, function(err, batch) {
337 for(user in users) {
338 var render = new Render(users[user]);
339 render.batch(batch);
340 }
341 });
342
343 }
344 });
345 ```
346
347
348
349 ## Contributors
350
351 * Nick Baugh <niftylettuce@gmail.com>
352
353
354
355 ## License
356
357 MIT Licensed
358
359
360
670942e @niftylettuce Fixed typos in Readme
authored
361 [1]: https://github.com/visionmedia/ejs
362 [2]: https://github.com/LearnBoost/juice
a340aa9 @niftylettuce Release 0.0.1
authored
363 [3]: https://github.com/andris9/Nodemailer
364 [4]: http://postmarkapp.com/
365 [5]: https://github.com/voodootikigod/postmark.js
366 [6]: https://github.com/andris9/Nodemailer#well-known-services-for-smtp
367 [7]: http://developer.postmarkapp.com/developer-build.html#message-format
Something went wrong with that request. Please try again.