Imap Commandline Linux

IMAP (Internet Message Access Protocol) is a standard email protocol that stores email messages on a mail server, but allows the end user to view and manipulate the messages as though they were stored locally on the end user's computing device(s).

This tutorial explains the details to handle the imap mail boxes using telnet command.


The telnet command will connect to the provided HOSTNAME via port 143. The hostname can be your domain, MX record, or IP address. Port 143 is used for the IMAP protocol.

Syntax: telnet [HOSTNAME] 143

telnet 143
Connected to
Escape character is '^]'.

Log in

Enter the username and password for the account you are trying to access. For this step, and all steps hereafter, we will be typing a command tag before the actual command. In this particular example, the command tag is 01. Telnet then uses that same tag in its response line, to let you know whether your command completed successfully and to display any errors.

Syntax: 01 login [USER@DOMAIN.COM] [PASSWORD]

01 login Password
01 OK LOGIN ok.

Listing Folders

Once logged in, you can view the folder structure of the email account. This will display all folders that currently exist for this email account:

Syntax: 02 list "" "*"

02 list "" "*"
* LIST (\Unmarked \HasNoChildren) "." "INBOX"
02 OK LIST completed

Notes on this command:
By default, the only mailbox is "INBOX".
The standard format of the list command is 'list "(folder directory)" "(search argument)"'. Specifying an empty folder directory, as in the example above, will show everything from the root folder. The asterisk (*) in the search argument lists all results. If you put a search string in, keep in mind that it is case-sensitive.
Take note of the output. The solo-character in double-quotes is the separator between parent and child folders. This character is typically a period (.) or a forward slash (/). In this case, it is the period. This character will be used in the future when accessing, creating, renaming, and deleting sub-folders.

Mailbox Status

Using the 'status' command, you can review the number of messages in the mailbox, recently received emails (since last accessing the mailbox), and unseen (unread) emails. The mailbox name is not case-sensitive unless you have two folders with the same name, "Folder" and "folder," for example. It is good practice to use case-sensitive entries.


03 status INBOX messages
03 OK STATUS Completed.

03 status INBOX recent
03 OK STATUS Completed.

03 status INBOX unseen
03 OK STATUS Completed.

Selecting a Mailbox

To view the emails, you will first have to select the mailbox from which you want to read.

Syntax: 04 select [MAILBOX]

04 select INBOX
* FLAGS (\Draft \Answered \Flagged \Deleted \Seen \Recent)
* OK [PERMANENTFLAGS (\* \Draft \Answered \Flagged \Deleted \Seen)] Limited
* OK [UIDVALIDITY 1287366396] Ok
* OK [MYRIGHTS "acdilrsw"] ACL

Listing Emails

"status" shows you the number of emails in a mailbox. "fetch" displays the emails in the mailbox, along with the corresponding flags. The following command will fetch all emails from first to last (1:*), and display their flags:

Syntax: 05 fetch 1:* flags

05 fetch 1:* flags
* 1 FETCH (FLAGS (\Recent))
* 2 FETCH (FLAGS (\Recent))
* 3 FETCH (FLAGS (\Recent))
05 OK FETCH completed.

Viewing Full Headers

If you want to view the headers of the email and no message content, you can use the following command. Headers are helpful for determining the true sender of an email and delivery latency. The following output will show that the email was sent from an iPod:

Syntax: 06 fetch [EMAIL ID] body[header]

