Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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