I'm trying to send welcome email to user when they create account in my application. I'm using nodemailer and mailtrap to see my emails. But I'm getting the error...
{
"status": "error",
"error": {
"code": "ETIMEDOUT",
"command": "CONN",
"statusCode": 500,
"status": "error"
},
"message": "Connection timeout",
"stack": "Error: Connection timeout\n at SMTPConnection._formatError (/home/adiln22/Desktop/after-section-12/node_modules/nodemailer/lib
My Email class looks like this
const nodemailer = require('nodemailer');
const pug = require('pug');
const htmlToText = require('html-to-text');
module.exports = class Email {
constructor(user, url) {
this.to = user.email;
this.firstName = user.name.split(' ')[0];
this.url = url;
this.from = `Adil Nehal <${process.env.EMAIL_FROM}>`;
}
newTransport() {
return nodemailer.createTransport({
host: process.env.EMAIL_HOST,
port: process.env.EMAIL_PORT,
auth: {
user: process.env.ADMIN_EMAIL,
pass: process.env.ADMIN_PASSWORD
}
});
}
// Send the actual email
async send(template, subject) {
// 1) Render HTML based on a pug template
const html = pug.renderFile(`${__dirname}/../views/emails/${template}.pug`, {
firstName: this.firstName,
url: this.url,
subject
});
// 2) Define email options
const mailOptions = {
from: this.from,
to: this.to,
subject,
html,
text: htmlToText.fromString(html)
};
// 3) Create a transport and send email
await this.newTransport().sendMail(mailOptions);
}
async sendWelcome() {
await this.send('welcome', 'Welcome to the Natours Family!');
}
async sendPasswordReset() {
await this.send(
'passwordReset',
'Your password reset token (valid for only 10 minutes)'
);
}
};
And from here I'm sending the email.
const Email = require('./../utils/email');
exports.signup = catchAsync(async (req, res, next) => {
const newUser = await User.create({
name: req.body.name,
email: req.body.email,
password: req.body.password,
passwordConfirm: req.body.passwordConfirm
});
//This is the main thing
const url = `${req.protocol}://${req.get('host')}/me`;
console.log(url);
await new Email(newUser, url).sendWelcome();
//leave it
createSendToken(newUser, 201, res);
});
Now, what I've tried.
Updated the nodemailer. Used different ports provided by the mailtrap used POP3 instead of SMTP(getting the same SMTP error)
I'm not using Gmail that the Gmail would block it.
Then I came to you. Please help.