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 using implicit SSL
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 SMTP
3513498266Sopenharmony_ci * capabilities. It builds on the smtp-mail.c example to add authentication
3613498266Sopenharmony_ci * and, more importantly, transport security to protect the authentication
3713498266Sopenharmony_ci * details from being snooped.
3813498266Sopenharmony_ci *
3913498266Sopenharmony_ci * Note that this example requires libcurl 7.20.0 or above.
4013498266Sopenharmony_ci */
4113498266Sopenharmony_ci
4213498266Sopenharmony_ci#define FROM_MAIL     "<sender@example.com>"
4313498266Sopenharmony_ci#define TO_MAIL       "<recipient@example.com>"
4413498266Sopenharmony_ci#define CC_MAIL       "<info@example.com>"
4513498266Sopenharmony_ci
4613498266Sopenharmony_cistatic const char *payload_text =
4713498266Sopenharmony_ci  "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
4813498266Sopenharmony_ci  "To: " TO_MAIL "\r\n"
4913498266Sopenharmony_ci  "From: " FROM_MAIL "\r\n"
5013498266Sopenharmony_ci  "Cc: " CC_MAIL "\r\n"
5113498266Sopenharmony_ci  "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
5213498266Sopenharmony_ci  "rfcpedant.example.org>\r\n"
5313498266Sopenharmony_ci  "Subject: SMTP example message\r\n"
5413498266Sopenharmony_ci  "\r\n" /* empty line to divide headers from body, see RFC 5322 */
5513498266Sopenharmony_ci  "The body of the message starts here.\r\n"
5613498266Sopenharmony_ci  "\r\n"
5713498266Sopenharmony_ci  "It could be a lot of lines, could be MIME encoded, whatever.\r\n"
5813498266Sopenharmony_ci  "Check RFC 5322.\r\n";
5913498266Sopenharmony_ci
6013498266Sopenharmony_cistruct upload_status {
6113498266Sopenharmony_ci  size_t bytes_read;
6213498266Sopenharmony_ci};
6313498266Sopenharmony_ci
6413498266Sopenharmony_cistatic size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
6513498266Sopenharmony_ci{
6613498266Sopenharmony_ci  struct upload_status *upload_ctx = (struct upload_status *)userp;
6713498266Sopenharmony_ci  const char *data;
6813498266Sopenharmony_ci  size_t room = size * nmemb;
6913498266Sopenharmony_ci
7013498266Sopenharmony_ci  if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
7113498266Sopenharmony_ci    return 0;
7213498266Sopenharmony_ci  }
7313498266Sopenharmony_ci
7413498266Sopenharmony_ci  data = &payload_text[upload_ctx->bytes_read];
7513498266Sopenharmony_ci
7613498266Sopenharmony_ci  if(data) {
7713498266Sopenharmony_ci    size_t len = strlen(data);
7813498266Sopenharmony_ci    if(room < len)
7913498266Sopenharmony_ci      len = room;
8013498266Sopenharmony_ci    memcpy(ptr, data, len);
8113498266Sopenharmony_ci    upload_ctx->bytes_read += len;
8213498266Sopenharmony_ci
8313498266Sopenharmony_ci    return len;
8413498266Sopenharmony_ci  }
8513498266Sopenharmony_ci
8613498266Sopenharmony_ci  return 0;
8713498266Sopenharmony_ci}
8813498266Sopenharmony_ci
8913498266Sopenharmony_ciint main(void)
9013498266Sopenharmony_ci{
9113498266Sopenharmony_ci  CURL *curl;
9213498266Sopenharmony_ci  CURLcode res = CURLE_OK;
9313498266Sopenharmony_ci  struct curl_slist *recipients = NULL;
9413498266Sopenharmony_ci  struct upload_status upload_ctx = { 0 };
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci  curl = curl_easy_init();
9713498266Sopenharmony_ci  if(curl) {
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 is the URL for your mailserver. Note the use of smtps:// rather
10313498266Sopenharmony_ci     * than smtp:// to request a SSL based connection. */
10413498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_URL, "smtps://mainserver.example.net");
10513498266Sopenharmony_ci
10613498266Sopenharmony_ci    /* If you want to connect to a site who is not using a certificate that is
10713498266Sopenharmony_ci     * signed by one of the certs in the CA bundle you have, you can skip the
10813498266Sopenharmony_ci     * verification of the server's certificate. This makes the connection
10913498266Sopenharmony_ci     * A LOT LESS SECURE.
11013498266Sopenharmony_ci     *
11113498266Sopenharmony_ci     * If you have a CA cert for the server stored someplace else than in the
11213498266Sopenharmony_ci     * default bundle, then the CURLOPT_CAPATH option might come handy for
11313498266Sopenharmony_ci     * you. */
11413498266Sopenharmony_ci#ifdef SKIP_PEER_VERIFICATION
11513498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
11613498266Sopenharmony_ci#endif
11713498266Sopenharmony_ci
11813498266Sopenharmony_ci    /* If the site you are connecting to uses a different host name that what
11913498266Sopenharmony_ci     * they have mentioned in their server certificate's commonName (or
12013498266Sopenharmony_ci     * subjectAltName) fields, libcurl will refuse to connect. You can skip
12113498266Sopenharmony_ci     * this check, but this will make the connection less secure. */
12213498266Sopenharmony_ci#ifdef SKIP_HOSTNAME_VERIFICATION
12313498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
12413498266Sopenharmony_ci#endif
12513498266Sopenharmony_ci
12613498266Sopenharmony_ci    /* Note that this option is not strictly required, omitting it will result
12713498266Sopenharmony_ci     * in libcurl sending the MAIL FROM command with empty sender data. All
12813498266Sopenharmony_ci     * autoresponses should have an empty reverse-path, and should be directed
12913498266Sopenharmony_ci     * to the address in the reverse-path which triggered them. Otherwise,
13013498266Sopenharmony_ci     * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more
13113498266Sopenharmony_ci     * details.
13213498266Sopenharmony_ci     */
13313498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_MAIL);
13413498266Sopenharmony_ci
13513498266Sopenharmony_ci    /* Add two recipients, in this particular case they correspond to the
13613498266Sopenharmony_ci     * To: and Cc: addressees in the header, but they could be any kind of
13713498266Sopenharmony_ci     * recipient. */
13813498266Sopenharmony_ci    recipients = curl_slist_append(recipients, TO_MAIL);
13913498266Sopenharmony_ci    recipients = curl_slist_append(recipients, CC_MAIL);
14013498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
14113498266Sopenharmony_ci
14213498266Sopenharmony_ci    /* We are using a callback function to specify the payload (the headers and
14313498266Sopenharmony_ci     * body of the message). You could just use the CURLOPT_READDATA option to
14413498266Sopenharmony_ci     * specify a FILE pointer to read from. */
14513498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
14613498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
14713498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
14813498266Sopenharmony_ci
14913498266Sopenharmony_ci    /* Since the traffic will be encrypted, it is useful to turn on debug
15013498266Sopenharmony_ci     * information within libcurl to see what is happening during the
15113498266Sopenharmony_ci     * transfer */
15213498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
15313498266Sopenharmony_ci
15413498266Sopenharmony_ci    /* Send the message */
15513498266Sopenharmony_ci    res = curl_easy_perform(curl);
15613498266Sopenharmony_ci
15713498266Sopenharmony_ci    /* Check for errors */
15813498266Sopenharmony_ci    if(res != CURLE_OK)
15913498266Sopenharmony_ci      fprintf(stderr, "curl_easy_perform() failed: %s\n",
16013498266Sopenharmony_ci              curl_easy_strerror(res));
16113498266Sopenharmony_ci
16213498266Sopenharmony_ci    /* Free the list of recipients */
16313498266Sopenharmony_ci    curl_slist_free_all(recipients);
16413498266Sopenharmony_ci
16513498266Sopenharmony_ci    /* Always cleanup */
16613498266Sopenharmony_ci    curl_easy_cleanup(curl);
16713498266Sopenharmony_ci  }
16813498266Sopenharmony_ci
16913498266Sopenharmony_ci  return (int)res;
17013498266Sopenharmony_ci}
171