Skip to content

HTTPS clone URL

Subversion checkout URL

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