skip to Main Content

I have a node.js application that sends email using nodemailer. It is working fine from localhost but when I try it from server I can’t receive any mail.

Since, other activities of application are working fine means I assumed nodemailer is installed properly on server. I tried on both ways using host as my server and using gmail as on of the services. Here, is what I tried last time. This also works fine on local host. But, I don’t get any response when I put this on server.

I have nodemailer’s configuration as:


const output = `
    <h3>Limit Details</h3>
    <p>Admin has added ${limit} quota(s) to ${username}.</p>
  `;

  // create reusable transporter object using the default SMTP transport
  let transporter = nodemailer.createTransport({
    service: 'gmail',
    port: 25,
    secure: false, // true for 465, false for other ports
    auth: {
        user: '<gmail>', 
        pass: '<password>' 
    }
  });

  // setup email data with unicode symbols
  let mailOptions = {
      from: '"Sender" <sender gmail>', // sender address
      to: '<receiver gmail>', // list of receivers
      subject: 'Quota added information.', // Subject line
      html: output // html body
  };

  // send mail with defined transport object
  transporter.sendMail(mailOptions, (error, info) => {
      if (error) {
          return console.log(error);
      }
      console.log('Message sent: %s', info.messageId);   
      console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
  });

For testing, I have consoled messageId and Preview Url So, from where I can view this console message in cpanel? How can I view such consoled stuffs from my application in cpanel??

3

Answers


  1. Unfortunately there is no easy way to access node’s logs on a cpanel server as far as I know.
    What I usually do is I set up log4js to write logs to a file:

    const log4js = require('log4js');
    
    log4js.configure({
      appenders: { everything: { type: 'file', filename: 'logs.log' } },
      categories: { default: { appenders: ['everything'], level: 'ALL' } }
    });
    
    const logger = log4js.getLogger();
    

    Then you can make logs with:

    logger.debug('log message');
    

    You can also serve the log file with the server:

    app.get('/log', (req, res) => {
      res.sendFile(path.join(__dirname + '/logs.log'));
    });
    
    Login or Signup to reply.
  2. cPanel’s Application manager isn’t very fleshed-out. I’ve been trying it out and comparing it to what I’m used to (PM2) and will be switching back to PM2.
    I did put up a feature request to cPanel if anyone’s interested in supporting.

    Otherwise, the answer is to write out using a custom logger such as @zenott suggests. However, I prefer to overwrite console.log though so it’s easy to switch back to regular console logging if needed.

    My solution:

    const moment = require('moment')
    const fs = require('fs')
    let logStream = fs.createWriteStream('log.txt')
    let console = {}
    console.log = (obj) => {
      var s = ''
      if (typeof obj === 'string')
        s = obj
      else
        s = JSON.stringify(obj)
    
      var dS = '[' + moment().format(momentFormat) + '] '
      s = `[${dS}] ${s}'n'`
      logStream.write(s)
    }
    
    Login or Signup to reply.
  3. There is an easier way to access your NodeJS app logs on a cPanel server.
    Instead of using:

    console.log()
    

    Use:

    console.error()
    

    This will populate the stderr.log file located in your NodeJS app’s root folder on your cPanel without holding back.
    It will even output Circular and JSON formats without any need to stringify().

    UPDATE:

    The above will only work when cPanel is running on CloudLinux (Setup NodeJS App is available).

    For other Distros (with only Application Manager available), just paste the below at the Main App.js file:

    const fs = require('fs');
    const util = require('util');
    const log_file = fs.createWriteStream(__dirname + '/debug.log', {flags: 'w'});
    const log_stdout = process.stdout;
    
    console.log = (d, e, f, g) => {
      log_file.write(util.format('LOG: ', d?d:'', e?e:'', f?f:'', g?g:'') + 'n');
      log_stdout.write(util.format('LOG: ', d?d:'', e?e:'', f?f:'', g?g:'') + 'n');
    }
    
    console.error = (d, e, f, g) => {
      log_file.write(util.format('ERROR: ', d?d:'', e?e:'', f?f:'', g?g:'') + 'n');
      log_stdout.write(util.format('ERROR: ', d?d:'', e?e:'', f?f:'', g?g:'') + 'n');
    }
    

    Then use console.log() as usual across all cascading modules.
    This will create and populate a debug.log file located in your NodeJS app’s root folder.
    You may also use console.trace() to get the line number for the error.
    Use the command tail -f debug.log in terminal for live logging.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search