I am using imap package in my project I have read the documentation and I found this for searching in email with google search extensions:
search() criteria extensions:
X-GM-RAW - string - Gmail's custom search syntax. Example: 'has:attachment in:unread' X-GM-THRID - string - Conversation/thread id X-GM-MSGID - string - Account-wide unique id X-GM-LABELS - string - Gmail label
I want to search email with the criteria 'has:attachment'.
How can i use this ?
My imap.search fields like this:
const Imap = require('imap');
const base64 = require('base64-stream')
var buffer = require("buffer");
var fs = require("fs");
const { simpleParser } = require('mailparser');
const imapConfig = {
user: 'no-replay@mymail.com.br',
password: 'mypass',
host: 'imap.gmail.com',
port: 993,
tls: true,
};
const getEmails = () => {
try {
const imap = new Imap(imapConfig);
imap.once('ready', () => {
imap.openBox('INBOX', false, () => {
imap.search(['ALL', ['FROM', 'rodrigo.barros.dsn@gmail.com']], (err, results) => {
const f = imap.fetch(results, { bodies: '' });
f.on('message', msg => {
msg.on('body', stream => {
simpleParser(stream, async (err, parsed) => {
// const {from, subject, textAsHtml, text} = parsed;
console.log('Imprimindo Parsed:')
console.log(parsed);
/* Make API call to save the data
Save the retrieved data into a database.
E.t.c
*/
});
});
msg.once('attributes', attrs => {
const { uid } = attrs;
imap.addFlags(uid, ['\\Seen'], () => {
// Mark the email as read after reading it
console.log('Marked as read!');
});
});
});
f.once('error', ex => {
return Promise.reject(ex);
});
f.once('end', () => {
console.log('Done fetching all messages!');
imap.end();
});
});
});
});
imap.once('error', err => {
console.log(err);
});
imap.once('end', () => {
console.log('Connection ended');
});
imap.connect();
} catch (ex) {
console.log('an error occurred');
}
};