Email Variable for file name



  • I want to use one email distribution setup script for several emails. I want the email subject file name and the content file name to be a variable that changes based on the report it is sending.

          subject: "[DEV][Oregon] DailyPlayer Report",
          text: "Please see attached report CSV file  Private and Confidential.  Internal Use Only.  Not for Distribution.",
          html: "<p>Please see attached <b>Daily Player Account CSV </b> </p> <p>Private and Confidential.  Internal Use Only.  Not for Distribution.</p>",
          attachments: [
          {  
            filename: 'Daily_Punter_Account_Report.csv',
            content: Buffer.from(res.content)
          }],
        })       
    }
    
    

    so the file name would be the report being sent. Something like this... %/Reports/csv_filename%

          subject: "[DEV][Oregon] %/Reports/csv_filename%",
          text: "Please see attached report CSV file  Private and Confidential.  Internal Use Only.  Not for Distribution.",
          html: "<p>Please see attached <b>%/Reports/csv_filename% CSV </b> </p> <p>Private and Confidential.  Internal Use Only.  Not for Distribution.</p>",
          attachments: [
          {  
            filename: '%/Reports/csv_filename%.csv',
            content: Buffer.from(res.content)
          }],
        })       
    }


  • You can reach the current template name like this and use it to construct the subject dynamically.

    function afterRender(req, res) {
       const templateName = req.template.name
    }
    

    Does this help you?



  • @jan_blaha are you suggesting something like this?

        attachments: [
          {  
            filename: function afterRender(req, res) {
                      const templateName = req.template.name
                    },
            content: Buffer.from(res.content)
          }],
        })


  • Guess not

    TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received function afterRender
        at validateString (internal/validators.js:124:11)
        at Object.parse (path.js:1283:5)
        at Object.detectMimeType (/home/ubuntu/jsreportapp/node_modules/nodemailer/lib/mime-funcs/mime-types.js:2065:27)
        at Object.detectMimeType (/home/ubuntu/jsreportapp/node_modules/nodemailer/lib/mime-funcs/index.js:490:44)
        at /home/ubuntu/jsreportapp/node_modules/nodemailer/lib/mail-composer/index.js:95:66
        at Array.map (<anonymous>)
        at MailComposer.getAttachments (/home/ubuntu/jsreportapp/node_modules/nodemailer/lib/mail-composer/index.js:86:66)
        at MailComposer.compile (/home/ubuntu/jsreportapp/node_modules/nodemailer/lib/mail-composer/index.js:26:34)
        at /home/ubuntu/jsreportapp/node_modules/nodemailer/lib/mailer/index.js:178:56
        at processPlugins (/home/ubuntu/jsreportapp/node_modules/nodemailer/lib/mailer/index.js:275:28)


  • const nodemailer = require('nodemailer')
    async function afterRender(req, res) { 
        const smtpTransport = nodemailer.createTransport({...})
    
        const mail = {
            from: '..>',
            to: '...',
            subject: "[DEV][Oregon] " + req.template.name,
            text: '...',       
            attachments: [
            {  
                filename: 'Report.pdf',
                content: new Buffer(res.content)
            }],
        }
    
        try {
            await smtpTransport.sendMail(mail)
        } finally {
            smtpTransport.close()
        }
    }
    


  • @jan_blaha I appreciate the interaction and the learning. I am sure this will help others too.

    That solves the subject line but not the file attachment

        {  
            filename: 'punter_account.csv' + new Date().getTime(),
            content: new Buffer(res.content)
        }],
    
    const axios = require('axios')
    
    async  function beforeRender(req, res)  {
      const resData =  await axios.get('http://jsonplaceholder.typicode.com/posts')
      console.log(resData.data)
      req.data.posts = resData.data
    }
    
    const nodemailer = require('nodemailer')
    async function afterRender(req, res) {
        const transporter = nodemailer.createTransport({
          service: 'SendGrid',
          auth: {
              user: 'apikey',
              pass: 'SG.............................................................S20'
          }
        });
    
        await transporter.sendMail({
          from: "someone@someco.com",
          to: "me@someco.com",
          subject: "[DEV][Oregon] " + req.template.name,
          text: "Please see attached report CSV file  Private and Confidential.  Internal Use Only.  Not for Distribution.",
          html: "<p>Please see attached <b>Daily Player Account CSV </b> </p> <p>Private and Confidential.  Internal Use Only.  Not for Distribution.</p>",
              attachments: [
            {  
                filename: 'punter_account.csv',
                content: new Buffer(res.content)
            }],
        }
        try {
            await smtpTransport.sendMail(mail)
        } finally {
            smtpTransport.close()
        }
    }


  • I'm probably missing something, but why don't you use for attachment name the same idea as for the subject?

    filename: req.template.name + '.csv',
    

Log in to reply
 

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