Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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