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.
Connecting
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 example.com 143
Trying 5.5.5.5...
Connected to example.com.
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA IDLE ACL ACL2=UNION STARTTLS] Courier-IMAP ready. Copyright 1998-2004 Double Precision, Inc. See COPYING for distribution information.
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 user@example.com 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.
Syntax: 03 status [MAILBOX] [MESSAGES|RECENT|UNSEEN]
03 status INBOX messages
* STATUS "INBOX" (MESSAGES 3)
03 OK STATUS Completed.
03 status INBOX recent
* STATUS "INBOX" (RECENT 3)
03 OK STATUS Completed.
03 status INBOX unseen
* STATUS "INBOX" (UNSEEN 3)
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
* 3 EXISTS
* 3 RECENT
* OK [UIDVALIDITY 1287366396] Ok
* OK [MYRIGHTS "acdilrsw"] ACL
04 OK [READ-WRITE] Ok
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]
* 1 FETCH (BODY[HEADER] {593}
Received: (qmail 7878 invoked from network); 17 Oct 2010 19:05:28 -0700
Received: from cpe-6-6-6-6.socal.res.rr.com (HELO ?192.168.0.0?) (6.6.6.6)
by other.server.com with SMTP; 17 Oct 2010 19:05:28 -0700
Subject: Test Email
From: "Test Account" <test@example.com>
Content-Type: text/plain; charset=us-ascii
Message-Id: <8F31B564-E40A-41FC-BB46-45D60EBF10D6@example.com>
Date: Sun, 17 Oct 2010 19:04:20 -0700
To: "user@example.com" <user@example.com>
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" <test@example.com>
Date: Sun, 17 Oct 2010 19:04:20 -0700
To: "user@example.com" <user@example.com>
)
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
* 3 EXPUNGE
* 2 EXISTS
* 0 RECENT
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.
Syntax: 13 rename [MAILBOX.SUBMAILBOX] [MAILBOX.SUBMAILBOX2]
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
Subscribing
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
Unsubscribing
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.