Nodemail x Node.js: Failed auth although tested with success - node.js

I am having a problem with a nodemailer Firebase Function supposed to send emails through an SMTP transporter.
It is strange because I am testing my connection with success using:
// check server readiness
const serverReady = await new Promise<boolean>(resolve => {
transporter.verify( (err: any, succ: any) => {
if (err) {
console.log(err);
resolve(false);
} else {
console.log('Server is ready to take our messages');
resolve(true);
}
});
})
Which prints Server is ready to take our messages.
Then after setting up my envelope andsending it with th .sendMail method I get this error:
Unhandled error { Error: Mail command failed: 530 5.5.1 Authentication Required.
at SMTPConnection._formatError (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:777:19)
at SMTPConnection._actionMAIL (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:1546:34)
at SMTPConnection._responseActions.push.str (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:1028:18)
at SMTPConnection._processResponse (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:935:20)
at SMTPConnection._onData (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:742:14)
at TLSSocket.SMTPConnection._onSocketData.chunk (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:195:44)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at TLSSocket.Readable.push (_stream_readable.js:208:10)
at TLSWrap.onread (net.js:601:20)
code: 'EENVELOPE',
response: '530 5.5.1 Authentication Required.',
responseCode: 530,
command: 'MAIL FROM' }
The error is pretty clear: Failed Auth but it's strange since the test worked earlier!
Connection parameters are:
// options to connect to server
const smtpOptions = {
host: 'smtp.zoho.eu',
port: 465,
secure: true,
auth: {
user: functions.config().nodemailer.user,
pass: functions.config().nodemailer.pass
}
};
// transporter for sending the email
const transporter = nodeMail.createTransport( smtpOptions );
Thank you guys in advance

transporter.verify only tests the connection and authentication, it looks like the that you are not authorised to use the particular MAIL FROM address.

Related

SMTP ERROR using Nodemailer to send email

