Skip to content

HTTPS clone URL

Subversion checkout URL

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