fb58b4b23a746e8f16ae5d17b8000e09af7ce147
[platform/upstream/curl.git] / docs / examples / imap-append.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 /* <DESC>
23  * Simple IMAP APPEND use
24  * </DESC>
25  */
26 #include <stdio.h>
27 #include <string.h>
28 #include <curl/curl.h>
29
30 /* This is a simple example showing how to send mail using libcurl's IMAP
31  * capabilities.
32  *
33  * Note that this example requires libcurl 7.30.0 or above.
34  */
35
36 #define FROM    "<sender@example.org>"
37 #define TO      "<addressee@example.net>"
38 #define CC      "<info@example.org>"
39
40 static const char *payload_text[] = {
41   "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
42   "To: " TO "\r\n",
43   "From: " FROM "(Example User)\r\n",
44   "Cc: " CC "(Another example User)\r\n",
45   "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n",
46   "Subject: IMAP example message\r\n",
47   "\r\n", /* empty line to divide headers from body, see RFC5322 */
48   "The body of the message starts here.\r\n",
49   "\r\n",
50   "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
51   "Check RFC5322.\r\n",
52   NULL
53 };
54
55 struct upload_status {
56   int lines_read;
57 };
58
59 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
60 {
61   struct upload_status *upload_ctx = (struct upload_status *)userp;
62   const char *data;
63
64   if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
65     return 0;
66   }
67
68   data = payload_text[upload_ctx->lines_read];
69
70   if(data) {
71     size_t len = strlen(data);
72     memcpy(ptr, data, len);
73     upload_ctx->lines_read++;
74
75     return len;
76   }
77
78   return 0;
79 }
80
81 int main(void)
82 {
83   CURL *curl;
84   CURLcode res = CURLE_OK;
85   struct upload_status upload_ctx;
86
87   upload_ctx.lines_read = 0;
88
89   curl = curl_easy_init();
90   if(curl) {
91     /* Set username and password */
92     curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
93     curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
94
95     /* This will create a new message 100. Note that you should perform an
96      * EXAMINE command to obtain the UID of the next message to create and a
97      * SELECT to ensure you are creating the message in the OUTBOX. */
98     curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/100");
99
100     /* In this case, we're using a callback function to specify the data. You
101      * could just use the CURLOPT_READDATA option to specify a FILE pointer to
102      * read from. */
103     curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
104     curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
105     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
106
107     /* Perform the append */
108     res = curl_easy_perform(curl);
109
110     /* Check for errors */
111     if(res != CURLE_OK)
112       fprintf(stderr, "curl_easy_perform() failed: %s\n",
113               curl_easy_strerror(res));
114
115     /* Always cleanup */
116     curl_easy_cleanup(curl);
117   }
118
119   return (int)res;
120 }