Question: How can I dynamically populate html data in node for posting, not displaying?

Question

How can I dynamically populate html data in node for posting, not displaying?

Answers 1
Added at 2016-12-29 20:12
Tags
Question

I'm currently trying to populate html data in a file stream so that I can send it in an email using smtpTransport. I've tried to use ejs but almost every example I'm seeing is using ejs.render to display the content while I'm trying to further send it along as a string. I want to do this manually versus using something like mail chimp but I haven't found anything too helpful yet. For context, I'm trying to create a receipt email when a user buys something and populate a table with the items. Here's the code that I've been trying to use with ejs but get a "TypeError: this.templateText.replace is not a function error":

router.post('/placeorder', function(req, res) {
    var orderInfo = req.body;

    var delivery = orderInfo.deliveryDate.split("00:00:00")[0] + " from " + orderInfo.deliveryTime;

    //Email buyer receipt
    fs.readFile('buyer_receipt.html',function (err, data){
        if (err) {
            console.log(err);
        } else {
            var renderedHtml = ejs.render(data, {cart: orderInfo.cart, subtotal: orderInfo.subtotal, fee: orderInfo.fee, total: orderInfo.total, deliveryInfo: delivery});

            smtpTransport.sendMail({
                from: "Test <myemail@gmail.com>",
                to: orderInfo.user.user.ownerName + " <" + orderInfo.user.user.email +">",
                subject: "Thanks for your order!",
                html: renderedHtml
            }, function(error, response){
                if(error) {
                    console.log(error);
                } else {
                    console.log("Mail sent: " + response.message);
                }
            });
    }
});

I'm open to using a different platform outside ejs as well.

Answers to

How can I dynamically populate html data in node for posting, not displaying?

nr: #1 dodano: 2016-12-29 20:12

Comment ejs.render use data.replace to replace varible cart , subtotal, fee etc.

Source Show
◀ Wstecz