I am using below code to download the Gmail attachments. In windows this is working fine but when I am trying to run this same code in my Linux machine it is giving me an error as below. Also can anyone help how can I perform the same thing for office outlook. in that I am getting an authentication error
Error :
Downloading attachments for pmgtesting1@gmail.com since 2020-11-25 to files/...
[connection] Connected to host
<= '* OK Gimap ready for requests from 51.103.27.188 l25mb237269790wmj'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH'
<= 'A0 OK Thats all she wrote! l25mb237269790wmj'
=> 'A1 LOGIN "pmgtesting1@gmail.com" "pswd****"'
<= '* NO [WEBALERT https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv-cVj07x6UiZjwizvTzmqYRNNyofoJ8yiGr960BU9t1OKkU83L43n5ocMP2SlhJ3MqWkb04Wj4ye1XxDbTSALJhCRkQ373HLB_tcH_yHBmmDycIiLGnX3NNETY0QakllRJFVjR] Web login required.'
<= 'A1 NO [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)'
[connection] Ended
[connection] Closed
disconnected from pmgtesting1@gmail.com
Error ----> {
latestTime: undefined,
lastSyncId: 0,
uidvalidity: undefined,
handledIds: [],
errors: [
Error: Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)
at Connection._resTagged (/home/textify/Mail-attachement/node_modules/imap/lib/Connection.js:1488:11)
at Parser.<anonymous> (/home/textify/Mail-attachement/node_modules/imap/lib/Connection.js:193:10)
at Parser.emit (events.js:314:20)
at Parser._resTagged (/home/textify/Mail-attachement/node_modules/imap/lib/Parser.js:175:10)
at Parser._parse (/home/textify/Mail-attachement/node_modules/imap/lib/Parser.js:139:16)
at Parser._tryread (/home/textify/Mail-attachement/node_modules/imap/lib/Parser.js:82:15)
at TLSSocket.Parser._cbReadable (/home/textify/Mail-attachement/node_modules/imap/lib/Parser.js:53:12)
at TLSSocket.emit (events.js:314:20)
at emitReadable_ (_stream_readable.js:558:12)
at processTicksAndRejections (internal/process/task_queues.js:83:21) {
type: 'no',
textCode: 'ALERT',
source: 'authentication'
}
]
}
NOTE : I have enabled the Less secure app connection
const downloadEmailAttachments = require('download-email-attachments');
const moment = require('moment');
const todaysDate = moment().format('YYYY-MM-DD');
var reTry = 1;
var paraObj = {
invalidChars: /\W/g,
account: `"****@gmail.com":Paswd******@imap.gmail.com:993`,
// all options and params
//besides account are optional
directory: './files',
filenameTemplate: '{filename}',
// filenameTemplate: '{day}-{filename}',
filenameFilter: /.txt?$/,
timeout: 10000,
log: { warn: console.warn, debug: console.info, error: console.error, info:
console.info },
since: todaysDate,
lastSyncIds: ['234', '234', '5345'], // ids already dowloaded and ignored, helpful
//because since is only supporting dates without time
attachmentHandler: function (attachmentData, callback, errorCB) {
console.log(attachmentData);
callback()
}
}
var onEnd = (result) => {
if (result.errors || result.error) {
console.log("Error ----> ", result);
if(reTry < 4 ) {
console.log('retrying....', reTry++)
return downloadEmailAttachments(paraObj, onEnd);
} else console.log('Failed to download attachment')
} else console.log("done ----> ");
}
downloadEmailAttachments(paraObj, onEnd);
const downloadEmailAttachments = require('download-email-attachments');
const moment = require('moment');
const todaysDate = moment().format('YYYY-MM-DD');
var reTry = 1;
var paraObj = {
invalidChars: /\W/g,
account: `"****@gmail.com":Paswd******@imap.gmail.com:993`,
// all options and params
//besides account are optional
directory: './files',
filenameTemplate: '{filename}',
// filenameTemplate: '{day}-{filename}',
filenameFilter: /.txt?$/,
timeout: 10000,
log: { warn: console.warn, debug: console.info, error: console.error, info:
console.info },
since: todaysDate,
lastSyncIds: ['234', '234', '5345'], // ids already dowloaded and ignored, helpful
//because since is only supporting dates without time
attachmentHandler: function (attachmentData, callback, errorCB) {
console.log(attachmentData);
callback()
}
}
var onEnd = (result) => {
if (result.errors || result.error) {
console.log("Error ----> ", result);
if(reTry < 4 ) {
console.log('retrying....', reTry++)
return downloadEmailAttachments(paraObj, onEnd);
} else console.log('Failed to download attachment')
} else console.log("done ----> ");
}
downloadEmailAttachments(paraObj, onEnd);
I got the answer just enable the 2-factor authentication in Gmail and then register your app and use the app password to fetch the email. Note: For the outlook part, I don't have the admin right to generate the app password if you have then this will also work for you Will update with the other solution as soon as I find them