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
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:
Then you can make logs with:
You can also serve the log file with the server:
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:
There is an easier way to access your NodeJS app logs on a cPanel server.
Instead of using:
Use:
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 MainApp.js
file: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.