Search code examples
node.jssmtpnodemaileremail

Nodemailor can't sending emails on cross domains


I am using nodemailor for sending emails with node and express application.

issues : when i am sending emails on cross domain for ex : i am using hostgator email server it was sending email on hosted emails only with hostgator , but while i am sending emails on Gmail it will return success

250 OK id=1hU5l2-000m0C-Lh 

but not getting emails.

Note : getting success response but not receiving emails in case of cross domain

const nodemailer = require('nodemailer');
const keys = require('../config/keys');
const smtpTransport = require('nodemailer-smtp-transport');
emailCredentaials = (data) => {

      var transporter = nodemailer.createTransport(smtpTransport ({
        host: 'my.hostgator.com',
        port: 465,
        secure:true,
        auth: {
            user: keys.email.emailUserName,
            pass: keys.email.emailPassword
          },
          tls: {
            // do not fail on invalid certs
            rejectUnauthorized: false
          }  
        }));
    var mailOptions = {
        from: keys.email.emailUserName,
        to: data.email,
        subject: 'Demo account credentials',
        html: '<h3>Please Follow the link to login : '
            + keys.emailLinks.accountCredentailLink + '<h3>' + '<br>Useraname &nbsp;&nbsp; : &nbsp;&nbsp;' + data.email + '</br><br> Password &nbsp;&nbsp; : &nbsp;&nbsp;' + data.password + '</br>'
    };

    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log("***********" + error);
        } else {
            console.log('*********Email sent:********' + info.response);
        }
    });
}
module.exports = emailCredentaials;

I used above configuration for nodemailor . Thanks


Solution

  • After digging a lot finally i come back with following answer, if someone having same issue please try with following solution . I added following configuration .

    const nodemailer = require('nodemailer');
    const keys = require('../config/keys');
    const smtpTransport = require('nodemailer-smtp-transport');
    
    emailCredentaials = (data) => {
    
        var transporter = nodemailer.createTransport(smtpTransport({
            name: 'hostgator',
            host: 'my.hostgator.com',
            port: 465,
            secure: true,
            auth: {
                user: keys.email.emailUserName,
                pass: keys.email.emailPassword
            }
        }));
    
        var mailOptions = {
            from: keys.email.emailUserName,
            to: data.email,
            subject: 'Demo account credentials',
            html: '<h3>Please Follow the link to login : '
                + keys.emailLinks.accountCredentailLink + '<h3>' + '<br>Useraname &nbsp;&nbsp; : &nbsp;&nbsp;' + data.email + '</br><br> Password &nbsp;&nbsp; : &nbsp;&nbsp;' + data.password + '</br>'
        };
    
    
        transporter.sendMail(mailOptions, function (error, info) {
            if (error) {
                console.log("***********" + error);
            } else {
                console.log('*********Email sent:********' + info.response);
            }
        });
    }
    module.exports = emailCredentaials;
    

    changes in posted question :

    • Needs to add "nodemailer-smtp-transport"

    npm install --save nodemailer-smtp-transport

    var transporter = nodemailer.createTransport(smtpTransport({
            name: 'hostgator',
            host: 'my.hostgator.com',
            port: 465,
            secure: true,
            auth: {
                user: keys.email.emailUserName,
                pass: keys.email.emailPassword
            }
        }));
    
    • above added "name" property it's a server name , Thanks