cURL / Mailing Lists / curl-library / Single Mail


Re: LibCurl IMAP C++ receive only subject of most recent mail?

From: Ray Satiro via curl-library <>
Date: Fri, 26 Feb 2016 02:29:13 -0500

On 2/25/2016 3:27 PM, Jules van der Toorn wrote:
> Hi,
> This question exists of 2 subquestions:
> 1.
> How do i set the UID, so that the latest email will be the result?
> Right now, UID=1 means the first mail that i've ever received.
> 2.
> If i get info about the mail using (int)res, i get loads of
> information, but how can i choose to only receive the subject?
> Thanks!

To get the most recent e-mail you should be able to use an asterisk for
the fetch to get the most recent UID.


Which looks like this in the IMAP URL format:


If you do that in recent libcurl it should give you the subject of the
most recent e-mail in the body (ie the data written to the function
specified by CURLOPT_WRITEFUNCTION). The server will set the 'Seen' flag
on the UID if it's not set already. You can avoid setting 'Seen' by
using BODY.PEEK instead of BODY, but that is not supported in the URL
format which means you would have to send a custom command and parse the
result in the headers.

I think what you're asking is unusual because I can imagine in most
cases in a mailbox folder you may receive an arbitrary number of e-mails
and using the method above you only get the most recent e-mail subject.
Below is a different way.

You may actually end up wanting to get all UIDs you haven't seen since 
the last time you checked. It would involve starting from one UID and 
then every so often checking for newer UIDs.
Once you have a UID with a subject you've seen and you want all new UIDs 
after that you'd issue a CURLOPT_CUSTOMREQUEST. For example if you've 
seen UID 14 subject and you decide you want all UIDs after that you'd 
issue a custom request of "UID SEARCH 14:*". If there were no later 
messages you'd see this in the body:
Check every so often and let's say new messages come in you'd see:
* SEARCH 14 15 or * SEARCH 14 15 16 or * SEARCH 14 16, etc.
You'd have to parse that and get those numbers. Then for each new number 
you'd get the subject, then update the last seen as 16, wait some time 
and loop.
Most custom commands though it's complicated because the result of the 
command usually ends up unparsed in the header instead of parsed in the 
body. So if you wanted to PEEK at subjects you'd issue a custom command 
and then parse the header data. It could be done in C++ by first 
clearing the header data, then issuing the custom command, then parsing 
the result like this:
cmatch match;
if(regex_search(headers, match, regex("^(?:.*\r\n)*\\* [0-9]+ FETCH 
\\(BODY\\[HEADER\\.FIELDS \\(SUBJECT\\)\\] \\{([0-9]{1,4})\\}\r\n"))) {
   string subject(headers, match.length(), stoi(match[1]));
   subject.erase(subject.find_last_not_of(" \t\r\n") + 1);
   cout << subject << endl;
If you find you have to parse the results of custom commands frequently 
that can become quite cumbersome and you may want to look into a 
dedicated IMAP library. Note also there is a bug right now if your UID 
search results are too much (like thousands) it is truncated, see #90 at

List admin:
Received on 2016-02-26