TS - NodeMailer OAuth2 'auth' does not exist in type 'TransportOptions - node.js

I have a problem. I need to make email sending function with Gmail api. In docs it tells me to make a createTransport function like this:
const transport = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: "horvatlucian1#gmail.com",
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
refreshToken: REFRESH_TOKEN,
accessToken: accessToken,
},
})
DOCS
But it all turns red and returns:
Error
Any solution? Thanks.

There is a problem of type script when you tring to pass the accessToken because in google oauth2client the return type of getAccessToken() is https://github.com/googleapis/google-auth-library-nodejs/blob/b0ddb7512fb9ed1e51b6874b7376d7e1f26be644/src/auth/oauth2client.ts#L331
that not supported by the types of nodemailer that should be string:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/7827d853a1d623dd5345d9a11ced3e15eb5d3d8b/types/nodemailer/lib/xoauth2.d.ts#L24
by looking at the return type of the getAccessToken you can see that the token is inside that have type string so to fix that just destructure your token from the accessToken.
const transport = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: "horvatlucian1#gmail.com",
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
refreshToken: REFRESH_TOKEN,
accessToken: accessToken.token,
},
})

1/
const transport = nodemailer.createTransport({
service: String("gmail") || "",
auth: {
type: String("OAuth2"),
user: String(SENDER_MAIL),
clientId: String(CLIENT_ID),
clientSecret: String(CLIENT_SECRET),
refreshToken: String(REFRESH_TOKEN),
access_token: String(access_token),
},
});
and the ERRO WITH MSG :
[ERROR] 14:02:23 тип Unable to compile TypeScript:
server/config/sendmail.ts(25,7): error TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type '{ service: string; auth: { type: string; user: string; clientId:string; clientSecret:string; refreshToken:string; access_token:GetAccessTokenResponse; }; }' is not assignable to parameter of type 'TransportOptions | Transport<unknown>'.
Object literal may only specify known properties, and 'service' does not exist in type 'TransportOptions | Transport<unknown>'.

The accessToken required in nodemailer.createTransport is nullable
Because of that, to avoid error, you must cover the case when accessToken is null by providing an empty string or something similar to avoid null error
const transport = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: process.env.EMAIL_ADDRESS,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken: process.env.REFRESH_TOKEN,
accessToken: accessToken.token || '',
},
})
Hopefully that help

Related

Why does nodemailer throw the error: Invalid credentials? [duplicate]

I would like to find a way to send email from my app using nodemailer to the users either with some kind of google authentication or any other way. Below mentioned working code has stopped working after Google has disabled less secure app option.
const nodemailer = require('nodemailer')
const sendEmail = async options => {
const transporter = nodemailer.createTransport({
// host: "smtp.gmail.com",
// port: "465",
// secure: true,
service:'gmail',
auth: {
user: "USER_EMAIL",
pass: "USER_PASSWORD"
},
tls:{rejectUnauthorized:false}
})
const message = {
from: `${process.env.FROM_NAME} <${process.env.FROM_EMAIL}>`,
to: options.email,
subject: options.subject,
text: options.message,
html: options.message,
attachments: [
{
filename: '.png',
path: __dirname + '.png',
cid: '.png'
}
]
}
const info = await transporter.sendMail(message)
console.log('Message sent : %s', info.messageId)
console.log(__dirname)
}
module.exports = sendEmail
At the time of writing, Less Secure Apps is no longer supported by google. And you can't use your google account password.
You're gonna have to generate a new app password.
App passwords only work if 2-step verification is turned on.
Follow this steps to get the app password
Go to https://myaccount.google.com/security
Enable 2FA
Create App Password for Email
Copy that password (16 characters) into the pass parameter in Nodemailer auth.
const client = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "username#gmail.com",
pass: "Google-App-Password-Without-Spaces"
}
});
client.sendMail(
{
from: "sender",
to: "recipient",
subject: "Sending it from Heroku",
text: "Hey, I'm being sent from the cloud"
}
)
You should check out Xoauth2.
Nodmailer supports serval types of Oauth
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
type: "OAuth2",
user: "user#example.com",
clientId: "000000000000-xxx0.apps.googleusercontent.com",
clientSecret: "XxxxxXXxX0xxxxxxxx0XXxX0",
refreshToken: "1/XXxXxsss-xxxXXXXXxXxx0XXXxxXXx0x00xxx",
accessToken: "ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x",
expires: 1484314697598,
},
});

