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.64k stars 339 forks source link

[fix] Unable to send dynamic template from DB #442

Closed sumitpatel93 closed 1 year ago

sumitpatel93 commented 1 year ago

I am rendering the template from DB ( MongoDB )

Node.js version: 16

OS version: MacOS

Description: I want to send dynamic templates after fetching it from the database, but they are not being sent to the corresponding email. I think I am missing something on the syntax for rendering dynamic emails from DB

Actual behaviour

It should fetch the template from DB with its respective name and should be sent to the corresponding email The code works fine when the msg stored in DB is plain text, but with ejs/pug it's failing to send mail.

Template Db schema { _id : 23123, templateName : hello templateBody : <Template in Ejs format> }

Code to reproduce

Gist - https://gist.github.com/sumitpatel93/4560cb3760e99e0f2a63497dc51d04bf

Checklist

titanism commented 1 year ago

Your code at https://gist.github.com/sumitpatel93/4560cb3760e99e0f2a63497dc51d04bf is mixing usage of async/await with callbacks, which is not supported and is bad code practice (it's an anti-pattern). It looks like we need to update our example on our side to support better code practice.

Also, you have a bug in your code at https://gist.github.com/sumitpatel93/4560cb3760e99e0f2a63497dc51d04bf#file-sendemail-js-L27-L31. You can see that you do not resolve for the case when there is !template found.