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 TLS 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 port 587 here, 10313498266Sopenharmony_ci * instead of the normal SMTP port (25). Port 587 is commonly used for 10413498266Sopenharmony_ci * secure mail submission (see RFC 4403), but you should use whatever 10513498266Sopenharmony_ci * matches your server configuration. */ 10613498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_URL, "smtp://mainserver.example.net:587"); 10713498266Sopenharmony_ci 10813498266Sopenharmony_ci /* In this example, we will start with a plain text connection, and upgrade 10913498266Sopenharmony_ci * to Transport Layer Security (TLS) using the STARTTLS command. Be careful 11013498266Sopenharmony_ci * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer 11113498266Sopenharmony_ci * will continue anyway - see the security discussion in the libcurl 11213498266Sopenharmony_ci * tutorial for more details. */ 11313498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); 11413498266Sopenharmony_ci 11513498266Sopenharmony_ci /* If your server does not have a valid certificate, then you can disable 11613498266Sopenharmony_ci * part of the Transport Layer Security protection by setting the 11713498266Sopenharmony_ci * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false). 11813498266Sopenharmony_ci * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); 11913498266Sopenharmony_ci * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); 12013498266Sopenharmony_ci * That is, in general, a bad idea. It is still better than sending your 12113498266Sopenharmony_ci * authentication details in plain text though. Instead, you should get 12213498266Sopenharmony_ci * the issuer certificate (or the host certificate if the certificate is 12313498266Sopenharmony_ci * self-signed) and add it to the set of certificates that are known to 12413498266Sopenharmony_ci * libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See docs/SSLCERTS 12513498266Sopenharmony_ci * for more information. */ 12613498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem"); 12713498266Sopenharmony_ci 12813498266Sopenharmony_ci /* Note that this option is not strictly required, omitting it will result 12913498266Sopenharmony_ci * in libcurl sending the MAIL FROM command with empty sender data. All 13013498266Sopenharmony_ci * autoresponses should have an empty reverse-path, and should be directed 13113498266Sopenharmony_ci * to the address in the reverse-path which triggered them. Otherwise, 13213498266Sopenharmony_ci * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more 13313498266Sopenharmony_ci * details. 13413498266Sopenharmony_ci */ 13513498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_MAIL); 13613498266Sopenharmony_ci 13713498266Sopenharmony_ci /* Add two recipients, in this particular case they correspond to the 13813498266Sopenharmony_ci * To: and Cc: addressees in the header, but they could be any kind of 13913498266Sopenharmony_ci * recipient. */ 14013498266Sopenharmony_ci recipients = curl_slist_append(recipients, TO_MAIL); 14113498266Sopenharmony_ci recipients = curl_slist_append(recipients, CC_MAIL); 14213498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); 14313498266Sopenharmony_ci 14413498266Sopenharmony_ci /* We are using a callback function to specify the payload (the headers and 14513498266Sopenharmony_ci * body of the message). You could just use the CURLOPT_READDATA option to 14613498266Sopenharmony_ci * specify a FILE pointer to read from. */ 14713498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); 14813498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); 14913498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 15013498266Sopenharmony_ci 15113498266Sopenharmony_ci /* Since the traffic will be encrypted, it is useful to turn on debug 15213498266Sopenharmony_ci * information within libcurl to see what is happening during the 15313498266Sopenharmony_ci * transfer. 15413498266Sopenharmony_ci */ 15513498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 15613498266Sopenharmony_ci 15713498266Sopenharmony_ci /* Send the message */ 15813498266Sopenharmony_ci res = curl_easy_perform(curl); 15913498266Sopenharmony_ci 16013498266Sopenharmony_ci /* Check for errors */ 16113498266Sopenharmony_ci if(res != CURLE_OK) 16213498266Sopenharmony_ci fprintf(stderr, "curl_easy_perform() failed: %s\n", 16313498266Sopenharmony_ci curl_easy_strerror(res)); 16413498266Sopenharmony_ci 16513498266Sopenharmony_ci /* Free the list of recipients */ 16613498266Sopenharmony_ci curl_slist_free_all(recipients); 16713498266Sopenharmony_ci 16813498266Sopenharmony_ci /* Always cleanup */ 16913498266Sopenharmony_ci curl_easy_cleanup(curl); 17013498266Sopenharmony_ci } 17113498266Sopenharmony_ci 17213498266Sopenharmony_ci return (int)res; 17313498266Sopenharmony_ci} 174