1

i use libcurl to search for mails in my mailbox. But i cannot search mails with mails that inlcudes special chars (ä,ö,ü) in the subject.

int main(void)
{
  CURL *curl;
  CURLcode res = CURLE_OK;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_USERNAME, "myuser");
    curl_easy_setopt(curl, CURLOPT_PASSWORD, "mypassword");

    curl_easy_setopt(curl, CURLOPT_URL, "imaps://imap.gmail.com:993/INBOX");

    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SEARCH SUBJECT Spülmaschine");

    res = curl_easy_perform(curl);

    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    curl_easy_cleanup(curl);
  }

  return (int)res;
}

The mail cannot be found.

I searching a while but i cannot find some tipps.

Have someone any ideas ?

EDIT:// No one of the following trys will work:

curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SEARCH CHARSET UTF-8 SUBJECT \x53\x70\xc3\xbc\x6c\x6d\x61\x73\x63\x68\x69\x6e\x65");


//try it with UTF-7
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SEARCH SUBJECT Sp+APw-lmaschine");


sprintf(strSearchString, "SEARCH CHARSET UTF-8 SUBJECT {%i}\r\n\x53\x70\xc3\xbc\x6c\x6d\x61\x73\x63\x68\x69\x6e\x65", strlen("\x53\x70\xc3\xbc\x6c\x6d\x61\x73\x63\x68\x69\x6e\x65"));
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, strSearchString);
BurningWay
  • 21
  • 3
  • 1
    https://stackoverflow.com/a/7441400/539810 suggests you need to send 8-bit data as a literal. –  May 12 '18 at 23:05

2 Answers2

1

After a long time i found a solution:

int main(void) {
    CURL *curl;
    CURLcode res = CURLE_OK;
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_USERNAME, "myuser");
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "mypass");
        curl_easy_setopt(curl, CURLOPT_URL, "imaps://imap.gmail.com:993");

        // see RFC6855 IMAP Support for UTF-8
        // imap.gmail.com states UTF8=ACCEPT in CAPABILITY response,
        // so enable it to use UTF-8 in quoted strings.
        // Must come after AUTHENTICATE and before SELECT.
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "ENABLE UTF8=ACCEPT");
        res = curl_easy_perform(curl);
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));

        // SELECT INBOX broken out from URL
        if (res == CURLE_OK) {
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SELECT INBOX");
            res = curl_easy_perform(curl);
            if (res != CURLE_OK)
                fprintf(stderr, "curl_easy_perform() failed: %s\n",
                        curl_easy_strerror(res));
        }

        if (res == CURLE_OK) {
            // U+00FC LATIN SMALL LETTER U WITH DIAERESIS is \xC3\xBC in UTF-8
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SEARCH
SUBJECT \"Sp\xC3\xBClmaschine\"");
            res = curl_easy_perform(curl);
            if (res != CURLE_OK)
                fprintf(stderr, "curl_easy_perform() failed: %s\n",
                        curl_easy_strerror(res));
        }

        curl_easy_cleanup(curl);
    }
    return (int) res;
}
BurningWay
  • 21
  • 3
0

While technically against protocol for including 8-bit characters, try SEARCH CHARSET UTF-8 SUBJECT "spülmaschine", and make sure you really are sending UTF-8 and not some other encoding. This will work on many servers. I do not know if you can make libcurl do it the proper way with an IMAP literal.

Note: some server software just doesn't support search that well. Remember, you can always use a tool like socat or python's imaplib, which give you pretty low level access to the protocol for experimenting.

Max
  • 10,701
  • 2
  • 24
  • 48
  • This does not work. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SEARCH CHARSET UTF-8 \"\x53\x70\xc3\xbc\x6c\x6d\x61\x73\x63\x68\x69\x6e\x65\""); curl_easy_perform() return "CURLE_QUOTE_ERROR" – BurningWay May 12 '18 at 22:44
  • the imap module of curl doesn't seem to have very good error handling. Is there a way to get it to print a protocol trace? – Max May 15 '18 at 03:56
  • Libcurl send "SEARCH SUBJECT Sp.lmaschine". The response from the server is "BAD Could not parse command" – BurningWay May 18 '18 at 07:01