Getting error while sending emails from nodemailer from server - javascript

I am trying to send emails from my node.js app using nodemailer.
I dont know why but when I try after uploading the files in my cpanel and check, it send error as :
"Error: connect ECONNREFUSED 74.125.24.108:587"
while it works fine in my localhost.
I even enabled to allow less secure apps in my google account.
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'myemail#gmail.com',
pass: 'XXXXXXXXX'
},
tls: {
rejectUnauthorized: false
}
});
var mailOptions = {
from: 'myemail#gmail.com',
to: "myclient#gmail.com",
subject: 'Code',
text: "Hello",
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent');
}
});
Getting error :
"Error: connect ECONNREFUSED 74.125.24.108:587"

use this way on server , allow your gmail account for less security
https://support.google.com/cloudidentity/answer/6260879?hl=en
var transporter = nodemailer.createTransport({
service: "gmail",
secure: true,
auth: {
user: 'myemail#gmail.com',
pass: 'XXXXXXXXX'
}
});

Related

Error 421 in NodeMailer and not send mail

I am running a server using Nodemailer.
Sometimes the api service respond me with 421.
"{"errorCode":421,
"erroText":"Error occurs",
"error":{"code":"EENVELOPE","response":"550 5.1.0 <xxx#xxxxxx.it> Connessione da 100.27.27.90 temporaneamente rifiutata / Connection from 100.27.27.90 temporarily rejected","responseCode":550,"command":"MAIL FROM"}}"
This is my code in node
var transporter = nodemailer.createTransport({
host: "smtps.aruba.it",
logger: true,
debug: true,
secure: true,
port: 465,
auth: {
user: "xxx#xxxxx.it",
pass: "xxxxx",
},
tls: {
minVersion: "TLSv1",
ciphers: "HIGH:MEDIUM:!aNULL:!eNULL:#STRENGTH:!DH:!kEDH",
},
});
let mailOptions = {
from: '"xxx" <xxx#xxx.it>',
to: email,
subject: "Riepilogo prenotazione",
html: `...`,
};
transporter.sendMail(mailOptions, function (err, data) {
if (err) {
res.json({
errorCode: 421,
erroText: "Error occurs",
error: err,
});
} else {
res.json({
errorCode: 200,
status: "Appointment Created!",
});
}
});
transporter.close();
If I make 2 or 3 calls in a row, the service gives me error 421 with this description
The SMTP 550 error being returned to nodemailer is saying that server smtps.aruba.it is temporarily rejecting the programs request to send data. I suspect it's either 1)the smtp server or 2)some conflict with your dev environment and localhost.
I've had the most success with nodemailer in conjunction with an email sandbox like mailtrap, which allowed me to "trap" the outgoing mail from my local dev environment for testing.

making nodemailer send emails with different gmail accounts

