# 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:

1. a Gmail account,
2. IMAP enabled in account settings,
3. an app password or an auth mechanism allowed for your environment,
4. OpenSSL installed locally.

Do not use your primary account password in terminal scripts or shared shells.

## Connect to Gmail IMAP

Open a TLS session:

```bash
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

```imap
a1 LOGIN your-email@gmail.com your-app-password
```

If successful, Gmail returns an `OK` for `a1`. You are now connected to your [IMAP account](/blog/what-is-an-imap-account/) and can list its folders.

### 2) List folders

```imap
a2 LIST "" "*"
```

### 3) Select inbox

```imap
a3 SELECT "INBOX"
```

Server responds with message counts and mailbox flags.

### 4) Fetch recent message headers

```imap
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

```imap
a5 FETCH 1 BODY.PEEK[TEXT]
```

### 6) Logout

```imap
a6 LOGOUT
```

This video runs the same kind of IMAP session over Telnet against a test mail server: it connects to the host and port, lists the mailboxes and fetches messages. Gmail needs the encrypted OpenSSL connection shown above, but the commands you type once connected are the same.



[How to read email in a terminal: Telnet IMAP tutorial](https://www.youtube.com/watch?v=w2y8KFR5YXA)



## 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:

1. trigger email from app,
2. capture in isolated inbox,
3. assert headers/body/links,
4. fail fast on timeout.

[MailSlurp](/) supports this through [email integration testing](/product/email-integration-testing/) and [test inbox APIs](/developers/email-testing-api/).

## 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.
