Попробуйте этот код. Сначала вам нужно создать приложение в Google Cloud Console и включить Gmail API из библиотеки. Получите учетные данные вашего приложения. Для этого нажмите на Учетные данные и вместо авторизованного URI перенаправления перейдите по этой ссылке https://developers.google.com/oauthplayground и сохраните его. Далее в другой вкладке откройте эту ссылку https://developers.google.com/oauthplayground/ нажмите на символ настроек справа. Установите флажок (т. Е. Используйте свои собственные учетные данные OAuth) после этого Вам необходимо дать ваш clientId и clientSecret. И в то же время на левой стороне есть текстовое поле с заполнителем, таким как Ввод ваших собственных областей, сохраните эту ссылку https://mail.google.com/ и нажмите Авторизовать API, затем нажмите код авторизации Exchange для токенов, и вы получите свои refreshToken и accessToken, сохраните эти два в своем коде. Надеюсь, это поможет вам ...
const nodemailer=require('nodemailer');
const xoauth2=require('xoauth2');
var fs=require('fs');
var transporter=nodemailer.createTransport({
service:'gmail',
auth:{
type: 'OAuth2',
user:'Sender Mail',
clientId:'Your_clientId',//get from Google Cloud Console
clientSecret:'Your clientSecret',//get from Google Cloud Console
refreshToken:'Your refreshToken',//get from https://developers.google.com/oauthplayground
accessToken:'Tor accessToken'//get from https://developers.google.com/oauthplayground
},
});
fs.readFile("filePath",function(err,data){
var mailOptions={
from:' <Sender mail>',
to:'receiver mail',
subject:'Sample mail',
text:'Hello!!!!!!!!!!!!!',
attachments:[
{
'filename':'filename.extension',//metion the filename with extension
'content': data,
'contentType':'application/type'//type indicates file type like pdf,jpg,...
}]
}
transporter.sendMail(mailOptions,function(err,res){
if(err){
console.log('Error');
}
else{
console.log('Email Sent');
}
})
});