i authenticate with passportjs with success but nodemailer only send emails with one email which is the one used in the console.cloud.google.
wheni try other gmail accounts, i get this error:
Error: Invalid login: 535-5.7.8 Username and Password not accepted.
Learn more at 535 5.7.8
https://support.google.com/mail/?p=BadCredentials u6sm13346885wrp.0 -
gsmtp
at SMTPConnection._formatError (/var/www/html/Recrute/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
at SMTPConnection._actionAUTHComplete (/var/www/html/Recrute/node_modules/nodemailer/lib/smtp-connection/index.js:1536:34)
at SMTPConnection. (/var/www/html/Recrute/node_modules/nodemailer/lib/smtp-connection/index.js:1515:26)
at SMTPConnection._processResponse (/var/www/html/Recrute/node_modules/nodemailer/lib/smtp-connection/index.js:947:20)
at SMTPConnection._onData (/var/www/html/Recrute/node_modules/nodemailer/lib/smtp-connection/index.js:749:14)
at TLSSocket.SMTPConnection._onSocketData (/var/www/html/Recrute/node_modules/nodemailer/lib/smtp-connection/index.js:189:44)
at TLSSocket.emit (events.js:376:20)
at addChunk (internal/streams/readable.js:309:12)
at readableAddChunk (internal/streams/readable.js:284:9)
at TLSSocket.Readable.push (internal/streams/readable.js:223:10) { code: 'EAUTH', response: '535-5.7.8 Username and Password not
accepted. Learn more at\n' +
'535 5.7.8 https://support.google.com/mail/?p=BadCredentials u6sm13346885wrp.0 - gsmtp', responseCode: 535, command: 'AUTH
XOAUTH2' }
i added all the emails to test users.
the code
type: 'oauth2',
user: req.user.email,
clientId: clientId,
clientSecret: clientSecret,
refreshToken: refreshToken,
};
var mailOptions = {
from: req.user.email,
to: 'email#gmail.com',
subject: req.query.sbjct,
text: req.query.msg,
html: req.query.msg,
};
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: auth,
});
transporter.sendMail(mailOptions, (err, res) => {
if (err) {
return console.log(err);
} else {
res.send('done !');
} ```
Gmail does not fully allow sending mails from third party apps. So for you to send mails from a third party app (like your app using nodemailer) you need to go to that gmail account settings and lower gmail security for that account [less secure][1]. nodemailer docs
and your createTransport should look like this
let transporter = nodemailer.createTransport({
host: "smtp.gmail.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: your username, // generated gmail user
pass: your password, // generated gmail password
},
});

Nodemailer connection timeout error using Godaddy SMTP server on aws

I am trying to send email using nodemailer using godaddy smtp server(secureserver.net).
On my local machine code works fine but when I deploy same code on aws server it gives Error: Connection timeout.
Here is my code
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: 'Godaddy',
host: 'smtpout.secureserver.net',
secureConnection: true,
port: 465,
auth: {
user: 'xxx#zzzzzz.com',
pass: '*******'
}
});
let mailOptions = {
from: 'xxx#zzzzzz.com',
to: 'aaaa#gmail.com',
subject: 'Test sub',
html: 'Test body'
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
I have added port 465/25 in outbound port list for the server
Please let me know any workaround this?
(Solution - Latest) This one has worked successfully.
static transport = nodeMailer.createTransport({
// service: 'Godaddy', <--- Dont add this anymore --->
host: 'smtpout.secureserver.net',
port: 465,
auth: {
user: config.get('application.mail.MAIL_SENDER'),
pass: config.get('application.mail.MAIL_SENDER_PASSWORD')
},
tls: { rejectUnauthorized: false }
});
SendMailService.transport.sendMail(mailOptions, function (error: Error, response: SentMessageInfo) {
if (error) {
loggerService.logError(' Error in mail send ' + error);
}
loggerService.logDebug(' response in mail send ' + response);
callback(error, response);
});
This has worked in my case. I am using GoDaddy professional mail service.
I was also getting connection refused and connection timeout for some changes but this one(above mentioned code had worked).
If still there is an issue then check for DNS records whether it is pointing properly under "My Domain >> DNS >> DNS management". Over there check for A type and MX.

Nodemailer - MS Exchaneg server - Error unable to verify the first certificate

I am trying to send email from NodeJS using out office MS Exchange Mail server. with below code. And get error
Our Admin said no certificates are needed.
Error:-
$ node test2.js
Error : { Error: unable to verify the first certificate
at TLSSocket.onConnectSecure (_tls_wrap.js:1048:34)
at TLSSocket.emit (events.js:182:13)
at TLSSocket._finishInit (_tls_wrap.js:628:8) code: 'ESOCKET', command: 'CONN' }
NodeJS Code:-
"use strict";
const nodemailer = require("nodemailer");
async function main() {
try {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'host',
port: 25,
secure : false, // true for 465, false for other ports
auth: {
user: 'user',
pass: 'password'
}
});
// setup email data
let mailOptions = {
from: 'me#email.com',
to: 'me#email.com',
subject: 'Hey you, awesome!',
html: '<b>This is bold text</b>',
text: 'This is text version!'
};
// send mail with defined transport object
let info = await transporter.sendMail(mailOptions)
console.log("Message sent: %s", JSON.stringify(info));
} catch (error) {
console.log('Error : ', error);
}
}
main(); // For testing
The below code change fixed the issue. Added this to the createTransport()
tls: {rejectUnauthorized: false}
Code:-
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'host',
port: 25,
secure : false, // true for 465, false for other ports
auth: {
user: 'user',
pass: 'password'
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
},
});

Nodejs nodemailer error

Running Nodemailer results in the following error, then what should I do to fix it?
Error: Missing credentials for "PLAIN"
at SMTPConnection._formatError
var nodemailer=require('nodemailer');
var transporter = nodemailer.createTransport({
service:'Gmail',
auth:{
user:'kumaresankeerthy#gmail.com',
password:'9943886854**'
}
});
var mailOptions={
};
transporter.sendMail(mailOptions,function(error,info){
if(error){
console.log(error);
} else{
console.log('email sent:' +info.response);
}
});
You are giving wrong arguments in the constructor. Its pass not password
As per official docs
let transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: account.user, // generated ethereal user
pass: account.pass // generated ethereal password
}
});
solved this by going to the following url (while connected to google with the account I want to send mail from):
https://www.google.com/settings/security/lesssecureapps*
you have enable less secure app in your gmail after that it will; work
You need to set up the transporter differently, using the actual email and password of a real Google Account.
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'somerealemailaddress#gmail.com', //email address to send from
pass: 'somerealemailpassword' //the actual password for that account
}
});

Categories