I was using my gmail account to send an smtp alert messages to my users email . Example, registration or account blocked etc. I am using a nodemailer and was email were sent
I have faced the same error, this worked for me
try {
var transporter = nodemailer.createTransport({
service: 'gmail',
port:465,
secure: true, // true for 465, false for other ports
logger: true,
debug: true,
secureConnection: false,
auth: {
user: 'test@gmail.com', // generated ethereal user
pass: '*****', // generated ethereal password
},
tls:{
rejectUnAuthorized:true
}
})
var info = await transporter.sendMail({
from: 'myuser@outlook.com', // sender address
to: 'anotheruser@hotmail.co.uk', // list of receivers
subject: 'Test Email', // Subject line
text: 'Hello world ?' // plain text body
html: output, // html body
});
}
catch(error) {
console.log(error)
}
Thank you for your time guys. Below is what works for me .
nodemailer.createTransport('smtps://user%myDomain.com:pass@smtp.gmail.com');
changed to
var smtpConfig = {
host: 'smtp.gmail.com',
port: 465,
secure: true, // use SSL
auth: {
user: 'user@myDomain.com',
pass: 'pass@pass'
}
};
var transporter = nodemailer.createTransport(smtpConfig);
I Found the example above on the doc https://github.com/nodemailer/nodemailer . I am suspecting this may happened to you if your password contained @
symbol considering u and the smtps link. Its therefore advisable to split it into an object without the @ symbol interfering with your smtps url. This is my guess thought. Nonetheless the above solution works for me. Plus don't forget to allow less secure apps from your google console.
Adding a config for Bluehost for anyone using it.
let transporter = nodemailer.createTransport({
host: 'box1109.bluehost.com',
port: 465,
secure: true,
auth: {
user: 'someusername@somewebsite.com',
pass: 'yourpassword'
}
});
You can verify the options using the verify()
method.
transporter.verify((err, success) => {
if (err) console.error(err);
console.log('Your config is correct');
});
If you are experiencing this error, make sure that the host property is set properly.
console.log(transporter.options.host);
I spent 20 minutes on this error to find out that the host property was undefined (I was using an environment variable to port it into the config).
I was using secure: false
and I faced the same issue on production and below was the solution that worked for me:
const transporter = nodemailer.createTransport({
host: <hostname>,
port: 25,
secure: false,
logger: true,
debug: true,
ignoreTLS: true // add this
});
Hope this will help someone.