Jsreport with nodemailer-express-handlebars



  • I use jsreport with nodemailer and nodemailer-express-handlebars to create html email. However in the afterRender script, the script could not access template.handlebar when configuring, it always yield an error: Error: EISDIR: illegal operation on a directory, read. Do anyone how can I access file from script?
    Thanks in advance



  • I have also tried fs to read the file, but got the same error???



  • The error says you are trying to read the directory where the file is expected.
    Please share your code in the afterRender.



  • I tried the example here "https://writejavascript.com/blog/sending-emails-with-node/". I set up the structure of directory as shown in the code and it can access the directory but always yield error: Error: EISDIR: illegal operation on a directory, read. The code as shown below:

    function afterRender(req, res, done) {
        console.log('After rendered report.....................');
        console.log(req.data);
        
        var mailer = require("nodemailer");
    
        var hbs = require("nodemailer-express-handlebars");
    
    
    
        var smtpTransport = mailer.createTransport({
            host : "mail.xxx.no",
            port : 465,
            secure: true,
            auth : {
                user : "noreply@xxx.no",
                pass : "xxx"
            },
            tls : {
                rejectUnauthorized: false
            }
    
        });
    
        var options = {
            viewEngine : {
                extname: ".hbs",
                partialsDir: './data/etakeaway/Invoice/views',
                layoutsDir: './data/etakeaway/Invoice/views/layouts'
            },
            viewPath : './data/etakeaway/Invoice/views',
            extName: ".hbs"
    
        };
    
        smtpTransport.use('compile', hbs(options));
    
        var mail = {
            from: "XXX <noreply@xxx.no>",
            to: req.data.email,
            subject: "Receipt for order id " + req.data.orderId,
            text: "See the attached report",
            template: 'template',
            context: {
                name : 'Name',
                orderId : '#123456'
            },
            attachments: [
            {  
                filename: 'Receipt.pdf',
                content: new Buffer(res.content)
            }],
        }
    
        smtpTransport.sendMail(mail, function(error, response){
            smtpTransport.close();
            if(error){
                return done(error);
            }
    
            return done();
        });
    }
    


  • I think that the error is not that of trying to read directory instead for a file. I have also tried fs to read file to test but still get the same error, despite the fact that the path is correct. Do you have an example of reading a file from script? Thanks in advance.



  • Did you check this tutorial?
    https://jsreport.net/learn/periodical-report-sending-through-email

    I think you over-complicate it with the extra handlebars when jsreport has it wired already and you can just email the outputs from the afterRender script.



  • Hi jan_blaha,
    Actually I need to send an HTML email with some parameters and attachments, I used the example and it worked perfectly. But I need a more complex HTML email, so that I need someway to read the external file from the script. Currently, I solved the problem with built-in html feature of nodemailer, but it is not a good practice. Do you know how to read file from script? Thanks in advance



  • Here is the code to read a file in the script and put it to the report result.

    const fs = require('fs')
    const path = require('path')
    function afterRender(req, res) {
        res.content = fs.readFileSync(path.join(__appDirectory, 'myfile.txt'))
    }
    


  • Still get the same error, maybe the issue comes from jsreport itself, or configuration???



  • Please try to create a minimal github repo and replicate the problem there so I can take a look.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.