How to connect to Gmail using node.js imapflow?

I'm using imapflow package like this:
import config from './config.js';
import { ImapFlow } from 'imapflow';
const client = new ImapFlow({
host: 'imap.gmail.com',
port: 993,
secure: true,
auth: {
user: 'muhemail#gmail.com',
pass: '123'
}
});
await client.connect();
console.log('OK');
And it throws with Invalid credentials (Failure). I've quadruple-checked that login and password are correct, IMAP is enabled in GMail settings. Less than secure apps aren't enabled though, and I'd prefer to keep it that way. When I try to use the same credentials in Thunderbird, it opens an OAuth login window, which I suppose I should somehow incorporate with imapflow? Or is there a different solution?
Gmail does not allow accessing its IMAP services by using plain username and password.
You should first get OAuth2.0 credentials via the Gmail api example (here) and then should convert it to xoauth2.
let {installed: {client_id, client_secret}} = require('./client_secret') // the client_secret.json file
let xoauth2gen = xoauth2.createXOAuth2Generator({
user: '*******', // the email address
clientId: client_id,
clientSecret: client_secret,
refreshToken: refresh_token
})
xoauth2gen.getToken(function(err, xoauth2token) {
if (err) {
return console.log(err)
}
let imap = new Imap({
xoauth2: xoauth2token,
host: 'imap.gmail.com',
port: 993,
tls: true,
authTimeout: 10000,
debug: console.log,
})

Send Email Using Microsoft 365 Email Server In NodeJS

let transporter = nodemailer.createTransport({
service: "Outlook365",
host: 'smtp.office365.com',
port: 587,
tls: {
ciphers:'SSLv3'
},
auth: {
user: 'username',
pass: 'password'
}
});
I have an EAUTH error while sending an email, please check the image for error.
[1]: https://i.stack.imgur.com/snt3T.jpg
This code should do what you wish, you'll need to set your password to test this.
If the password is incorrect, you'll get an error:
Error: Invalid login: 535 5.7.3 Authentication unsuccessful message.
const nodemailer = require('nodemailer');
// Set this from config or environment variable.
const PASSWORD = '....';
async function send365Email(from, to, subject, html, text) {
try {
const transportOptions = {
host: 'smtp.office365.com',
port: '587',
auth: { user: from, pass: PASSWORD },
secureConnection: true,
tls: { ciphers: 'SSLv3' }
};
const mailTransport = nodemailer.createTransport(transportOptions);
await mailTransport.sendMail({
from,
to,
replyTo: from,
subject,
html,
text
});
} catch (err) {
console.error(`send365Email: An error occurred:`, err);
}
}
send365Email("from#example.com", "to#example.com", "Subject", "<i>Hello World</i>", "Hello World");
You will need to disable the SmtpClientAuthenticationDisabled through the online power shell with the command
Set-TransportConfig -SmtpClientAuthenticationDisabled $false
You can check for more information at Enable or disable authenticated client SMTP submission (SMTP AUTH) in Exchange Online
You can find the detailed information here:
https://developer.microsoft.com/en-us/graph/quick-start?code=M.R3_BAY.822b5ade-d816-85bb-ec94-8c349cdfca4b&state=option-node

nodemailer and gmail APIs

Im trying to get nodemailer working using Google GMail API.
I've got my Google project set up - using their oauth sandbox for redirect,
I've got a clientID, secret and refresh token from my developers console.
The Code
const nodemailer = require('nodemailer');
const xoauth2 = require('xoauth2');
// create reusable transporter
let transporter = nodemailer.createTransport( {
service: 'gmail',
xoauth2: xoauth2.createXOAuth2Generator({
user: 'me#myDomain.com',
clientid: 'blahblahblah.apps.googleusercontent.com',
clientSecret: 'k33p-gUeSsINg',
refreshToken: '123BritneyIsTheBest'
}),
tls: {
rejectUnauthorized: false
}
} );
// setup email data
let mailOptions = {
from: 'me#myDomain.com',
to: 'me#yahoo.com',
subject: 'Hello there Google API...',
text: 'Hello Google API',
html: '<b>Hello Google API</b>'
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: ', info.messageId);
});
The Error.
{
Error: Mail command failed: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 https://support.google.com/mail/?p=WantAuthError l24sm4075119ywk.21 - gsmtp
at SMTPConnection._formatError (C:\abc\index.js:591:19)
at SMTPConnection._actionMAIL (C:\abc\index.js:1350:34)
at SMTPConnection._responseActions.push.str (C:\abc\index.js:840:18)
at SMTPConnection._processResponse (C:\abc\index.js:747:20)
at SMTPConnection._onData (C:\abc\index.js:543:14)
at TLSSocket._socket.on.chunk (C:\abc\index.js:495:47)
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)
code: 'EENVELOPE',
response: '530-5.5.1 Authentication Required. Learn more at\n530 5.5.1 https://support.google.com/mail/?p=WantAuthError l24sm4075119ywk.21 - gsmtp',
responseCode: 530,
command: 'MAIL FROM'
}
I have tried this - and it works, but it's my understanding that this is very unsecured and NOT acceptable for production.
const nodemailer = require('nodemailer');
// create reusable transporter object
let transporter = nodemailer.createTransport( {
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'me#myDomain.com',
pass: 'Seriously?'
}
});
... all the rest is the same, removed for brevity...
The existing posts solutions have not helped.
Thank You
How about this modification?
From :
let transporter = nodemailer.createTransport( {
service: 'gmail',
xoauth2: xoauth2.createXOAuth2Generator({
user: 'me#myDomain.com',
clientid: 'blahblahblah.apps.googleusercontent.com',
clientSecret: 'k33p-gUeSsINg',
refreshToken: '123BritneyIsTheBest'
}),
tls: {
rejectUnauthorized: false
}
} );
To :
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'oauth2',
user: 'me#myDomain.com',
clientId: 'blahblahblah.apps.googleusercontent.com', // This key is "clientId".
clientSecret: 'k33p-gUeSsINg',
refreshToken: '123BritneyIsTheBest'
},
tls: {
rejectUnauthorized: false
}
});
Note :
Please confirm whether https://mail.google.com/ was included in the scope, when you retrieved the refresh token.
Please confirm whether gmail API is enabled.
It may be required to turn on "Allow less secure apps: ON" at https://myaccount.google.com/lesssecureapps.
Reference :
nodemailer OAuth2
In my environment, I confirmed that this works fine. But if this was not useful for you, I'm sorry.

