OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to send email using nodemailer with hostinger professional email?

  • Thread starter Thread starter Mouzin Gulzar
  • Start date Start date
M

Mouzin Gulzar

Guest
I am trying to send emails using nodemailer. Email is hosted on hostinger. Here is my configuration:

Code:
host: "smtp.hostinger.com",
port: 465,
secure: true,
auth: {
    user: process.env.GRIEVANCE_EMAIL,
    pass: process.env.GRIEVANCE_EMAIL_PASSWORD,
},

But every time I encounter this error: Error occurred while sending the email: Invalid login: 535 5.7.8 Error: authentication failed:

user and pass is correct as i log into hostinger webmail with the same credentials. I tried setting secure: false, adding tls: {rejectUnauthorized: false} but nothing works. I have to deploy the project in 2 days, event ChatGPT can't help me.

<p>I am trying to send emails using nodemailer. Email is hosted on hostinger.
Here is my configuration:</p>
<pre><code>host: "smtp.hostinger.com",
port: 465,
secure: true,
auth: {
user: process.env.GRIEVANCE_EMAIL,
pass: process.env.GRIEVANCE_EMAIL_PASSWORD,
},
</code></pre>
<p>But every time I encounter this error:
<code>Error occurred while sending the email: Invalid login: 535 5.7.8 Error: authentication failed:</code></p>
<p>user and pass is correct as i log into hostinger webmail with the same credentials. I tried setting <code>secure: false</code>, adding <code>tls: {rejectUnauthorized: false}</code> but nothing works. I have to deploy the project in 2 days, event ChatGPT can't help me.</p>
 
The error you're encountering, "535 5.7.8 Error: authentication failed," typically indicates an issue with the authentication process. This can occur even if you are sure the credentials are correct when logging in via webmail. Here are some steps to help troubleshoot and resolve this issue:

1. Verify Environment Variables​

Ensure that your environment variables are correctly set and accessible in your Node.js application. Add some logging to confirm that the values are being read correctly:

Code:
console.log('Email:', process.env.GRIEVANCE_EMAIL);
console.log('Password:', process.env.GRIEVANCE_EMAIL_PASSWORD);

2. Check for Typos and Special Characters​

Ensure there are no typos in the email or password. Also, check for any special characters in the password that might need to be escaped.

3. Use Direct Credentials for Testing​

For debugging purposes, you can try using the credentials directly in the code to ensure there is no issue with reading environment variables:

Code:
const transporter = nodemailer.createTransport({
    host: "smtp.hostinger.com",
    port: 465,
    secure: true,
    auth: {
        user: '[email protected]',
        pass: 'your-password',
    },
});

4. Configure Nodemailer Transport​

Make sure you configure the nodemailer transport correctly. Here's an example configuration:
Code:
const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
    host: "smtp.hostinger.com",
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
        user: process.env.GRIEVANCE_EMAIL, // your email
        pass: process.env.GRIEVANCE_EMAIL_PASSWORD, // your email password
    },
});

const mailOptions = {
    from: process.env.GRIEVANCE_EMAIL,
    to: '[email protected]',
    subject: 'Test Email',
    text: 'This is a test email',
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log('Error occurred while sending the email:', error.message);
    }
    console.log('Email sent: %s', info.messageId);
});

5. Enable Less Secure Apps​

If Hostinger's SMTP server requires enabling access for less secure apps, make sure this option is enabled in your email account settings.

6. Check Hostinger SMTP Settings​

Verify the SMTP settings provided by Hostinger. Sometimes the settings might differ slightly from what is documented.

7. Enable Debug Mode​

Enable debug mode in Nodemailer to get more detailed logs:

Code:
const transporter = nodemailer.createTransport({
    host: "smtp.hostinger.com",
    port: 465,
    secure: true,
    auth: {
        user: process.env.GRIEVANCE_EMAIL,
        pass: process.env.GRIEVANCE_EMAIL_PASSWORD,
    },
    logger: true,
    debug: true, // include SMTP traffic in the logs
});

Example of Full Code with Debugging​

Code:
const nodemailer = require('nodemailer');

console.log('Email:', process.env.GRIEVANCE_EMAIL);
console.log('Password:', process.env.GRIEVANCE_EMAIL_PASSWORD);

const transporter = nodemailer.createTransport({
    host: "smtp.hostinger.com",
    port: 465,
    secure: true,
    auth: {
        user: process.env.GRIEVANCE_EMAIL,
        pass: process.env.GRIEVANCE_EMAIL_PASSWORD,
    },
    logger: true,
    debug: true, // include SMTP traffic in the logs
});

const mailOptions = {
    from: process.env.GRIEVANCE_EMAIL,
    to: '[email protected]',
    subject: 'Test Email',
    text: 'This is a test email',
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log('Error occurred while sending the email:', error.message);
    }
    console.log('Email sent: %s', info.messageId);
});
By following these steps, you should be able to identify and resolve the authentication issue with sending emails using Nodemailer and Hostinger. If the issue persists, it might be helpful to contact Hostinger support for further assistance.
 
Top