06 fetch 1 body[header]
Received: (qmail 7878 invoked from network); 17 Oct 2010 19:05:28 -0700
Received: from (HELO ? (
by with SMTP; 17 Oct 2010 19:05:28 -0700
Subject: Test Email
From: "Test Account" <>
Content-Type: text/plain; charset=us-ascii
Message-Id: <>
Date: Sun, 17 Oct 2010 19:04:20 -0700
To: "" <>
Content-Transfer-Encoding: 7bit
Mime-Version: 1.0 (iPod Mail 8B117)
X-Mailer: iPod Mail (8B117)
* 1 FETCH (FLAGS (\Seen \Recent))
06 OK FETCH completed.

Viewing Specific Header Elements

You can also specify which elements of the header you would like to display. The output from the example below will show you the "From", "To", "Subject", and "Date" fields. It will not display any content from the email message.

Syntax: 07 fetch [EMAIL ID] (body[header.fields (from to subject date)])

07 fetch 1 (body[header.fields (from to subject date)])
* 1 FETCH (BODY[HEADER.FIELDS ("from" "to" "subject" "date")] {158}
Subject: Test Email
From: "Test account" <>
Date: Sun, 17 Oct 2010 19:04:20 -0700
To: "" <>

07 OK FETCH completed.

Reading Email

To read the contents of a message (but not the subject), run the following command:

Syntax: 08 fetch [EMAIL ID] body[text]

08 fetch 1 body[text]
* 1 FETCH (BODY[TEXT] {23}
This is a test email.
08 OK FETCH completed.

Setting Flags

Every email will have at least one flag. Typically, it will be the "\Seen" or "\Recent" flag. The following flags can be set on an email:
NonJunk - email marked as Not Spam by email client
$Forwarded - email which has been forwarded to other recipients; not by alias
\Draft - email marked as a draft
\Answered - email which has been replied
\Flagged - email which has been flagged/starred
\Deleted - email marked for deletion
\Recent - new email since last select or examine on mailbox
\Unseen - email which has not been read
\Seen - email which has been read
The flags need to be entered as provided, including the backslash (\) and dollar sign ($). The flag itself is not case-sensitive.

Add (+flags):

Syntax: 09 store [EMAIL ID] +flags [FLAGS]

09 store 1 +flags \Answered
* 1 FETCH (FLAGS (\Answered \Seen \Recent))
09 OK STORE completed.

Remove (-flags):

Syntax: 09 store [EMAIL ID] -flags [FLAGS]

09 store 2 -flags \Recent
* 2 FETCH (FLAGS ())
09 OK STORE completed.

Set the specified (flags):

Syntax: 09 store [EMAIL ID] flags [FLAGS]

09 store 3 flags \Seen
* 3 FETCH (FLAGS (\Seen))
09 OK STORE completed.

For consistency, I will use the same command tag, 05, for listing the flags that have been set. Here is the verification that the flags have been set:

05 fetch 1:* flags
* 1 FETCH (FLAGS (\Answered \Seen \Recent))
* 2 FETCH (FLAGS ())
* 3 FETCH (FLAGS (\Seen))
05 OK FETCH completed.

Deleting Emails

Deleting an email is a 2-part process.
The email will need to be marked for deletion. This can be done by either adding (+flags) or setting (flags) the \Deleted flag.
Once the email has been flagged for deletion, it can be deleted through the use of close or expunge. When viewing an email through WebMail or IMAP in an email client, this will appear as a strike-through on the message; Sender - Subject - Date. When this occurs, it is simply pending the execution of close or expunge.

Syntax: 10 store [EMAIL ID] [+FLAGS|FLAGS] \Deleted

10 store 3 flags \Deleted
* 3 FETCH (FLAGS (\Deleted))
10 OK STORE completed.

See the next section for finishing the removal.

Close or Expunge

Using either close or expunge will delete the emails flagged with \Deleted. Using expunge will simply delete the emails from the folder, and nothing else. Using close, however, deletes any emails flagged with \Deleted, and closes the mailbox currently in use. Take note that it will automatically delete the email and not place it in the Trash mailbox for later removal. Also, close will not confirm removal, contrary to expunge. The expunge output will state the email ID that has been expunged (NOT the number of emails removed - don't worry).
In this output, email ID 3 has been expunged, per the previous command.

Syntax: 11 expunge

11 expunge
11 OK EXPUNGE completed
Syntax: 11 close
11 close
11 OK mailbox closed.

Creating Mailboxes

If you want to create a sub-mailbox on your email account, use the create command. You will need to enter the full path. The command will create sub-mailboxes, even if the parent does not exist.

Syntax: 12 create [MAILBOX.SUBMAILBOX]

12 create INBOX.Sub
12 OK "INBOX.Sub" created.
Creation can be verified with the list command.
02 list "" "*"
* LIST (\Unmarked \HasChildren) "." "INBOX"
* LIST (\HasNoChildren) "." "INBOX.Sub"
02 OK LIST completed

Renaming Mailboxes

When renaming a mailbox, enter the full path of the current mailbox name, followed by the full path with the new mailbox name.


13 rename INBOX.Sub INBOX.Sub2
13 OK Folder renamed.

You can confirm that the renaming was successful with the list command:

02 list "" "*"
* LIST (\Unmarked \HasChildren) "." "INBOX"
* LIST (\HasNoChildren) "." "INBOX.Sub2"
02 OK LIST completed

Moving and Copying Emails

There is no move command, but you can accomplish the same thing by copying the email between mailboxes, then deleting the email from the original location. The copy command will only copy to one directory at a time. The copy command needs to be repeated as many times as necessary.

Syntax: 14 copy [EMAIL ID] [MAILBOX]

14 copy 2 INBOX.Sub2
14 OK [COPYUID 1287371623 6 1] COPY completed.

Deleting Mailboxes

Deleting a mailbox must be done with caution, as it will remove any emails and sub-mailboxes it may contain.
Once assured that it is empty, or contains only emails you want to remove, run this command to delete an entire mailbox:

Syntax: 15 delete [MAILBOX.SUBMAILBOX]

15 delete INBOX.Sub2
15 OK Folder deleted.
The INBOX cannot be deleted.

Viewing Subscribed Folders

When you run the list command, it may display more folders than you are seeing in WebMail and/or email client. This is because you are not subscribed to those folders. We will now check to which folders you are subscribed.
I have created a new sub-folder, "Subscribe." Newly created folders are not subscribed. All mailboxes will appear with the list command, but may not with the lsub command.

Syntax: 16 lsub "" "*"

16 lsub "" "*"
* LSUB (\Unmarked \HasNoChildren) "." "INBOX"
16 OK LSUB completed


If you want to subscribe to a mailbox, use the following command:

Syntax: 17 subscribe [MAILBOX]

17 subscribe INBOX.Subscribe
17 OK Folder subscribed.

To verify your subscription, run the lsub command:

16 lsub "" "*"
* LSUB (\HasNoChildren) "." "INBOX.Subscribe"
* LSUB (\Unmarked \HasChildren) "." "INBOX"
16 OK LSUB completed


If you no longer want to see emails from a particular subscribed mailbox, you can unsubscribe from it at any time. The server will continue to receive emails for that mailbox, if a server-side (WebMail) filter exists, but they will not be displayed in the email client.

Syntax: 18 unsubscribe [MAILBOX]

18 unsubscribe INBOX.Subscribe
18 OK Folder unsubscribed.

To verify your unsubscription, run the lsub command:
16 lsub "" "*"
* LSUB (\Unmarked \HasNoChildren) "." "INBOX"
16 OK LSUB completed

Log out

When you are finished with your IMAP session via Telnet, you can issue the logout command:

Syntax: 19 logout

19 logout
* BYE Courier-IMAP server shutting down
19 OK LOGOUT completed
Connection closed by foreign host.

Leave a Reply