i am building a web app and i want to send emails to registered users that forgot their password. But I am having problem with sending the mail through Node mail. I keep getting a 421 response error that the server is busy with too many connections ans i dont understand why.
below is my code and the error i keep getting.
require('dotenv').config()
const nodeMailer = require('nodemailer')
const sendMail = async (email, token) => {
const transporter = nodeMailer.createTransport({
host: process.env.EMAIL_HOST,
port: 587,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASSWORD,
},
});
console.log("passed transportrt ");
await transporter.sendMail({
from: process.env.EMAIL_USER,
to: email,
subject: "Password RESET",
text: `Follow this link to reset your password. It expires in 15minutes. \n\n
http://localhost:4000/reset/${token}`,
});
console.log('out')
}
module.exports = sendMail
the other parts and work flow works well till it gets to await transporter.sendMail({...
and this is the error i keep getting below
Error: Invalid greeting. response=421 Service not available: 421 Service not available
at SMTPConnection._actionGreeting (.../node_modules/nodemailer/lib/smtp-connection/index.js:1205:27)
at SMTPConnection._processResponse (.../node_modules/nodemailer/lib/smtp-connection/index.js:947:20)
at SMTPConnection._onData (.../node_modules/nodemailer/lib/smtp-connection/index.js:749:14)
at Socket.SMTPConnection._onSocketData (.../node_modules/nodemailer/lib/smtp-connection/index.js:189:44)
at Socket.emit (node:events:526:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
code: 'EPROTOCOL',
response: '421 Service not available',
responseCode: 421,
command: 'CONN'
}
Is there something i am possibly doing wrong? Plus i am running this on unbuntu(if it has any connection to the error)
A 421 error with an EPROTOCOL error usually means the mail client could not connect to the mail server. Two easy possibilities to check:
process.env.EMAIL_HOST is not defined (not set in .env file or OS)
Your server cannot connect to the mail host at port 587 (easily checked with telnet).

535 Authentication Credentials Invalid in Node.js

I am using node mailer with smtp-pool to sending Mail When I run the app I get this error.
my user and pass and host every thing is right but i face this error.
email.js
const nodemailer = require("nodemailer");
var smtpPool = require('nodemailer-smtp-pool');
const sendEnquiryEmail = function (enquiryData) {
let FromUserName = 'Chat2cars';
nodemailer.createTestAccount((err, account) => {
var transporter = nodemailer.createTransport(smtpPool({
host: 'smtp.gmail.com',
port: 587,
auth: {
user: 'user#gmail.com',
pass: 'mypass'
},
maxConnections: 5,
maxMessages: 10
}));
module.exports = {
sendEnquiryEmail: sendEnquiryEmail
}
And i face this Error
Error: Invalid login: 535 Authentication Credentials Invalid
at SMTPConnection._formatError (D:\CHAT_CAR\Node\node_modules\smtp-connection\lib\smtp-connection.js:528:15)
at SMTPConnection._actionAUTHComplete (D:\CHAT_CAR\Node\node_modules\smtp-connection\lib\smtp-connection.js:1231:30)
at SMTPConnection.<anonymous> (D:\CHAT_CAR\Node\node_modules\smtp-connection\lib\smtp-connection.js:319:22)
at SMTPConnection._processResponse (D:\CHAT_CAR\Node\node_modules\smtp-connection\lib\smtp-connection.js:669:16)
at SMTPConnection._onData (D:\CHAT_CAR\Node\node_modules\smtp-connection\lib\smtp-connection.js:493:10)
at TLSSocket.emit (events.js:210:5)
at addChunk (_stream_readable.js:308:12)
at readableAddChunk (_stream_readable.js:289:11)
at TLSSocket.Readable.push (_stream_readable.js:223:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:182:23) {
code: 'EAUTH',
response: '535 Authentication Credentials Invalid',
responseCode: 535,
command: 'AUTH PLAIN'
}```
Solutions in order of likelihood to help.
Double check you have the correct password 😊
Check if the user has 2fa enabled if so you will need an apps password
Check your Captcha loc
Look into Xoauth2

Nodemailer EAUTH 535 5.7.3 Authentication unsuccessful on Azure Ubuntu server but not local server

I have an Azure Ubuntu 18.04 server with Node v12.13.1, my local machine is Windows 10 and node v10.16.0.
When I run on my local computer there is no issue with Nodemailer sending the email, it sends and quickly appears in my email.
However when it is run on my Azure server, I get the following error.
Error: Invalid login: 535 5.7.3 Authentication unsuccessful [BY5PR16CA0034.nampr d16.prod.outlook.com]
at SMTPConnection._formatError (/home/user/EMR/node_modules/nodemailer/l ib/smtp-connection/index.js:784:19)
at SMTPConnection._actionAUTHComplete (/home/user/EMR/node_modules/nodem ailer/lib/smtp-connection/index.js:1523:34)
at SMTPConnection.<anonymous> (/home/user/EMR/node_modules/nodemailer/li b/smtp-connection/index.js:1481:18)
at SMTPConnection._processResponse (/home/user/EMR/node_modules/nodemail er/lib/smtp-connection/index.js:942:20)
at SMTPConnection._onData (/home/user/EMR/node_modules/nodemailer/lib/sm tp-connection/index.js:749:14)
at TLSSocket.SMTPConnection._onSocketData (/home/user/EMR/node_modules/n odemailer/lib/smtp-connection/index.js:195:44)
at TLSSocket.emit (events.js:210:5)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10) {
code: 'EAUTH',
response: '535 5.7.3 Authentication unsuccessful [BY5PR16CA0034.namprd16.prod. outlook.com]',
responseCode: 535,
command: 'AUTH LOGIN'
}
Here is the code for running Nodemailer on my node.js server:
let transport = nodemailer.createTransport({
service: "Outlook365",
auth: {
user: process.env.USER,
pass: process.env.PASS
},
tls:{
rejectUnauthorized: false
}
})
let message = {
from: '"Name Apps" <nameapps#name.org>',
to: item.email,
subject: subjectText,
html: `<b>HTML message</b>`
};
transport.sendMail(message, function(err, info) {
if (err) {
console.log(err)
} else {
console.log(info);
}
});

Getting unrecognized authentication type error with node.js nodemailer module

I am trying to connect to a microsoft mail server, with node.js nodemailer module. But I get an error.
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'mysmtp.server.com',
port: 25,
secure: false, // true for 465, false for other ports
requireTLS: true,
ignoreTLS: false,
tls : {
rejectUnauthorized: false
},
authMethod: 'LOGIN',
auth: {
type: 'login',
user: 'account#domain.com', // generated ethereal user
pass: 'password' // generated ethereal password
}
});
transporter.verify(function(error, success) {
if (error) {
console.log(error);
} else {
console.log('Server is ready to take our messages');
}
});
ERROR:
{ Error: Invalid login: 504 5.7.4 Unrecognized authentication type
at SMTPConnection._formatError (C:\Users\user\Desktop\mail\node_modules\
nodemailer\lib\smtp-connection\index.js:577:19)
at SMTPConnection._actionAUTHComplete (C:\Users\user\Desktop\mail\node_m
odules\nodemailer\lib\smtp-connection\index.js:1306:34)
at SMTPConnection._responseActions.push.str (C:\Users\user\Desktop\mail\
node_modules\nodemailer\lib\smtp-connection\index.js:349:26)
at SMTPConnection._processResponse (C:\Users\user\Desktop\mail\node_modu
les\nodemailer\lib\smtp-connection\index.js:733:20)
at SMTPConnection._onData (C:\Users\user\Desktop\mail\node_modules\nodem
ailer\lib\smtp-connection\index.js:529:14)
at TLSSocket._socket.on.chunk (C:\Users\user\Desktop\mail\node_modules\n
odemailer\lib\smtp-connection\index.js:680:51)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
code: 'EAUTH',
response: '504 5.7.4 Unrecognized authentication type',
responseCode: 504,
command: 'AUTH PLAIN' }
Does anyone know what is wrong?
Thanks

Nodemailer whit GMail SMTP OAuth2 autentication is getting me 'Invalid status code 401'

I'm trying to make a simple mail sender using Nodemailer 3, the GMail API whit OAuth2 autentication.
Here's my script:
var serverConfig = {
gmail: {
client_user : 'my#email.com',
client_id : '349...myClientId',
secret : 'mysecret..123jd123',
refresh_token : 'x/xxxxxxxxxxxxxx-reZuEMeSuJaSERmCVY',
access_token : 'xxxxxxxxxxxxxxxxx',
expires: '3599'
}
}
// 3LO authentication https://nodemailer.com/smtp/oauth2/#example-3
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: serverConfig.gmail.client_user,
clientId: serverConfig.gmail.client_id,
clientSecret: serverConfig.gmail.secret,
refreshToken: serverConfig.gmail.refresh_token
},
});
module.exports = {
"send": function(_from,_to,_subject,_html,_text){
// setup email data with unicode symbols
var mailOptions = {
from: _from,
to: _to, // list of receivers
subject: _subject, // Subject line
html: _html, // html body
text: _text // plain text body
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
return console.log(error);
}
console.log('Message ' + info.messageId + ' sent: %s' + info.response);
})
}
}
When I force the access_token in the auth object, the email is sent without any problem. But, when I don't specify the access_token, only de refresh_token, I'm getting this error:
{ Error: Invalid status code 401
at ClientRequest.req.on.res (/myproject/node_modules/nodemailer/lib/fetch/index.js:193:23)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:474:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at TLSSocket.socketOnData (_http_client.js:363:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
type: 'FETCH',
sourceUrl: 'https://accounts.google.com/o/oauth2/token',
code: 'EAUTH',
command: 'AUTH XOAUTH2' }
Maybe it's too late, but just answering here incase someone comes across the same issue. My problem rectified when the refreshToken was reset. This article gives great instructions for the whole process
I was able to fix this by going to https://console.cloud.google.com/apis/credentials, selecting the correct OAuth 2.0 Client ID, and choosing "Reset Secret" at the top. This revoked my old secret and generated a new one, which I then used in the OAuth Playground to exchange for a refresh token. I put the new Client Secret and Refresh Token in the auth object for nodemailer and it started working again.

Resources