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 : ' + data.email + '</br><br> Password : ' + 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
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 : ' + data.email + '</br><br> Password : ' + 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 :
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
}
}));
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With