IMAP Read Mail with Gmail not working

I’m trying to make a simlpe e-mail forwarder.

  1. Read emails from Gmail’s (G Suite Account) with IMAP
  2. Send any received email to a Matrix’s channel

This never detects new emails, whether in Execution Mode, or simply by activating the workflow.

I don’t really know what else I can say to provide further info, but I’m happy to provide anything you need to help me debug this. The node process itself doesn’t produce any logs, as far as I can see, to get more info on what might be going wrong.

Is SSL/TLS toggled on in the credentials settings?

image

Yes it is. It will actually fail if it’s not enabled.
Either way, still no luck.

Personally I cannot reproduce it, but a team member recently experienced this. I will follow up with him and post here if we find anything.

1 Like

Please do, I’ll gladly help in anyway possible.

Not being able to detect new emails is a big turn off since most of the automation I was planning to do depend on that feature. :cry:

Just adding more info that might (or not) be helpful n8n is running inside a kubernetes cluster, traefik is proxying the editor through port 80/443 (HTTPS).

Grafana doesn’t show any significant changes on CPU and Memory metrics over time, either it’s really stable or is doing nothing at all, but I have no logs to confirm any activity, except that I know that other workflows (cron) are working.

I think it’s something related to how the GMAIL account is configured. This happened to me with my personal email yesterday (gmail). I did no receive any of the messages. Then did the same process with my work email (also gmail) and it worked perfect.

That’s interesting. I’m using a G Suite Gmail account, however I just configured Thunderbird to read from that same account using IMAP (with unencrypted password) and it worked, same thing with other email clients.

I will double check that IMAP is enabled at admin level, I will update this thread if I find anything.

Also, question, is there any flag/configuration/env variable that one can use to have n8n output logs to stdout? I find it so odd that I have no logs whatsoever :cry:

Hello @Couto

I just managed to make it work properly for both my Peronal account and my business (Google Suite) account.

One question: did you follow all steps described in the docs? This is the link: https://docs.n8n.io/credentials/imap/#using-imap

You have to enable “Less secure app” by adding 2-step authentication to your account and then creating an app password. This is the only way the IMAP will work.

For more information about app passwords, check here: https://support.google.com/accounts/answer/185833?authuser=1

Regarding the logs, we have a pull request on the way for this funcionality.

I am working on it. Still needs some improvement but may be released in the next version if I make it in time (possibly tomorrow).

1 Like

Yes and yes.

Like I said, I’ve had other IMAP clients reading from that account without any problems.

If you don’t use a less secure app, the IMAP node will output an error from Google that makes it pretty obvious when you have to use a less secure password, so that’s not the reason :cry:

ERROR: Application-specific password required: https://support.google.com/accounts/answer/185833 (Failure)

Error: Application-specific password required: https://support.google.com/accounts/answer/185833 (Failure)
    at Connection._resTagged (/usr/local/lib/node_modules/n8n/node_modules/imap/lib/Connection.js:1502:11)
    at Parser.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/imap/lib/Connection.js:194:10)
    at Parser.emit (events.js:315:20)
    at Parser._resTagged (/usr/local/lib/node_modules/n8n/node_modules/imap/lib/Parser.js:175:10)
    at Parser._parse (/usr/local/lib/node_modules/n8n/node_modules/imap/lib/Parser.js:139:16)
    at Parser._tryread (/usr/local/lib/node_modules/n8n/node_modules/imap/lib/Parser.js:82:15)
    at TLSSocket.Parser._cbReadable (/usr/local/lib/node_modules/n8n/node_modules/imap/lib/Parser.js:53:12)
    at TLSSocket.emit (events.js:315:20)
    at emitReadable_ (internal/streams/readable.js:569:12)
    at processTicksAndRejections (internal/process/task_queues.js:79:21)

Hum strange.

Then maybe it might have something to do with your inbox perhaps?

Do you have filters? Any chance mails are skipping your inbox? This might cause such issues.

I do have some filters, but e-mails are definitely hitting my inbox (the current filters are for very specific addresses from which I can not send emails to test this). Emails from me to me, are clearly hitting my inbox, and so are regular third-party emails :frowning:

I’m really open to suggestions to be honest since I ran out of ideas.

Can someone with a Gmail account confirm what’s the value for the Mailbox Name field please? Maybe it’s not INBOX, but something else??

On my end, setting Mailbox Name to “INBOX” works.

@RicardoE105 Quick tests:

  • From GSuite to GSuite working
  • From normal Gmail to GSuite working
  • From non-Gmail to GSuite working

Will test non-GSuite recipients tomorrow.

At this point I’m pretty sure it’s not a problem with my account.
I just made a quick proof-of-concept using imap-simple, the same library that n8n uses.

const imaps = require("imap-simple");

let conn;

const fetchAndProcess = () => {
  const searchCriteria = ["UNSEEN"];
  const fetchOptions = {
    bodies: ["HEADER"],
    markSeen: false,
  };
  conn.search(searchCriteria, fetchOptions).then((messages) => {
    messages.forEach((item) => console.log(item));
  });
};

const config = {
  imap: {
    user: "[email protected]",
    password: "App Password From Google",
    host: "imap.gmail.com",
    port: 993,
    tls: true,
    authTimeout: 3000,
    tlsOptions: { rejectUnauthorized: false },
  },
  onmail: fetchAndProcess,
};

imaps
  .connect(config)
  .then((connection) => {
    conn = connection;
    return connection.openBox("INBOX");
  })
  .catch((e) => console.error(e));

This works, and I’m able to retrieve my >6000 unread messages, and reacts to new emails. Granted that code is inefficient, but it works with my account perfectly. We need logs to get a better understanding of what’s happening with that node, it could be my infrastructure, or any other layer in between, but without logs, it gets really hard to debug.

I just tested it again with the same account that was not working yesterday, and it now works. I just deleted everything and did the process described in the credentials again.

With regards to the logs, agreed, this is something that is being worked on right now. You can add a couple of console.logs in the node and run the project locally.

So I think I got something.

While debugging this locally, I accidentally left the Action parameter as “Mark as read” (Thanks Obama!)
This not so obviously marked all my 6K unread email as read, since the imap-simple pulls all unread emails when it firsts connects

After this point, the workflow started working. Not perfectly, but working.

So what can I conclude, without debugging this any further since I no longer have 6K unread emails

  1. It’s possible that the Read Email can’t deal with 6K unread emails
  2. It’s possible that the Matrix node simply doesn’t allow sending 6K messages (I would expect a failure here, but none was ever introduced)
  3. The Read Email trigger is bugged, when it connects, it always triggers the onmail event, however we probably want to ignore this first event trigger since we didn’t received any email.
  4. If I leave the Action as “Nothing”, Im now receiving in Matrix as many messages as the number of unread emails that I have in my mailbox, so it’s receiving repeated messages (See attached screenshot), this means that over time, the workflow will stop working (see point 1), either this is a bug, or the trigger needs a better description of what is does.
  5. The executions window, is not particularly helpful if all executions start at exactly the same hour, at least it keeps showing repeatedly a past execution.
  6. It would be nice to be able to configure the polling interval


it keeps incrementing executions of Simple Node Forwarder, even though that was cancelled.

Noticed the repeated messages, those are unread emails. Also, the test number 4 and test number 5 were sent at the same time, but I only received the number 4, the fifth email only showed up in the next poll trigger.

As before, I will try to provide any info you ask for.