postalsys / imapflow

IMAP Client library for EmailEngine Email API (https://emailengine.app)
https://imapflow.com
Other
367 stars 64 forks source link

Exception raised when activating envelope: true #145

Closed lucfranken closed 1 year ago

lucfranken commented 1 year ago

Describe the bug

At first try with the library I run into this one:

{
  "type":"TypeError",
  "message":"name.at is not a function",
  "stack":"TypeError: name.at is not a function\n    at Object.processName (/Users/xxxxx/node_modules/imapflow/lib/tools.js:359:37)\n 

Exception is raised when activating envelope: true.

To Reproduce

I use the following code below.

When I set: envelope = false no error happens. With envelope = true error happens.

When envelope = false I do receive e-mail id's etc. So the code generally works.

Relevant code in imapflow:

https://github.com/postalsys/imapflow/blob/d48d0d84e169d0c4315e32d1d565c08f382cace7/lib/tools.js#L358

 processName(name) {
        name = (name || '').toString();
        if (name.length > 2 && name.at(0) === '"' && name.at(-1) === '"') {
            name = name.replace(/^"|"$/g, '');
        }
        return name;
    },

Expected behavior

No error.

Code example

// Wait until client connects and authorizes
            await client.connect();

            // Select and lock a mailbox. Throws if mailbox does not exist
            let lock = await client.getMailboxLock('INBOX');
            try {

                // list all unseen messages
                for await (let msg of client.fetch(
                    {
                        // search query to filter messages
                        // https://imapflow.com/global.html#SearchObject
                        seen: false
                    },
                    {
                        // attributes to request for
                        // https://imapflow.com/global.html#FetchQueryObject
                        uid: true,
                        flags: true,
                        internalDate: true,
                        envelope: true,
                        bodyStructure: true,
                    }
                )) {
                    // extract variables
                    let {seq, uid, flags, bodyStructure, envelope, internalDate} = msg;

                    console.log(msg)

                    console.log(`#${seq} Attributes:`, {seq, uid, flags, bodyStructure, envelope, internalDate});

                    msg.uid = Number(msg);
                    msg.source = msg.source.toString()
                    CollectionEmails.upsert({id: msg.id}, {$set: msg})
                }

            } finally {
                // Make sure lock is released, otherwise next `getMailboxLock()` never returns
                lock.release();
            }

            // log out and close connection
            await client.logout();

Platform

OSX Node v14.21.3 (Meteor)

andris9 commented 1 year ago

ImapFlow does not work with Node.js 14. Whenever you hit a method that is not supported by Node 14 (e.g. String.at()), you will get an error. If you are unable to upgrade Node, you could install EmailEngine and use it as an API backend instead of connecting to IMAP servers directly with ImapFlow.

skeetmtp commented 7 months ago

For records, adding this monkey patch code (generated by chatgpt) overcome the issue:

// Polyfill for String.prototype.at() method
if (!String.prototype.at) {
  String.prototype.at = function(index) {
    // Convert index to a number, in case a string is passed
    index = Number(index);
    // If index is negative, calculate the position from the end of the string
    if (index < 0) index = this.length + index;
    // Return the character at the specified index, or undefined if index is out of bounds
    return index >= 0 && index < this.length ? this.charAt(index) : undefined;
  };
}