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