cant download attachments using mail-listener2

Viewed 85

iam using mail-listener2 to download files from an gmail account

it works and I can see the files on the mailListener.on("mail",) event but it looks like files r not save on the attachments folder

on the file event the file.path is undefined so I am guessing it dit not saved...

  1. why I can't see the files?

  2. why there is a .on("attachment") event id i can see the files on "mail" event?

  3. i only want to process png files, is there any filter i can use?

this is my code...

thanks

import path from "path";

var MailListener = require("mail-listener2");

export class InvoiceFileLisetner {

    private readonly hostName: string = 'imap.gmail.com'
    private readonly userName: string = 'username';
    private readonly password: string = 'password';
    private readonly port: number;




    public Listen() {
        var mailListener = new MailListener({
            username: this.userName,
            password: this.password,
            host: this.hostName,
            port: 993, // imap port
            tls: 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"], // 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: path.join(__dirname, '\\attachments/') }
        });

        mailListener.start(); // start listening

        mailListener.on("server:connected", function () {
            console.log("imapConnected");
        });

        mailListener.on("server:disconnected", function () {
            console.log("imapDisconnected");
        });

        mailListener.on("error", function (err) {
            console.log(err);
        });

        mailListener.on("mail", function (mail, seqno, attributes) {

            if (mail.attachments != undefined) {
                let accountName = mail.subject;
                let invoiceFile = null;

                for (let index = 0; index < mail.attachments.length; index++) {
                    if (mail.attachments[index].contentType == 'application/vnd.ms-excel') {

                    }

                }
            }

            // mail processing code goes here
        });

        mailListener.on("attachment", function (attachment) {


            console.log(attachment.path);
        });
    }


}
0 Answers
Related