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