curl / Mailing Lists / curl-library / Single Mail

curl-library

Question about CURLOPT_CUSTOMREQUEST in IMAP program

From: kinglt <kinglotterry_at_163.com>
Date: Sat, 05 May 2018 14:15:49 +0800

Hello.
May I get help? 

In my program, I want to use libcurl search a particular email, then
just fetch back the BODY[TEXT](its size is far smaller than header),
and delete it in server. But libcurl didn't writeback the email body.
So I use libcurl's example with little change to test this. It got the
same problem. Below is the code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
struct MemoryStruct {
        char *memory;
        size_t size;
};
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void
*userp)
{
        size_t realsize = size * nmemb;
        struct MemoryStruct *mem = (struct MemoryStruct *)userp;
        mem->memory = realloc(mem->memory, mem->size + realsize);
        memcpy(&(mem->memory[mem->size]), contents, realsize);
        mem->size += realsize;
        mem->memory[mem->size] = '\0';
        return realsize;
}
int main()
{
        CURL *chdl;
        struct MemoryStruct chunk;
        chunk.memory = malloc(1);
        chunk.size = 0;   
        chunk.memory[0] = '\0';

        chdl = curl_easy_init();
        curl_easy_setopt(chdl, CURLOPT_VERBOSE, 1);
        curl_easy_setopt(chdl, CURLOPT_URL,
"imap://example.com/INBOX");
        curl_easy_setopt(chdl, CURLOPT_USERPWD, "user:password");
        curl_easy_setopt(chdl, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);
        curl_easy_setopt(chdl, CURLOPT_WRITEDATA, (void *)&chunk);
        curl_easy_setopt(chdl, CURLOPT_CUSTOMREQUEST, "FETCH 2
BODY[TEXT]");
        curl_easy_perform(chdl);
        curl_easy_cleanup(chdl);
        
        printf("received(%d bytes):%s\n",chunk.size, chunk.memory);
        return 0;
}

This is the running result:

*   Trying xxx.xxx.xxx.xxx...
* TCP_NODELAY set
* Connected to imap.xxx.com (xxx.xxx.xxx.xxx) port 143 (#0)
< * OK IMAP4 ready
> A001 CAPABILITY
< * CAPABILITY IMAP4 IMAP4rev1 UIDPLUS IDLE AUTH=PLAIN STARTTLS
< A001 OK completed
> A002 AUTHENTICATE PLAIN
< + 
> xxxxxxxxxxxxxxxxxxxxxxxxxxxx
< A002 OK LOGIN completed
> A003 SELECT INBOX
< * FLAGS (\Answered \Seen \Flagged \Deleted \Draft \Recent)
< * 2 EXISTS
< * 0 RECENT
< * OK [UNSEEN 0]
< * OK [UIDVALIDITY 1] UID validity status.
< * OK [UIDNEXT 2698] Predicted next UID
< * OK [PERMANENTFLAGS (\Answered \Seen \Flagged \Deleted \Draft
\Recent)] Permanent flags.
< A003 OK [READ-WRITE] SELECT completed
> A004 FETCH 2 BODY[TEXT]
< * 2 FETCH (BODY[TEXT] {322}
<
PGRpdiBzdHlsZT0ibWluLWhlaWdodDoyMnB4O21hcmdpbi1ib3R0b206OHB4OyI+MTIzNDU
2
<
Nzg5MHRlc3QxPC9kaXY+PGRpdiBzdHlsZT0ibWluLWhlaWdodDoyMnB5O21hcmdpbi1ib3R
0
<
b206OHB4OyI+PGJyPjwvZGl2PjxzcGFuIGNsYXNzPSJtYWlsLWZvb3RlciIgYXJpYS1oaWR
k
<
ZW49InRydWUiPuWPkeiHquaIkeeahGlQaG9uZTwvc3Bhbj48ZGl2IGlkPSJvcmlnaW5hbC1
j
< b250ZW50Ij48L2Rpdj4=

<
< )
< A004 OK FETCH completed
* Connection #0 to host imap.xxx.com left intact
received(29 bytes):* 2 FETCH (BODY[TEXT] {322}

Libcurl just writeback one line "* 2 FETCH (BODY[TEXT] {322}". 
How can I resolve the problem?

Best regardS.

-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2018-05-05