Skip to content

Commit

Permalink
add libcurl example for fetching pop e and imap messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pabeni committed May 10, 2013
1 parent c01c403 commit c08ca81
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CPPFLAGS += -Wp,-MD,-MG
CFLAGS += -g -Wall
LIBS+= -lcurl

all: fetchmail

fetchmail: fetchmail.c
$(CC) $(LDFLAGS) -o $@ $? $(LIBS)

clean:
rm -f *.o fetchmail
55 changes: 55 additions & 0 deletions examples/fetchmail.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdio.h>

#include <curl/curl.h>

int main(int argc, char *argv[])
{
char buf[128];
CURLcode res;
CURL *curl;
int use_pop = 1;

if (argc < 4) {
fprintf(stderr, "syntax %s <pop> <user> <pwd> [imap]\n",argv[0]);
return -1;
}

use_pop = (argc == 4);
curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "can't init curl\n");
return -1;
}

/* Set username and password */
snprintf(buf, 128, "%s:%s", argv[2], argv[3]);
curl_easy_setopt(curl, CURLOPT_USERPWD, buf);

/* This will list every message of the given mailbox */
snprintf(buf, 128, "%ss://%s@%s/%s", use_pop? "pop3":"imap", argv[2], argv[1], use_pop?"":"INBOX");
printf("XXXX %s\n", buf);
curl_easy_setopt(curl, CURLOPT_URL, buf);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));

snprintf(buf, 128, "%ss://%s@%s/%s1", use_pop? "pop3":"imap", argv[2], argv[1], use_pop?"":"INBOX;UID=");
printf("XXXX %s\n", buf);
curl_easy_setopt(curl, CURLOPT_URL, buf);
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));

/* always cleanup */
curl_easy_cleanup(curl);
return 0;
}

0 comments on commit c08ca81

Please sign in to comment.