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 SMTP email with the multi interface
2713498266Sopenharmony_ci * </DESC>
2813498266Sopenharmony_ci */
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci#include <string.h>
3113498266Sopenharmony_ci#include <curl/curl.h>
3213498266Sopenharmony_ci
3313498266Sopenharmony_ci/* This is an example showing how to send mail using libcurl's SMTP
3413498266Sopenharmony_ci * capabilities. It builds on the smtp-mail.c example to demonstrate how to use
3513498266Sopenharmony_ci * libcurl's multi interface.
3613498266Sopenharmony_ci */
3713498266Sopenharmony_ci
3813498266Sopenharmony_ci#define FROM_MAIL     "<sender@example.com>"
3913498266Sopenharmony_ci#define TO_MAIL       "<recipient@example.com>"
4013498266Sopenharmony_ci#define CC_MAIL       "<info@example.com>"
4113498266Sopenharmony_ci
4213498266Sopenharmony_cistatic const char *payload_text =
4313498266Sopenharmony_ci  "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
4413498266Sopenharmony_ci  "To: " TO_MAIL "\r\n"
4513498266Sopenharmony_ci  "From: " FROM_MAIL "\r\n"
4613498266Sopenharmony_ci  "Cc: " CC_MAIL "\r\n"
4713498266Sopenharmony_ci  "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
4813498266Sopenharmony_ci  "rfcpedant.example.org>\r\n"
4913498266Sopenharmony_ci  "Subject: SMTP example message\r\n"
5013498266Sopenharmony_ci  "\r\n" /* empty line to divide headers from body, see RFC 5322 */
5113498266Sopenharmony_ci  "The body of the message starts here.\r\n"
5213498266Sopenharmony_ci  "\r\n"
5313498266Sopenharmony_ci  "It could be a lot of lines, could be MIME encoded, whatever.\r\n"
5413498266Sopenharmony_ci  "Check RFC 5322.\r\n";
5513498266Sopenharmony_ci
5613498266Sopenharmony_cistruct upload_status {
5713498266Sopenharmony_ci  size_t bytes_read;
5813498266Sopenharmony_ci};
5913498266Sopenharmony_ci
6013498266Sopenharmony_cistatic size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
6113498266Sopenharmony_ci{
6213498266Sopenharmony_ci  struct upload_status *upload_ctx = (struct upload_status *)userp;
6313498266Sopenharmony_ci  const char *data;
6413498266Sopenharmony_ci  size_t room = size * nmemb;
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci  if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
6713498266Sopenharmony_ci    return 0;
6813498266Sopenharmony_ci  }
6913498266Sopenharmony_ci
7013498266Sopenharmony_ci  data = &payload_text[upload_ctx->bytes_read];
7113498266Sopenharmony_ci
7213498266Sopenharmony_ci  if(data) {
7313498266Sopenharmony_ci    size_t len = strlen(data);
7413498266Sopenharmony_ci    if(room < len)
7513498266Sopenharmony_ci      len = room;
7613498266Sopenharmony_ci    memcpy(ptr, data, len);
7713498266Sopenharmony_ci    upload_ctx->bytes_read += len;
7813498266Sopenharmony_ci
7913498266Sopenharmony_ci    return len;
8013498266Sopenharmony_ci  }
8113498266Sopenharmony_ci
8213498266Sopenharmony_ci  return 0;
8313498266Sopenharmony_ci}
8413498266Sopenharmony_ci
8513498266Sopenharmony_ciint main(void)
8613498266Sopenharmony_ci{
8713498266Sopenharmony_ci  CURL *curl;
8813498266Sopenharmony_ci  CURLM *mcurl;
8913498266Sopenharmony_ci  int still_running = 1;
9013498266Sopenharmony_ci  struct curl_slist *recipients = NULL;
9113498266Sopenharmony_ci  struct upload_status upload_ctx = { 0 };
9213498266Sopenharmony_ci
9313498266Sopenharmony_ci  curl_global_init(CURL_GLOBAL_DEFAULT);
9413498266Sopenharmony_ci
9513498266Sopenharmony_ci  curl = curl_easy_init();
9613498266Sopenharmony_ci  if(!curl)
9713498266Sopenharmony_ci    return 1;
9813498266Sopenharmony_ci
9913498266Sopenharmony_ci  mcurl = curl_multi_init();
10013498266Sopenharmony_ci  if(!mcurl)
10113498266Sopenharmony_ci    return 2;
10213498266Sopenharmony_ci
10313498266Sopenharmony_ci  /* This is the URL for your mailserver */
10413498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
10513498266Sopenharmony_ci
10613498266Sopenharmony_ci  /* Note that this option is not strictly required, omitting it will result in
10713498266Sopenharmony_ci   * libcurl sending the MAIL FROM command with empty sender data. All
10813498266Sopenharmony_ci   * autoresponses should have an empty reverse-path, and should be directed
10913498266Sopenharmony_ci   * to the address in the reverse-path which triggered them. Otherwise, they
11013498266Sopenharmony_ci   * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details.
11113498266Sopenharmony_ci   */
11213498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_MAIL);
11313498266Sopenharmony_ci
11413498266Sopenharmony_ci  /* Add two recipients, in this particular case they correspond to the
11513498266Sopenharmony_ci   * To: and Cc: addressees in the header, but they could be any kind of
11613498266Sopenharmony_ci   * recipient. */
11713498266Sopenharmony_ci  recipients = curl_slist_append(recipients, TO_MAIL);
11813498266Sopenharmony_ci  recipients = curl_slist_append(recipients, CC_MAIL);
11913498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
12013498266Sopenharmony_ci
12113498266Sopenharmony_ci  /* We are using a callback function to specify the payload (the headers and
12213498266Sopenharmony_ci   * body of the message). You could just use the CURLOPT_READDATA option to
12313498266Sopenharmony_ci   * specify a FILE pointer to read from. */
12413498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
12513498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
12613498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
12713498266Sopenharmony_ci
12813498266Sopenharmony_ci  /* Tell the multi stack about our easy handle */
12913498266Sopenharmony_ci  curl_multi_add_handle(mcurl, curl);
13013498266Sopenharmony_ci
13113498266Sopenharmony_ci  do {
13213498266Sopenharmony_ci    CURLMcode mc = curl_multi_perform(mcurl, &still_running);
13313498266Sopenharmony_ci
13413498266Sopenharmony_ci    if(still_running)
13513498266Sopenharmony_ci      /* wait for activity, timeout or "nothing" */
13613498266Sopenharmony_ci      mc = curl_multi_poll(mcurl, NULL, 0, 1000, NULL);
13713498266Sopenharmony_ci
13813498266Sopenharmony_ci    if(mc)
13913498266Sopenharmony_ci      break;
14013498266Sopenharmony_ci
14113498266Sopenharmony_ci  } while(still_running);
14213498266Sopenharmony_ci
14313498266Sopenharmony_ci  /* Free the list of recipients */
14413498266Sopenharmony_ci  curl_slist_free_all(recipients);
14513498266Sopenharmony_ci
14613498266Sopenharmony_ci  /* Always cleanup */
14713498266Sopenharmony_ci  curl_multi_remove_handle(mcurl, curl);
14813498266Sopenharmony_ci  curl_multi_cleanup(mcurl);
14913498266Sopenharmony_ci  curl_easy_cleanup(curl);
15013498266Sopenharmony_ci  curl_global_cleanup();
15113498266Sopenharmony_ci
15213498266Sopenharmony_ci  return 0;
15313498266Sopenharmony_ci}
154