IMAP is a surprisingly simple protocol to implement, particularly in an environment like the Translator. Here are some basic concepts:
IMAP stands for Internet Message Access Protocol usually between an email client and an email server
It’s a protocol intended to let a email program communicate and get email from a email server.
It is a TCP/IP protocol which means it's over a network and consists of a stream of data back and forth
A TCP/IP stream gives guaranteed delivery of the order of data.
Guaranteed means the TCP/IP protocol is designed to try and help the data reach the other side back and forth in a way that the data and order of the data is preserved.
IMAP is a conversational protocol
IMAP is a conversation between the client and the server. The client and server talk to each other using the language of IMAP.
The IMAP client connects and the IMAP server says Hello!
This is a common pattern with many TCP/IP protocols - it’s a way for the server to say - Hi - I speak your language and let’s begin a conversation.
It’s like going up to someone and saying Hola! It signals you are ready for a conversation and that you speak Spanish.
This is what we’d see say with a Gmail IMAP server:
* OK Gimap ready for requests from 69.196.142.158 ql9mb2511069qvb
This is IMAP for Hello!
Next we need to verify our identity since we don't want to give sensitive information to anyone
We need to authenticate - usually with a user name and password. We send a LOGIN command which looks like this:
a01 LOGIN aryn.wiebe@interfaceware.com lkjllkjddkdjsfdhgh\r\n
So IMAP messages start with a unique code for each message we send to the server. In this case a01 - this unique ID that we use to track the command we have sent so when we get a reply from the server then we know what the topic was.
Notice the \r\n newline sequence? There is an interesting story behind that.
Remember that question I asked you?
Then we get the response back to what we said:
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS LITERAL- SPECIAL-USE APPENDLIMIT=35651584
a01 OK aryn.wiebe@interfaceware.com authenticated (Success)
Take special note of the a01 OK.
Each response comes back with ID plus code - either OK, NO or BAD
The ID matches up with the ID we sent before as the client - so this is part of the conversation of telling the client whether the last command it sent was OK or refused NO, or was formatted incorrectly - i.e. BAD an error.
So IMAP clients typically need a method of generating these IDs either randomly or in sequence.
An IMAP client also needs a routine to read the data back from the client to get the data.
Then the client can issue whatever commands they like
It’s a conversation after all - no two conversations are alike.
Select INBOX command - also tells us what the highest ID mail that we can request
Line 8 tells us we have successfully selected the INBOX.
Line 4 also tells us the highest ID mail which exists is 82838.
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)
* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing \*)] Flags permitted.
* OK [UIDVALIDITY 1] UIDs valid.
* 82838 EXISTS
* 0 RECENT
* OK [UIDNEXT 84128] Predicted next UID.
* OK [HIGHESTMODSEQ 8669111]
a02 OK [READ-WRITE] INBOX selected. (Success)
Fetch an email
Here is a sample of an IMAP Fetch response:
Note we can clearly see the header, body, and success response all separated by a boundry (001a113da73205a6ea056272f2ab). We can use this boundary to help parse the email and isolate its parts.
* 1 FETCH (BODY[] {4835}
MIME-Version: 1.0
x-no-auto-attachment: 1
Received: by 10.25.43.137; Wed, 10 Jan 2018 13:45:09 -0800 (PST)
Date: Wed, 10 Jan 2018 13:45:09 -0800
Message-ID: <CAP14fq11RQ3+xEZk7uSmWb4rv1oEDdX66fuq7wJJbkxFv+u1fA@mail.gmail.com>
Subject: Hey!
From: real.person@gmail.com;
To: Aryn Wiebe <aryn.wiebe@interfaceware.com>
Content-Type: multipart/alternative; boundary="001a113da73205a6ea056272f2ab"
--001a113da73205a6ea056272f2ab
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Hi Aryn
This is a sample email!
Thanks,
--001a113da73205a6ea056272f2ab
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html>
...
--001a113da73205a6ea056272f2ab--)
a03 OK Success
What other commands are possible - Chat GPT is helpful resource here
Lot’s of commands are possible like getting a summary of an email (can be helpful to avoid loading a very big email).
You can delete emails.
You can probably filter on them. Ask Chat GPT - it knows a lot about IMAP
The best way to understand the protocol is to open up the IMAPemail Feeder and look at it in the translator
You’ll never get a better way to see how IMAP really works.
Load the IMAP Email Feed and use The string viewing window and Annotation Windows you really get to see how it works.
That’s important since it’s also how you can diagnose problems.
I don’t know about you but I get very frustrated with the spinning disc I often get on my Apple Email client on my phone with no visibility as to what is going wrong.