113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci
2513498266Sopenharmony_ci/* <DESC>
2613498266Sopenharmony_ci * Send email with IMAP
2713498266Sopenharmony_ci * </DESC>
2813498266Sopenharmony_ci */
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci#include <stdio.h>
3113498266Sopenharmony_ci#include <string.h>
3213498266Sopenharmony_ci#include <curl/curl.h>
3313498266Sopenharmony_ci
3413498266Sopenharmony_ci/* This is a simple example showing how to send mail using libcurl's IMAP
3513498266Sopenharmony_ci * capabilities.
3613498266Sopenharmony_ci *
3713498266Sopenharmony_ci * Note that this example requires libcurl 7.30.0 or above.
3813498266Sopenharmony_ci */
3913498266Sopenharmony_ci
4013498266Sopenharmony_ci#define FROM    "<sender@example.org>"
4113498266Sopenharmony_ci#define TO      "<addressee@example.net>"
4213498266Sopenharmony_ci#define CC      "<info@example.org>"
4313498266Sopenharmony_ci
4413498266Sopenharmony_cistatic const char *payload_text =
4513498266Sopenharmony_ci  "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
4613498266Sopenharmony_ci  "To: " TO "\r\n"
4713498266Sopenharmony_ci  "From: " FROM "(Example User)\r\n"
4813498266Sopenharmony_ci  "Cc: " CC "(Another example User)\r\n"
4913498266Sopenharmony_ci  "Message-ID: "
5013498266Sopenharmony_ci  "<dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n"
5113498266Sopenharmony_ci  "Subject: IMAP example message\r\n"
5213498266Sopenharmony_ci  "\r\n" /* empty line to divide headers from body, see RFC 5322 */
5313498266Sopenharmony_ci  "The body of the message starts here.\r\n"
5413498266Sopenharmony_ci  "\r\n"
5513498266Sopenharmony_ci  "It could be a lot of lines, could be MIME encoded, whatever.\r\n"
5613498266Sopenharmony_ci  "Check RFC 5322.\r\n";
5713498266Sopenharmony_ci
5813498266Sopenharmony_cistruct upload_status {
5913498266Sopenharmony_ci  size_t bytes_read;
6013498266Sopenharmony_ci};
6113498266Sopenharmony_ci
6213498266Sopenharmony_cistatic size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
6313498266Sopenharmony_ci{
6413498266Sopenharmony_ci  struct upload_status *upload_ctx = (struct upload_status *)userp;
6513498266Sopenharmony_ci  const char *data;
6613498266Sopenharmony_ci  size_t room = size * nmemb;
6713498266Sopenharmony_ci
6813498266Sopenharmony_ci  if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
6913498266Sopenharmony_ci    return 0;
7013498266Sopenharmony_ci  }
7113498266Sopenharmony_ci
7213498266Sopenharmony_ci  data = &payload_text[upload_ctx->bytes_read];
7313498266Sopenharmony_ci
7413498266Sopenharmony_ci  if(*data) {
7513498266Sopenharmony_ci    size_t len = strlen(data);
7613498266Sopenharmony_ci    if(room < len)
7713498266Sopenharmony_ci      len = room;
7813498266Sopenharmony_ci    memcpy(ptr, data, len);
7913498266Sopenharmony_ci    upload_ctx->bytes_read += len;
8013498266Sopenharmony_ci
8113498266Sopenharmony_ci    return len;
8213498266Sopenharmony_ci  }
8313498266Sopenharmony_ci
8413498266Sopenharmony_ci  return 0;
8513498266Sopenharmony_ci}
8613498266Sopenharmony_ci
8713498266Sopenharmony_ciint main(void)
8813498266Sopenharmony_ci{
8913498266Sopenharmony_ci  CURL *curl;
9013498266Sopenharmony_ci  CURLcode res = CURLE_OK;
9113498266Sopenharmony_ci
9213498266Sopenharmony_ci  curl = curl_easy_init();
9313498266Sopenharmony_ci  if(curl) {
9413498266Sopenharmony_ci    size_t filesize;
9513498266Sopenharmony_ci    long infilesize = LONG_MAX;
9613498266Sopenharmony_ci    struct upload_status upload_ctx = { 0 };
9713498266Sopenharmony_ci
9813498266Sopenharmony_ci    /* Set username and password */
9913498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
10013498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
10113498266Sopenharmony_ci
10213498266Sopenharmony_ci    /* This will create a new message in folder "Sent". */
10313498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/Sent");
10413498266Sopenharmony_ci
10513498266Sopenharmony_ci    /* In this case, we are using a callback function to specify the data. You
10613498266Sopenharmony_ci     * could just use the CURLOPT_READDATA option to specify a FILE pointer to
10713498266Sopenharmony_ci     * read from. */
10813498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
10913498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
11013498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
11113498266Sopenharmony_ci
11213498266Sopenharmony_ci    filesize = strlen(payload_text);
11313498266Sopenharmony_ci    if(filesize <= LONG_MAX)
11413498266Sopenharmony_ci      infilesize = (long)filesize;
11513498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_INFILESIZE, infilesize);
11613498266Sopenharmony_ci
11713498266Sopenharmony_ci    /* Perform the append */
11813498266Sopenharmony_ci    res = curl_easy_perform(curl);
11913498266Sopenharmony_ci
12013498266Sopenharmony_ci    /* Check for errors */
12113498266Sopenharmony_ci    if(res != CURLE_OK)
12213498266Sopenharmony_ci      fprintf(stderr, "curl_easy_perform() failed: %s\n",
12313498266Sopenharmony_ci              curl_easy_strerror(res));
12413498266Sopenharmony_ci
12513498266Sopenharmony_ci    /* Always cleanup */
12613498266Sopenharmony_ci    curl_easy_cleanup(curl);
12713498266Sopenharmony_ci  }
12813498266Sopenharmony_ci
12913498266Sopenharmony_ci  return (int)res;
13013498266Sopenharmony_ci}
131