I am trying to parse the response returned by IMAP that contains the content of an email. How can I determine the start and end of the message returned from the response retrieved through IMAP?
Below are 2 examples of responses when I retrieve an email through IMAP:
. fetch 3728 body.peek[1]
* 3728 FETCH (BODY[1] {45}
Hello,
This is test message 1.
:(
:)
)
* 3732 FETCH (FLAGS (\Seen \Recent))
* 3733 FETCH (FLAGS (\Seen \Recent))
. OK FETCH completed.
And the second retrieval:
. fetch 3729 body.peek[1]
* 3729 FETCH (BODY[1] {42}
Hello,
This is test message 2. :)
)
The above bracket is part of the message's content.
)
. OK FETCH completed.
First, both retrievals sometimes give different ending when new emails come into the account. Second, it seems like the message end of with a bracket and a . OK FETCH Completed response. But what happens if the content consists of brackets too? Then I may read the wrong bracket and never get the full content.
Like this, when parsing the response from IMAP, how should I interpret the response and get the content correctly?
You should read rfc 3501 …
IMAP strings can be represented in 2 forms:
1) Quotes string.
2) string literal.
String literal synatx:
{number_of_bytes}CRLF
— here you need read number of bytes. NOTE: BYTES not string !!!
Also as IMAP specifications says: you need to be ready to parse any response at any time, also any string part can be string literal.
Coding stable-full functional IMAP client not easy job.