1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel (at) 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 #include <stdio.h> 23 #include <string.h> 24 #include <curl/curl.h> 25 26 /* This is a simple example showing how to send mail using libcurl's IMAP 27 * capabilities. 28 * 29 * Note that this example requires libcurl 7.30.0 or above. 30 */ 31 32 #define FROM "<sender (at) example.org>" 33 #define TO "<addressee (at) example.net>" 34 #define CC "<info (at) example.org>" 35 36 static const char *payload_text[] = { 37 "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n", 38 "To: " TO "\r\n", 39 "From: " FROM "(Example User)\r\n", 40 "Cc: " CC "(Another example User)\r\n", 41 "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd (at) rfcpedant.example.org>\r\n", 42 "Subject: IMAP example message\r\n", 43 "\r\n", /* empty line to divide headers from body, see RFC5322 */ 44 "The body of the message starts here.\r\n", 45 "\r\n", 46 "It could be a lot of lines, could be MIME encoded, whatever.\r\n", 47 "Check RFC5322.\r\n", 48 NULL 49 }; 50 51 struct upload_status { 52 int lines_read; 53 }; 54 55 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp) 56 { 57 struct upload_status *upload_ctx = (struct upload_status *)userp; 58 const char *data; 59 60 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) { 61 return 0; 62 } 63 64 data = payload_text[upload_ctx->lines_read]; 65 66 if(data) { 67 size_t len = strlen(data); 68 memcpy(ptr, data, len); 69 upload_ctx->lines_read++; 70 71 return len; 72 } 73 74 return 0; 75 } 76 77 int main(void) 78 { 79 CURL *curl; 80 CURLcode res = CURLE_OK; 81 struct upload_status upload_ctx; 82 83 upload_ctx.lines_read = 0; 84 85 curl = curl_easy_init(); 86 if(curl) { 87 /* Set username and password */ 88 curl_easy_setopt(curl, CURLOPT_USERNAME, "user"); 89 curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret"); 90 91 /* This will create a new message 100. Note that you should perform an 92 * EXAMINE command to obtain the UID of the next message to create and a 93 * SELECT to ensure you are creating the message in the OUTBOX. */ 94 curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/100"); 95 96 /* In this case, we're using a callback function to specify the data. You 97 * could just use the CURLOPT_READDATA option to specify a FILE pointer to 98 * read from. */ 99 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); 100 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); 101 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 102 103 /* Perform the append */ 104 res = curl_easy_perform(curl); 105 106 /* Check for errors */ 107 if(res != CURLE_OK) 108 fprintf(stderr, "curl_easy_perform() failed: %s\n", 109 curl_easy_strerror(res)); 110 111 /* Always cleanup */ 112 curl_easy_cleanup(curl); 113 } 114 115 return (int)res; 116 } 117