How to sendMail using nodemail and smtp transport?

3.8k Views Asked by At

I have new user registration once i get user detail from client i am sending email to group to grant permission to user , Below code is throwing exception any idea what is implemented wrong ?

I have pasted error in question.

Mailer.js

var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');

var MAIL_SERVER_URL = '127.0.0.0'; 


//establish the transporter
var transporter = nodemailer.createTransport(smtpTransport({
    host: MAIL_SERVER_URL,
    port: 80,
    connectionTimeout: 60000,
}));

var Mailer = {
      options: function (mailOptions) {
        mailOptions.to = '[email protected]';
        mailOptions.text = mailOptions.auid + ''+ 'requested access for modeler';
        console.log('mailOptions',mailOptions);
        transporter.sendMail(mailOptions, function(error, info) {
                    if (error) {
                        return console.log(error);
                    } else {
                        console.log('Message sent: ' + info.response);
                    }
        });
      }
}



module.exports = Mailer;

Error

{ [Error: connect EACCES 127.0.0.0:25]
  code: 'ECONNECTION',
  errno: 'EACCES',
  syscall: 'connect',
  address: '127.0.0.0',
  port: 80,
  command: 'CONN' }
2

There are 2 best solutions below

9
On BEST ANSWER

Your error output is telling you that nodemailer cannot access the SMTP server at 127.0.0.0 on port 25.

Your implementation of nodemailer using SMTP looks good assuming the mailOptions getting passed in has a from property. You can read more about SMTP through nodemailer in the docs.

let nodemailer = require('nodemailer');

let MAIL_SERVER_URL = '127.0.0.0';
let smtp = nodemailer.createTransport({
    host: MAIL_SERVER_URL,
    connectionTimeout: 60000
});

module.exports = {
    options: (mailOptions) => {
        let email = {
            from: '[email protected]',
            to: '[email protected]',
            subject: 'yourEmailSubject'
            text: `${mailOptions.auid} requested access for modeler`
        };
        smtp.sendMail(email, (err, info) => {
            if (err) 
                console.log(err);
            else
                console.log(`Message sent: ${info.response}`);

        });
    }
};
0
On

SMTP mail send using xoauth

var smtpTransport = nodemailer.createTransport({
                service: "Gmail",
                auth: {
                  type: "OAuth2",
                  user: credential.email,
                  clientId: credential.clientId,
                  clientSecret: credential.clientSecret,
                  refreshToken: credential.refreshToken,
                },
              });

 smtpTransport.sendMail(mailOptions);