Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 616
Bhagvat Lande Avatar asked Oct 27 '25 23:10

Bhagvat Lande


2 Answers

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
like image 177
Bhagvat Lande Avatar answered Oct 31 '25 12:10

Bhagvat Lande


Hours wasted until I found this. Simply adding name: hostgator, and everything is working as it should. In the docs "https://nodemailer.com/smtp/" it states:

"name – optional hostname of the client, used for identifying to the server, defaults to ***hostname of the machine***".

I think the issue here is the difference between a subdomain and domain as the mail servers are subdomains ie-> gator1234.hostgator.com and when using the default it is not "hostgator.com" or any provider for that matter but the subdomain name which is the hostname "gator1234.hostgator.com" and that will not work. This can occur for any hosted email provider. The docs aren't wrong per se but all of the examples do not use "name:" as it is implied with that statement as default.

Thanks @Bhagvat Lande

like image 29
user1946891 Avatar answered Oct 31 '25 13:10

user1946891



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!