MeteorJS: Generating emails from templates server-side
Asked Answered
Y

3

9

I need to send emails from MeteorJS application and I want to generate them using html templates, not by "html-in-js" stuff.
What I've tried to do:
1) Use Template.emailTemplate(data), but Template is not defined server-side.
2) Save my email templates as *.html files under <app>/server/email/templates directory, get their contents using fs.readSync() and then compile/render it using meteor's built-in handlebars package.
This works fine in development environment, but fails in production using bundled app because of *.html files under server directory are not bundled. Besides, the structure of directories is changed during bundle process and relative paths to templates become invalid.
3) Your proposals? =)

Ybarra answered 13/5, 2013 at 20:11 Comment(0)
E
14

Currently, templates are not supported server-side. That functionality is coming. In the mean time, I created a package you might find useful called handlebars-server that allows you to use Handlebars on the server. You can use the package with atmosphere or by copying the project directory into your packages folder. Here is an example:

Example:

my-email.handlebars

Hello, {{name}}

server.js

Email.send({
  html: Handlebars.templates['my-email']({ name: 'Chris' })
});

Note

No templates in the handlebars file. Just put your html and Handlebars expressions. The file will get compiled into a function and assigned to a property on the Handlebars.templates object. The property name will be the name of the file minus the handlebars extension.

Github

https://github.com/eventedmind/meteor-handlebars-server

Edgerton answered 14/5, 2013 at 22:28 Comment(1)
Is that HTML-agnostic? That is, can I use it for plain-text templates as well? Thanks!Jarredjarrell
B
4

Another option now is to use the server side 'private' directory to read resources out of and use them to store resources your application will use.

create the meteor project, and then create a /private directory.

Place your templates in there (You should use the meteor-handlebars-server package instead if you need handlebars)

Read in your template with:

Assets.getText(assetPath, [asyncCallback]);

Obviously you can also do pattern matching regex/replace against the string once it's loaded.

example:

var template = Assets.getText(assetPath);  // Synchronous
var username = 'John Doe';

template = template.replace('{{username}}', username);
Email.send({
  html: template
});

For more info on the assets functionality: Meteor Assets

Bucktooth answered 22/11, 2013 at 21:0 Comment(0)
M
1

Meteor 0.8.*, here is another solution.

https://gist.github.com/fpoirier1/534bf5db69ece2c83205

Mahatma answered 11/6, 2014 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.