1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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 https://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
24 * IMAP example showing how to send e-mails
30 #include <curl/curl.h>
32 /* This is a simple example showing how to send mail using libcurl's IMAP
35 * Note that this example requires libcurl 7.30.0 or above.
38 #define FROM "<sender@example.org>"
39 #define TO "<addressee@example.net>"
40 #define CC "<info@example.org>"
42 static const char *payload_text[] = {
43 "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
45 "From: " FROM "(Example User)\r\n",
46 "Cc: " CC "(Another example User)\r\n",
48 "<dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n",
49 "Subject: IMAP example message\r\n",
50 "\r\n", /* empty line to divide headers from body, see RFC5322 */
51 "The body of the message starts here.\r\n",
53 "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
58 struct upload_status {
62 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
64 struct upload_status *upload_ctx = (struct upload_status *)userp;
67 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
71 data = payload_text[upload_ctx->lines_read];
74 size_t len = strlen(data);
75 memcpy(ptr, data, len);
76 upload_ctx->lines_read++;
87 CURLcode res = CURLE_OK;
88 struct upload_status upload_ctx;
90 upload_ctx.lines_read = 0;
92 curl = curl_easy_init();
94 /* Set username and password */
95 curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
96 curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
98 /* This will create a new message 100. Note that you should perform an
99 * EXAMINE command to obtain the UID of the next message to create and a
100 * SELECT to ensure you are creating the message in the OUTBOX. */
101 curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/100");
103 /* In this case, we're using a callback function to specify the data. You
104 * could just use the CURLOPT_READDATA option to specify a FILE pointer to
106 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
107 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
108 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
110 /* Perform the append */
111 res = curl_easy_perform(curl);
113 /* Check for errors */
115 fprintf(stderr, "curl_easy_perform() failed: %s\n",
116 curl_easy_strerror(res));
119 curl_easy_cleanup(curl);