Can't send email with nestjs and nodemailer - nestjs

I try to send email from server with nodemailer and nestjs.
Here is module configuration
import { Module } from "#nestjs/common";
import { MailService } from "./services/mail/mail.service";
import { MailController } from "./controllers/mail/mail.controller";
import { MailerModule } from "#nestjs-modules/mailer";
#Module({
controllers: [MailController],
providers: [MailService],
imports: [
MailerModule.forRoot({
transport: {
host: process.env.EMAIL_HOST,
port: process.env.EMAIL_PORT,
secure: true,
auth: {
user: process.env.EMAIL_ID,
pass: process.env.EMAIL_PASS
}
}
})
]
})
export class MailModule {
}
and the method
#import { MailerService } from "#nestjs-modules/mailer";
constructor(private mailer: MailerService) {
}
async sendConfirmationLetter(to: string): Promise<void> {
try {
await this.mailer.sendMail({
to: 'to',
from: 'from',
subject: 'subject',
text: 'some text'
});
} catch (e) {
console.log(e);
}
}
but I have an exception
Error: connect ECONNREFUSED 127.0.0.1:465
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1139:16) {
errno: -4078,
code: 'ESOCKET',
syscall: 'connect',
address: '127.0.0.1',
port: 465,
command: 'CONN'
}
What am I doing wrong? All information is taken from the documentation https://nest-modules.github.io/mailer/docs/mailer.html
Please, help!
If some information is not enough, I can provide it

MailerModule.forRoot({
transport: {
host: process.env.EMAIL_HOST, // change to your email smtp server like www.example.com
port: process.env.EMAIL_PORT, // change to configured tls port for smtp server
secure: true,
auth: {
user: process.env.EMAIL_ID,
pass: process.env.EMAIL_PASS
}
}
})
you are using your host and port for smtp server, if u had setup the smtp on the same instance it show be fine, but I don think it is same port and host should be domain also.
If u plan to use gmail smtp then the setting should be
MailerModule.forRoot({
transport: {
host: "smtp.gmail.com",
port: "465",
secure: true,
auth: {
user: "your_gmail_email",
pass: "your_gmail_app_password"
}
}
})

Related

Nodemailer- connect ECONNREFUSED 127.0.0.1:587

I'm trying sending emails using node mailer but I'm getting the error
Error: connect ECONNREFUSED 127.0.0.1:587
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
errno: -4078,
code: 'ESOCKET',
syscall: 'connect',
address: '127.0.0.1',
port: 587,
command: 'CONN'
}
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'smtp.outlook.com',
secure: false,
debug: true,
secureConnection: false,
port: 587,
auth: {
user: 'myemail#outlook.com',
pass: 'pass'
} ,
tls: {
rejectUnauthorized: true,
}
});
var mailOptions = {
from: 'myemail#outlook.com',
to: 'myemail#outlook.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
}
else {
console.log('Email sent: ' + info.response);
}
});

Nodemailer smtp timeout

SMTP informationI'm getting this error :-
Error: connect ETIMEDOUT 144.76.97.27:25
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1132:16)
{
errno: -4039,
code: 'ESOCKET',
syscall: 'connect',
address: '144.76.97.27',
port: 25,
command: 'CONN'
}
my code is:-
let transporter = nodemailer.createTransport({
host: "mail.tusharjoshi.tech",
port: 25,
secure: false, // true for 465, false for other ports
auth: {
user: "contact#tusharjoshi.tech", // generated ethereal user
pass: "mypAssword" // generated ethereal password
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
}
});
const mailOptions = {
from: "tusharjoshi.tech",
to: "hellotusharr2318#gmail.com",
subject: `message from ${req.body.name}`,
text: `Message recieved from ${req.body.email}: and message is about: ${req.body.message}`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
res.send("error");
} else {
console.log("send");
res.send("success");
}
});
did you check this answer? nodemailer SMTP server connection issue (Error: connect ETIMEDOUT) but it works for another system not in mine

Node.js nodemailer Gmail SMTP not working

require("dotenv").config();
"use strict";
var nodemailer = require("nodemailer");
const SMTPTransport = require("nodemailer/lib/smtp-transport");
console.log("Nach attr");
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// Generate test SMTP service account from ethereal.email
// create reusable transporter object using the default SMTP transport
console.log("vor transporter");
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secureConnection: true, // true for 465, false for other ports
auth: {
user: 'email', // generated ethereal user
pass: 'password', // generated ethereal password
tls: {
rejectUnauthorized: false
}
},
});
transporter.verify().then(console.log).catch(console.error);
let mailOptions = {
from: 'testsender',
to: 'reciever',
subject: 'Test Mail',
text: '123456789'
};
console.log("vor transporter.send");
transporter.sendMail(mailOptions, function (err, data) {
if (err) {
console.log('Error Occurs: ', err);
} else {
console.log('Email sent!');
}
});
//console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview only available when sending through an Ethereal account
//console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);
Console output
Error Occurs: Error: connect ETIMEDOUT 64.233.166.108:465
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
errno: 'ETIMEDOUT',
code: 'ESOCKET',
syscall: 'connect',
address: '64.233.166.108',
port: 465,
command: 'CONN'
}

Problem with connect by Sequelize when I connect to SQL Server

I use the mssql package and connect to database like this:
const sql = require('mssql')
const config = {
user: 'sa',
password: 'xxxxxx',
server: 'xxx.xxx.xxx.xxx',
database: 'zorgo',
options : {
enableArithAbort: false,
cryptoCredentialsDetails: {
minVersion: 'TLSv1'
}
}
};
Now I want to try 'sequelize' library and try to connect
const sequelize = new Sequelize('zorgo', 'sa', 'xxxxxx', {
dialect: 'mssql',
host: 'xxx.xxx.xxx.xxx',
options: {
enableArithAbort: false,
cryptoCredentialsDetails: {
minVersion: 'TLSv1'
}
}
});
But I get an error
Failed to connect to xxx.xxx.xxx.xxx:1433 - 584:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol
Please help me resolve this problem
I found the answer to my question
const sequelize = new Sequelize('zorgo', 'sa', 'xxxxxx', {
dialect: 'mssql',
host: 'xxx.xxx.xxx.xxx',
dialectOptions: {
options: {
enableArithAbort: false,
cryptoCredentialsDetails: {
minVersion: 'TLSv1'
}
}
}
});
And works well...

Nodemailer - Error: connect ETIMEDOUT 216.58.221.36:587

I have an api which sends email when a user submits data on the frontend. I can't get the nodemailer sendmail to work.
nodemailer.js -
const nodemailer = require("nodemailer");
async function sendmail(data) {
let transporter = nodemailer.createTransport({
host: "www.google.com",
port: 587,
secure: false,
auth: {
user: <my email>,
pass: <password>,
},
});
await transporter.sendMail(
{
from: data.email,
to: <my email>,
subject: "CEWA Feedback",
text: data.message,
html: "<b>Hello world?</b>",
},
(err) => {
console.log(err);
}
);
}
module.exports = sendmail;
When this function runs I get this error in the console -
Error: connect ETIMEDOUT 216.58.221.36:587
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
errno: 'ETIMEDOUT',
code: 'ESOCKET',
syscall: 'connect',
address: '216.58.221.36',
port: 587,
command: 'CONN'
}
Any idea what's going wrong? Thanks in advance.
can you try adding tls config to the transporter object
let transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
auth: {
user: <my email>,
pass: <password>,
},
tls: {rejectUnauthorized: false}
});

Resources