Search code examples
node.jsemailgmail-imap

How to fetch Inbox only with attachments using mail-listener2?


I am new to mail-listener. I am able to fetch gmail inbox along with attachments but my requirement is to fetch inbox only. With attachments using mail-listener:

var mailListener = new MailListener({
    username: "[email protected]",
    password: "xxx",
    host: "imap.gmail.com",
    port: 993, // imap port
    tls: true,
    //ssl:true,
    connTimeout: 10000, // Default by node-imap
    authTimeout: 5000, // Default by node-imap,
    debug: console.log, // Or your custom function with only one   incoming argument. Default: null
    tlsOptions: { rejectUnauthorized: false },
    mailbox: "INBOX", // mailbox to monitor
    //searchFilter: ["UNSEEN", "FLAGGED"], // the search filter being used after an IDLE notification has been retrieved
    markSeen: true, // all fetched email willbe marked as seen and not fetched next time
    fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
    mailParserOptions: {streamAttachments: true}, // options to be passed to mailParser lib.
    attachments: true, // download attachments as they are encountered to the project directory
    attachmentOptions: { directory: "attachments/" } // specify a   download directory for attachments
});

Instead mailbox : "INBOX" I want something like mailbox : "INBOX ONLY ATTACHMENTS" or searchfilter : "HAS ATTACHMENTS"


Solution

  • I don't think using mail-listener2 is a good idea. In my opinion using these third-party libraries is never a good because you will have to deal with a lot of other issues. Google provides Gmail API's using which you can access the inbox, get push notifications etc. Read about it here https://developers.google.com/gmail/api

    And btw you have made your password public.