7
7
8
8
TAG = ' mailer'
9
9
10
+ CONTENT_TYPES = {
11
+ html : ' text/html' ,
12
+ text : ' text/plain'
13
+ }
14
+
10
15
# ## Setup
11
16
12
17
# Main exported symbol with some initial settings:
@@ -269,13 +274,12 @@ MailerClass = (options) ->
269
274
# This function adds the `preview` route from a `template` object.
270
275
# It will apply the returned data from a `data` function on the
271
276
# provided `route` prop from the template.
272
- previewAction = (opts , template , params ) ->
273
- check opts,
274
- type : Match .OneOf (' html' , ' text' )
277
+ previewAction = (type ) ->
278
+ check type, Match .OneOf (' html' , ' text' )
275
279
276
- return (template , params ) ->
280
+ return (req , res , params , template ) ->
277
281
try
278
- data = template .route .data and template .route .data .call (this , params)
282
+ data = template .route .data and template .route .data .call (res , params)
279
283
catch ex
280
284
msg = ' '
281
285
exception = ' Exception in ' + template .name + ' data function: ' + ex .message
@@ -291,29 +295,29 @@ MailerClass = (options) ->
291
295
msg = exception
292
296
293
297
Utils .Logger .error (msg, TAG)
294
- this .writeHead (500 )
295
- return this .end (msg)
298
+ res .writeHead (500 )
299
+ res .end (msg)
296
300
297
301
# Compile, since we wanna refresh markup and CSS inlining.
298
302
compile template
299
303
300
- Utils .Logger .info " Rendering #{ template .name } as #{ opts . type } ..." , TAG
304
+ Utils .Logger .info " Rendering #{ template .name } as #{ type} ..." , TAG
301
305
302
306
try
303
307
html = render template .name , data
304
- content = if opts . type is ' html' then html else toText (html)
308
+ content = if type is ' html' then html else toText (html)
305
309
Utils .Logger .info " Rendering successful!" , TAG
306
310
catch ex
307
311
msg = ' Could not preview email: ' + ex .message
308
312
Utils .Logger .error msg, TAG
309
313
content = msg
310
314
311
- @ writeHead 200 , ' Content-Type' : if opts . type is ' html ' then ' text/html ' else ' text/plain '
312
- @ end (content, ' utf8' )
315
+ res . writeHead 200 , ' Content-Type' : CONTENT_TYPES[ type]
316
+ res . end (content, ' utf8' )
313
317
314
318
# This function adds the `send` route, for easy sending emails from
315
319
# the browser.
316
- sendAction = (template , params ) ->
320
+ sendAction = (req , res , params , template ) ->
317
321
# Who to send to? It depends: it primarly reads from the `?to`
318
322
# query param, and secondly from the `testEmail` prop in settings.
319
323
to = params .query .to or settings .testEmail
@@ -322,42 +326,42 @@ MailerClass = (options) ->
322
326
323
327
if to?
324
328
try
325
- data = template .route .data and template .route .data .call (this , params)
329
+ data = template .route .data and template .route .data .call (res , params)
326
330
catch ex
327
331
Utils .Logger .error ' Exception in ' + template .name + ' data function: ' + ex .message , TAG
328
332
return
329
333
330
- res = sendEmail (
334
+ result = sendEmail (
331
335
to : to
332
336
data : data
333
337
template : template .name
334
338
subject : ' [TEST] ' + template .name
335
339
)
336
340
337
- if res is false
338
- @response .writeHead 500
341
+ if result is false
342
+ res .writeHead 500
339
343
msg = ' Did not send test email, something went wrong. Check the logs.'
340
344
else
341
- @ writeHead 200
345
+ res . writeHead 200
342
346
# If there's no `MAIL_URL` environment variable, Meteor cannot send
343
347
# the email and echoes it out to `STDOUT` instead.
344
348
reallySentEmail = !! process .env .MAIL_URL
345
349
msg = if reallySentEmail then " Sent test email to #{ to} " else " Sent email to STDOUT"
346
350
347
- @ end (msg)
351
+ res . end (msg)
348
352
349
353
else
350
- @ writeHead 400
351
- @ end (" No testEmail provided." )
354
+ res . writeHead 400
355
+ res . end (' No testEmail provided.' )
352
356
353
357
# Adds all the routes from a template.
354
358
addRoutes = (template ) ->
355
359
check template .name , String
356
360
check template .route .path , String
357
361
358
362
types =
359
- preview : previewAction (type : ' html' )
360
- text : previewAction (type : ' text' )
363
+ preview : previewAction (' html' )
364
+ text : previewAction (' text' )
361
365
send : sendAction
362
366
363
367
_ .each types, (action , type ) ->
@@ -378,7 +382,7 @@ MailerClass = (options) ->
378
382
379
383
# we use Picker for server side routes
380
384
Picker .route (path, (params , req , res ) ->
381
- action . call res, template, params
385
+ action (req, res, params, template)
382
386
)
383
387
384
388
0 commit comments