IMAP test failed? Work through these causes in order
A CI job reporting a broken IMAP test almost always has one of six ordinary causes. Here they are ranked by how often they bite, each with the check that confirms it and the fix.
On this page
- Cause 1: the password stopped being a valid credential
- Cause 2: right host, wrong port or wrong TLS mode
- Cause 3: the test looked before the mail landed
- Cause 4: the mail arrived and your SEARCH missed it
- Cause 5: one mailbox, many test runs
- Cause 6: the body decoded differently in CI
- When IMAP is the wrong tool for the test
When a build reports that an imap test failed, IMAP is rarely the thing that broke. Six ordinary causes cover almost all of it, and they are worth checking in this order: a credential the provider no longer accepts, the wrong port or TLS mode, a test that looked before the mail arrived, a search against the wrong folder, a mailbox shared across runs, and a body that decoded differently in CI.
First, get the real error. IMAP servers answer in words, but most client libraries raise a generic exception instead, which is why people start editing configuration at random. Turn the protocol transcript on first.
import imaplib
imaplib.Debug = 4 # prints every command and response
m = imaplib.IMAP4_SSL("imap.example.com", 993)
m.login(user, password)
A line such as NO [AUTHENTICATIONFAILED] Invalid credentials (Failure) tells you which
section below to read.
Cause 1: the password stopped being a valid credential
This is the most common reason an imap test failed on a job that worked last month, because nothing in your repository changed. The provider changed.
Google’s app passwords are 16 digit codes that exist only on accounts with 2-Step Verification turned on, so a plain account password gets rejected. Microsoft removed Basic authentication for IMAP and POP in Exchange Online across all tenants, and its own documentation says nobody, including Microsoft support, can turn it back on. The only path there is OAuth 2.0 with an XOAUTH2 token.
Mint the credential type the provider actually wants and store it as a CI secret. Do not skip the boring case either: a rotated password or an expired refresh token produces exactly the same error as a typo.
Cause 2: right host, wrong port or wrong TLS mode
RFC 9051 is explicit that a server listens on port 143, the cleartext port, or port 993, the
implicit TLS port. STARTTLS belongs only on the cleartext port, and a server must answer
BAD if it arrives on 993. Mixing the two gives you either a hang until timeout or an
OpenSSL “wrong version number”.
Confirm the endpoint by hand:
openssl s_client -crlf -connect imap.example.com:993 # expect a "* OK" greeting
If that works from your machine but not from CI, the difference is the environment, not the
code: hosted runners and locked down networks often block outbound 993, and slim container
images ship without ca-certificates, which turns every handshake into a certificate
verification failure.
Cause 3: the test looked before the mail landed
A sleep(5) followed by one fetch is the single biggest source of a flaky mail test.
Delivery is not synchronous: the message queues, gets scanned, and sometimes gets greylisted
and retried minutes later.
Poll with a deadline instead of a fixed wait, and re-issue NOOP between checks. A
connection opened before the message arrived will not always volunteer an updated EXISTS
count on its own. When you need arrival within a second, RFC 2177’s IDLE exists because, in its
words, “it’s often more desirable to have the server transmit updates to the client in real
time”.
The same bug shows up in browser suites; the pattern for waiting for email inside a Playwright test applies whatever protocol sits underneath.
Cause 4: the mail arrived and your SEARCH missed it
SEARCH runs server side against the mailbox you selected. If the message was filed in
Junk, Spam or a provider specific label, an INBOX search truthfully returns nothing.
Two habits fix most of this. First, list the folders and search the plausible ones instead
of assuming INBOX. Second, use UID SEARCH and UID FETCH, never bare sequence numbers:
RFC 9051 states that when a message is expunged, the sequence number of every message after
it is decremented, while UIDs stay stable for the session. A test that fetched message 3
after a cleanup deleted message 1 reads the wrong mail and asserts happily.
Best of all, put a unique token in the subject when you trigger the flow and search for that. It survives folder moves and makes the assertion unambiguous.
Cause 5: one mailbox, many test runs
If the suite is green alone and red in a matrix, the mailbox is shared state. Two jobs read
the same INBOX, the first marks a message \Seen or deletes it, and the second finds
nothing. Yesterday’s message with the same subject matches today’s search and you assert on
a stale body. Providers also cap simultaneous IMAP connections per account, so a wide matrix
can trip a “too many connections” error; check your provider’s documentation for the limit.
Give every test its own address. Plus addressing is the cheap version, though some senders normalise or strip the tag. A fresh mailbox per test is better, and purging before the test rather than after means a crashed run cannot poison the next one.
Cause 6: the body decoded differently in CI
A regex over the raw message is the last trap. Quoted-printable inserts soft line breaks, so
a six digit code can arrive split across two lines as 48= then 2913. Multipart messages
hide the code in a part a naive parser never opens, and HTML entities break a pattern that
matched locally.
Decode the MIME parts properly, pick the one you want, then match. If the suite also sends its own mail as a fixture, read the notes on using Nodemailer safely in a test suite before you wire a real transport into CI.
When IMAP is the wrong tool for the test
Keep IMAP when IMAP is the thing under test: a mail client, a ticket ingest that polls a real mailbox, a parser you ship. That flakiness belongs to your product.
If your imap test failed for a reason on this list rather than a reason in your own code, the dependency is not earning its keep. When all you need is “did the mail arrive, and what is the code”, IMAP costs you an account, a credential someone else controls, folder rules, and a port your runner may not reach. An HTTP test inbox removes all of it:
curl "https://inboxdome.com/v1/inboxes/inb_.../wait?for=otp&timeoutSeconds=30" \
-H "Authorization: Bearer idk_live_..."
The free tier covers 100 inbox tests a month with no credit card, and the assertion endpoint checks SPF, DKIM and DMARC in the same request. See the Email Testing API reference and email tests in GitHub Actions.
It does not fit everywhere. InboxDome exposes no IMAP or POP3 endpoint and its addresses are receive-only, so it cannot prove your code can log into a mail server or send a message. Compare hosted test mail tools such as Mailtrap and Ethereal too; check their documentation for the protocols each exposes, and read why an Ethereal test mailbox stops showing mail if that is your current stack.
Disclosure: InboxDome publishes this blog and makes the products it mentions. We describe what they do rather than promise results.
Key takeaways
- Check in this order: credentials, port and TLS mode, timing, folder, shared mailbox, MIME decoding.
- Gmail app passwords only work on accounts with 2-Step Verification, and Exchange Online removed Basic authentication for IMAP entirely.
- Port 993 is implicit TLS and port 143 is cleartext plus STARTTLS; sending STARTTLS on 993 is a protocol error.
- Sequence numbers shift after an expunge, so use UID commands and search for a unique token you put in the subject.
- If the thing under test is not IMAP itself, an HTTP test inbox removes the whole dependency.
Sources
- RFC 9051, Internet Message Access Protocol (IMAP) Version 4rev2 — Section 2.1 on ports 143 and 993, and 2.3.1.2 on sequence numbers changing after an expunge.
- RFC 2177, IMAP4 IDLE command — Why real time updates beat frequent polling.
- Microsoft: Deprecation of Basic authentication in Exchange Online — Basic auth for IMAP and POP is disabled in all tenants and cannot be re-enabled.
- Google Account Help: Sign in with app passwords — App passwords only work on accounts that have 2-Step Verification turned on.
Links last checked September 7, 2026.
Frequently asked questions
Why does my IMAP test pass locally but fail in CI?
Usually egress rules or shared state. Hosted runners often block outbound port 993, slim containers ship without ca-certificates, and a matrix of parallel jobs reading one mailbox will steal messages from one another.
What does AUTHENTICATIONFAILED Invalid credentials actually mean?
The server rejected the credential, not necessarily the password. On Gmail it usually means a raw account password where an app password or an OAuth token is required; on Microsoft 365 it means Basic authentication is gone and you need OAuth 2.0.
Should a test use IDLE or polling?
Polling with a deadline is simpler and good enough for most suites, as long as you re-issue NOOP so the server can report new messages. IDLE is worth the extra code only when you need arrival within a second or two.
Does InboxDome give me an IMAP endpoint?
No. InboxDome inboxes are read over HTTPS and are receive-only, so if your test has to prove your code can speak IMAP to a real server, keep a dedicated test mailbox for that job.