blog
How to Read Gmail Messages with IMAP from Terminal (OpenSSL Walkthrough)
Connect to Gmail IMAP from your terminal using OpenSSL, authenticate safely, select folders, and fetch message data for debugging.
If you need to debug mailbox behavior, reading Gmail via IMAP in terminal is a fast way to inspect server responses directly.
This guide uses OpenSSL to open an encrypted IMAP session and run core commands.
Before you start
You need:
- a Gmail account,
- IMAP enabled in account settings,
- an app password or an auth mechanism allowed for your environment,
- OpenSSL installed locally.
Do not use your primary account password in terminal scripts or shared shells.
Connect to Gmail IMAP
Open a TLS session:
openssl s_client -crlf -connect imap.gmail.com:993
If connection succeeds, you should see an IMAP greeting similar to * OK.
IMAP command basics
Each command starts with a client tag (a1, a2, etc.). The server echoes that tag in the response.
1) Login
a1 LOGIN your-email@gmail.com your-app-password
If successful, Gmail returns an OK for a1.
2) List folders
a2 LIST "" "*"
3) Select inbox
a3 SELECT "INBOX"
Server responds with message counts and mailbox flags.
4) Fetch recent message headers
a4 FETCH 1:5 (BODY.PEEK[HEADER.FIELDS (FROM TO SUBJECT DATE)])
Adjust range (1:5) based on mailbox size and what you need to inspect.
5) Fetch body preview
a5 FETCH 1 BODY.PEEK[TEXT]
6) Logout
a6 LOGOUT
Common failures and fixes
Authentication fails
- verify app-password setup,
- confirm IMAP is enabled,
- check account security restrictions.
TLS or certificate errors
- ensure system trust store is current,
- test network/proxy behavior,
- confirm you are connecting to
imap.gmail.com:993.
Unexpected folder names
Use LIST first and then SELECT exact names returned by server.
Why terminal IMAP is useful
- debug mailbox state quickly,
- validate message arrival/order,
- inspect server capabilities,
- reproduce edge cases outside app code.
For automated tests in CI, direct IMAP polling can be brittle. API-based test inboxes are often easier to make deterministic.
CI-safe alternative for email tests
For product testing workflows:
- trigger email from app,
- capture in isolated inbox,
- assert headers/body/links,
- fail fast on timeout.
MailSlurp supports this through email integration testing and test inbox APIs.
Final take
Terminal IMAP access is excellent for protocol-level debugging, but production QA should use repeatable automated inbox assertions. Use both approaches where each is strongest.