Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <curl/curl.h>
00031
00032
00033
00034
00035
00036
00037
00038 #define FROM "<sender@example.org>"
00039 #define TO "<addressee@example.net>"
00040 #define CC "<info@example.org>"
00041
00042 static const char *payload_text[] = {
00043 "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
00044 "To: " TO "\r\n",
00045 "From: " FROM "(Example User)\r\n",
00046 "Cc: " CC "(Another example User)\r\n",
00047 "Message-ID: "
00048 "<dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n",
00049 "Subject: IMAP example message\r\n",
00050 "\r\n",
00051 "The body of the message starts here.\r\n",
00052 "\r\n",
00053 "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
00054 "Check RFC5322.\r\n",
00055 NULL
00056 };
00057
00058 struct upload_status {
00059 int lines_read;
00060 };
00061
00062 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
00063 {
00064 struct upload_status *upload_ctx = (struct upload_status *)userp;
00065 const char *data;
00066
00067 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
00068 return 0;
00069 }
00070
00071 data = payload_text[upload_ctx->lines_read];
00072
00073 if(data) {
00074 size_t len = strlen(data);
00075 memcpy(ptr, data, len);
00076 upload_ctx->lines_read++;
00077
00078 return len;
00079 }
00080
00081 return 0;
00082 }
00083
00084 int main(void)
00085 {
00086 CURL *curl;
00087 CURLcode res = CURLE_OK;
00088 const char **p;
00089 long infilesize;
00090 struct upload_status upload_ctx;
00091
00092 upload_ctx.lines_read = 0;
00093
00094 curl = curl_easy_init();
00095 if(curl) {
00096
00097 curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
00098 curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
00099
00100
00101
00102
00103 curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/100");
00104
00105
00106
00107
00108 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
00109 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
00110 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00111
00112 infilesize = 0;
00113 for(p = payload_text; *p; ++p) {
00114 infilesize += (long)strlen(*p);
00115 }
00116 curl_easy_setopt(curl, CURLOPT_INFILESIZE, infilesize);
00117
00118
00119 res = curl_easy_perform(curl);
00120
00121
00122 if(res != CURLE_OK)
00123 fprintf(stderr, "curl_easy_perform() failed: %s\n",
00124 curl_easy_strerror(res));
00125
00126
00127 curl_easy_cleanup(curl);
00128 }
00129
00130 return (int)res;
00131 }