Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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