nodemailer help!
-
Hi guys, can you help me?
I want to send a file excel (.xlsx) by mail (nodemailer), but I see this error.function beforeRender(req, res, done) { require('request')({ url:"http://#######/detalle.js", json:true }, function(err, response, body){ console.log(JSON.stringify(body)) req.data = { detalle: body }; done(); }); return; } function afterRender(req, res, done) { var mailer = require("nodemailer"); var smtpTransport = mailer.createTransport('smtps://Joseantonio%40gmail.com:###@smtp.gmail.com'); var mail = { from: "Jose <@gmail.com>", to: "josesito@gmail.com", subject: "Enviado desde Jsreport", text: "Reporte ", html: "<b>Reporte excel</b>", attachments: [ { filename: 'Reporte.xlsx', content: new Buffer(res.content) }], } smtpTransport.sendMail(mail, function(error, response){ smtpTransport.close(); if(error){ return done(error); } return done(); }); }
---------This error appears
Error while executing templating engine. Cannot read property 'xl/worksheets/sheet2.xml' of undefined. Error on line 199:42. 197 | 198 | function add (filePath, xmlPath) { > 199 | var obj = this.ctx.root.$xlsxTemplate[filePath] | ^ 200 | var collection = safeGet(obj, xmlPath) 201 | 202 | var xml = escape(this.tagCtx.render(this.ctx.data).trim(), this.ctx.root) TypeError: Cannot read property 'xl/worksheets/sheet2.xml' of undefined at Array.add (evaluate-template-engine-helpers.js:199:42) at Array.<anonymous> (evaluate-template-engine-helpers.js:429:17) at eval (eval at createFunctionContext (/home/marlon/jsreportapp/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:257:23), <anonymous>:5:94) at prog (/home/marlon/jsreportapp/node_modules/handlebars/dist/cjs/handlebars/runtime.js:221:12) at execIteration (/home/marlon/jsreportapp/node_modules/handlebars/dist/cjs/handlebars/helpers/each.js:51:19) at Object.<anonymous> (/home/marlon/jsreportapp/node_modules/handlebars/dist/cjs/handlebars/helpers/each.js:80:11) at Object.eval [as main] (eval at createFunctionContext (/home/marlon/jsreportapp/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:257:23), <anonymous>:5:34) at main (/home/marlon/jsreportapp/node_modules/handlebars/dist/cjs/handlebars/runtime.js:175:32) at ret (/home/marlon/jsreportapp/node_modules/handlebars/dist/cjs/handlebars/runtime.js:178:12) at ret (/home/marlon/jsreportapp/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:526:21)
-
hi! it looks like your template is having an error when executing the xlsx recipe, what is the content of the template? can you share on the playground? first, try to comment the script that you are using (the two functions beforeRender/afterRender) and try to render, you should verify first that your template is able to finish normally. it looks like that error is present even if you don't send the email so you have to solve that first.
-
Do you have an example of sending mail with an excel file?
-
Do you have an example of sending mail with an excel file?
the code that you are using for sending the email looks fine, but the error that you are getting seems that it is not related to the email at all.. as i said it seems that error is present in the rendering without the script that sends the email, so you should first verify that the template works without the script
-
if it works, and send mail
when I open this file I get the message "excel can not open the file because the format or extension of this are not valid"
-
ok, i think i know what is happening.. when you render from the studio the response is modified a bit in order for you to be able to see in browser preview.
you can add this to your
beforeRender
function:function beforeRender(req, res, done) { // THIS CONFIGURES THAT THE RESPONSE SHOULD NOT BE MODIFIED IN ORDER TO DISPLAY IN BROWSER req.options = req.options || {} req.options.preview = false require('request')({ url:"http://#######/detalle.js", json:true }, function(err, response, body){ console.log(JSON.stringify(body)) req.data = { detalle: body }; done(); }); return; }
if it works then you should add something to your
afterRender
function so it does not send the email when you render the template from the studio, because likely when you are working with studio you just want to see the final xlsx and don't send the emailfunction afterRender(req, res, done) { // THIS CONDITION WILL PREVENT SENDING EMAIL WHEN YOU RENDER THE TEMPLATE FROM STUDIO // AND WHEN YOU RENDER THE TEMPLATE USING THE HTTP API THEN THE EMAIL WILL BE SENT NORMALLY if (req.options && req.options.preview) { return done() } var mailer = require("nodemailer"); var smtpTransport = mailer.createTransport('smtps://Joseantonio%40gmail.com:###@smtp.gmail.com'); var mail = { from: "Jose <@gmail.com>", to: "josesito@gmail.com", subject: "Enviado desde Jsreport", text: "Reporte ", html: "<b>Reporte excel</b>", attachments: [ { filename: 'Reporte.xlsx', content: new Buffer(res.content) }], } smtpTransport.sendMail(mail, function(error, response){ smtpTransport.close(); if(error){ return done(error); } return done(); }); }
-
Thank you very much, you saved me, that was the problem. Thank you