forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.66k stars 339 forks source link

pug template is working fine but not handlebars #408

Closed mcajkovs closed 3 years ago

mcajkovs commented 3 years ago

I've created similar structure as stated in documentation but I've added handlebars template into it:

├── app.js
├── mars
│   ├── html.pug
│   └── subject.pug
└── venus
    ├── html.hbs
    └── subject.hbs

Content of those hbs files is following:

html.hbs

Hi {{name}}
Welcome to Venus, the red planet.

subject.hbs

Hi {{name}}, welcome to Venus

My code is copied from mentioned documentation (except for mars template I'm using venus):

const Email = require('email-templates');

const email = new Email({
  message: {
    from: 'niftylettuce@gmail.com'
  },
  // uncomment below to send emails in development/test env:
  // send: true
  transport: {
    jsonTransport: true
  }
});

email
  .send({
    template: 'venus',
    message: {
      to: 'elon@spacex.com'
    },
    locals: {
      name: 'Elon'
    }
  })
  .then(console.log)
  .catch(console.error);

I've installed both pug and handlebars: npm install email-templates pug handlebars

mars template is working fine, but when I try to use venus I get following error

Error: No content was passed for subject, html, text, nor attachments message props. Check that the files for the template "venus" exist.
    at Email.renderAll (/home/mcajkovs/email-templates/node_modules/email-templates/lib/index.js:279:275)
    at async Email.send (/home/mcajkovs/email-templates/node_modules/email-templates/lib/index.js:302:20)

what I'm doing wrong? Thanks

niftylettuce commented 3 years ago

You have to set the views option (see https://github.com/forwardemail/email-templates#custom-template-engine-eg-ejs).

Change you code to this:

const email = new Email({
  message: {
    from: 'niftylettuce@gmail.com'
  },
  // uncomment below to send emails in development/test env:
  // send: true
  transport: {
    jsonTransport: true
-  }
+  },
+  views: {
+    options: {
+      extension: 'hbs' // <---- HERE
+    }
+  }
});