Search code examples
node.jsexpresscpanelnodemailerwebmail

Using Webmail with Nodemailer


I used gmail with node mailer to create a login system in js node. In development mode I used gmail.

const sendEmail = async options =>{
//1)Create a transporter
// const transporter = nodemailer.createTransport({
    service: 'gmail',
     auth: {
         user: process.env.EMAIL_SEND_SESSION,
         pass: process.env.EMAIL_SEND_PASSWORD,
   }
});
//2) Define the mail options
const mailOptions = {
    from:'Creative Point <[email protected]>',
    to:options.email,
    subject:options.subject,
    html:options.message
}
//3) Actually send the email
await transporter.sendMail(mailOptions);
};

But in production mode I would like to use webmail,I received when I created the data and port account on cpanel.

const transporter = nodemailer.createTransport({
    service:'smtp.specialsoft.ro ',
    port: 465,
    auth: {
        user: process.env.EMAIL_SEND_SESSIONCPANEL,
        pass: process.env.EMAIL_SEND_PASSWORDCPANEL,
  }
});

But it gives me the next mistake.

Error: There was an error sending the email.Try again later!Error: Invalid login: 535-5.7.8 Username 
and Password not accepted. Learn more at
 535 5.7.8  https://support.google.com/mail/?p=BadCredentials g20sm3266545ejz.88 - gsmtp
at C:\Users\Cosmin\Desktop\Authentification System  Node 
 JS,Express,JsonWebToken\controllers\sessionController.js:56:21
at processTicksAndRejections (internal/process/task_queues.js:93:5)

I haven't found anything relevant on the internet about this error.


Solution

  • I solved the problem by following the steps below.

    1. I installed a module: nodemailer-smtp-transport.

       const smtpTransport = require('nodemailer-smtp-transport');
      
    2. I corrected the host name and some options.

      host:'mail.mydomani.ro'
      secureConnection: false,
      tls: {
         rejectUnauthorized: false
       }
      

    3)I integrated the module nodemailer-smtp-transport in the transporter.

     const transporter = nodemailer.createTransport(smtpTransport({
        host:'mail.mydomanin.ro',
        secureConnection: false,
        tls: {
          rejectUnauthorized: false
        },
        port: 587,
        auth: {
            user: process.env.EMAIL_SEND_SESSION,
            pass: process.env.EMAIL_SEND_PASSWORD,
      }
    }));