nodemailer with XOauth2 settings says bad credentials

I have the following node code:
var nodemailer = require("nodemailer");
var xoauth2 = require('xoauth2');
/*
Here we are configuring our SMTP Server details.
STMP is mail server which is responsible for sending and recieving email.
*/
var smtpTransport = nodemailer.createTransport({
service: "Gmail",
host: "smtp.gmail.com",
auth: {
XOAuth2: {
user: "buckscountytourofhonor#gmail.com",
pass: "****",
clientId:"myclientidenteredhere",
clientSecret:"mysecretenteredhere",
refreshToken:"mytokenenteredhere"
}
}
});
module.exports = function(app, passport){
app.get('/send',function(req,res){
var mailOptions={
to : 'buckscountytourofhonor#gmail.com',
from: req.query.email,
subject : req.query.subject,
text : req.query.name +": "+req.query.message
}
console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
res.end("error");
}else{
console.log("Message sent: " + response.message);
res.end("sent");
}
});
});
I am currently following this tutorial. Actually followed many, but this is the most succinct.
I am getting the following error:
Error: invalid login: 535-5.7.8 username and password not accepted
Can anyone help me figure out what I am missing, I have been spending over 3 hours researching this.
I seem to have faced this issue just now. I have an older implementation working just fine with xoauth, another app with the same code but a more recent nodemailer install did give
Invalid login: 535-5.7.8 Username and Password not accepted.
I went from xoauth2 like so
var generator = require('xoauth2').createXOAuth2Generator({
user: 'xxx',
clientId: 'xxx',
clientSecret: 'xxx',
refreshToken: 'xxx'
});
and
var transporter = nodemailer.createTransport(({
service: 'gmail',
auth: {
xoauth2: generator
}
to
var transporter = nodemailer.createTransport(({
service: 'gmail',
auth: {
type: 'OAuth2',
user: 'xxx',
clientId: 'xxx',
clientSecret: 'xxx',
refreshToken: 'xxxx'
}
}));
This was then working for me. Not sure if thats it but worth a try. I did follow the same tutorial for the Gmail-setup, this did work in my earlier implementation and again with the new project.

Resources