Email read IMAP issue

Hi,

I have set up the ‘Email Read IMAP’ node to read an office 365 folder for incoming emails.

I always try to save attachments even when the email has no attachments, I have the rule in the node that says if it fails continue, but sometimes I get this error:

85|n8n  | TypeError: Cannot read property 'filename' of null
85|n8n  |     at /usr/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/EmailReadImap.node.js:195:103
85|n8n  |     at runMicrotasks (<anonymous>)
85|n8n  |     at processTicksAndRejections (internal/process/task_queues.js:95:5)
85|n8n  |     at async Promise.all (index 0)
85|n8n  |     at async getNewEmails (/usr/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/EmailReadImap.node.js:283:39)
85|n8n  |     at async Connection.onmail (/usr/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/EmailReadImap.node.js:343:44)

To rectify this I then execute workflow manually from gui and the emails are marked as unread and the node triggers correctly.

Could someone please suggest what might be going on please?

Thank you!

Christian

Hey @Christian_Garcia!

Welcome to the community :sparkling_heart:

Do you get this error when there are no attachments? And to make sure that I understood, you get this error when the workflow is triggered, but not when you execute it manually?

Thanks for you fast response.

Sorry for delay I was testing some scenaries, I was test with GUI manually and it give the same error but now there are more details.

It is happend when the mail has attachments.

85|n8n  | (node:13696) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'filename' of null
85|n8n  |     at /usr/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/EmailReadImap.node.js:195:103
85|n8n  |     at processTicksAndRejections (internal/process/task_queues.js:95:5)
85|n8n  |     at async Promise.all (index 0)
85|n8n  |     at async getNewEmails (/usr/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/EmailReadImap.node.js:283:39)
85|n8n  |     at async Connection.onmail (/usr/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/EmailReadImap.node.js:343:44)
85|n8n  | (Use `node --trace-warnings ...` to show where the warning was created)
85|n8n  | (node:13696) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
85|n8n  | (node:13696) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Thanks

Can you share the workflow? Please replace any sensitive data with dummy data. I tested this and it works well for me. I don’t get any errors, for the emails that have attachments and the ones that don’t.

Thanks.

Hey @Christian_Garcia,

Thank you for sharing the workflow. I see that you’ve 4 different HTTP Request nodes that do the same operation. Do you always get 4 files? Also, are you getting this email from a particular email address or with a particular subject? You can setup Custom email rules in the IMAP node to listen for particular emails.

I tested only the IMAP node, and I am not able to replicate your issue still. I am getting the expected result.

Hi, thanks for your help.

I always try to save 4 files because I don’t know if the email has attachments or not, I don’t know if there is another way to check that the email has attachments and then make a iteration “for or each” to save the attachments.

Maybe the error is because it tries to process a third or fourth attachment that does not exist, could you help me with how I can create a “for or each” for the attachments according to the number of attachments

If the emails are from Cortana, or from certain recipients, I will not process them.

Thanks

Hey @Christian_Garcia,

Here’s an example workflow that might help:

If you see the IMAP Email node, I am using Custom Rules to only trigger the node when I receive an email from a particular email. You can refer to the rules here and set up custom rules.
Next, the Function node, Split Binary Data, splits the incoming binary files into individual items and passes them to the next node.
The HTTP Request node will now use these files and upload them to the specified URL. This solution solves the issue when you receive less or more than 4 files.

I hope this helps :slight_smile:

Thanks, I was testing your code and works, but I have an error “Cannot convert undefined or null to object” when the email has no attachments, can I do something inside function for avoid not executing this node when not has attachments ??

Hey @Christian_Garcia,

You can check if the binary data is present or not and then run the function. The below code should cover your use case

for (let item in items) {
  if(Object.keys(items[item]).includes('binary')){
    return Object.keys(items[item].binary).map(key => {
    return {
      json: {},
      binary: {
        data: items[item].binary[key],
      }
    }
});
  }
}
return items;

Thanks for your help, I replaced workflow with the new and gave the same error:

85|n8n  | TypeError: Cannot read property 'filename' of null
85|n8n  |     at /usr/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/EmailReadImap.node.js:195:103
85|n8n  |     at runMicrotasks (<anonymous>)
85|n8n  |     at processTicksAndRejections (internal/process/task_queues.js:95:5)
85|n8n  |     at async Promise.all (index 0)
85|n8n  |     at async getNewEmails (/usr/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/EmailReadImap.node.js:283:39)
85|n8n  |     at async Connection.onmail (/usr/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/EmailReadImap.node.js:343:44)

I use pm2 for running n8n in background, I don’t know if this is something related, I did tests by running n8n manually and getting the same error .

:frowning:

Thanks

That is strange. I tested the above code and it worked well for me. Can you share your updated workflow?

Hello,

Thanks

Hey @Christian_Garcia,

I tried your workflow and couldn’t replicate the issue. Which version of n8n are you using? If you’re not using the latest version, can you update and try on that?

Hey, thanks for your patience and help.

My version is 0.137.0 :frowning: I think is the last.