imap-append.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 /* <DESC>
00024  * IMAP example showing how to send e-mails
00025  * </DESC>
00026  */
00027 
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <curl/curl.h>
00031 
00032 /* This is a simple example showing how to send mail using libcurl's IMAP
00033  * capabilities.
00034  *
00035  * Note that this example requires libcurl 7.30.0 or above.
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", /* empty line to divide headers from body, see RFC5322 */
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     /* Set username and password */
00097     curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
00098     curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
00099 
00100     /* This will create a new message 100. Note that you should perform an
00101      * EXAMINE command to obtain the UID of the next message to create and a
00102      * SELECT to ensure you are creating the message in the OUTBOX. */
00103     curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/100");
00104 
00105     /* In this case, we're using a callback function to specify the data. You
00106      * could just use the CURLOPT_READDATA option to specify a FILE pointer to
00107      * read from. */
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     /* Perform the append */
00119     res = curl_easy_perform(curl);
00120 
00121     /* Check for errors */
00122     if(res != CURLE_OK)
00123       fprintf(stderr, "curl_easy_perform() failed: %s\n",
00124               curl_easy_strerror(res));
00125 
00126     /* Always cleanup */
00127     curl_easy_cleanup(curl);
00128   }
00129 
00130   return (int)res;
00131 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05