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 mime emails 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 mime mail using libcurl's SMTP 3513498266Sopenharmony_ci * capabilities. For an example of using the multi interface please see 3613498266Sopenharmony_ci * smtp-multi.c. 3713498266Sopenharmony_ci * 3813498266Sopenharmony_ci * Note that this example requires libcurl 7.56.0 or above. 3913498266Sopenharmony_ci */ 4013498266Sopenharmony_ci 4113498266Sopenharmony_ci#define FROM "<sender@example.org>" 4213498266Sopenharmony_ci#define TO "<addressee@example.net>" 4313498266Sopenharmony_ci#define CC "<info@example.org>" 4413498266Sopenharmony_ci 4513498266Sopenharmony_cistatic const char *headers_text[] = { 4613498266Sopenharmony_ci "Date: Tue, 22 Aug 2017 14:08:43 +0100", 4713498266Sopenharmony_ci "To: " TO, 4813498266Sopenharmony_ci "From: " FROM " (Example User)", 4913498266Sopenharmony_ci "Cc: " CC " (Another example User)", 5013498266Sopenharmony_ci "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@" 5113498266Sopenharmony_ci "rfcpedant.example.org>", 5213498266Sopenharmony_ci "Subject: example sending a MIME-formatted message", 5313498266Sopenharmony_ci NULL 5413498266Sopenharmony_ci}; 5513498266Sopenharmony_ci 5613498266Sopenharmony_cistatic const char inline_text[] = 5713498266Sopenharmony_ci "This is the inline text message of the email.\r\n" 5813498266Sopenharmony_ci "\r\n" 5913498266Sopenharmony_ci " It could be a lot of lines that would be displayed in an email\r\n" 6013498266Sopenharmony_ci "viewer that is not able to handle HTML.\r\n"; 6113498266Sopenharmony_ci 6213498266Sopenharmony_cistatic const char inline_html[] = 6313498266Sopenharmony_ci "<html><body>\r\n" 6413498266Sopenharmony_ci "<p>This is the inline <b>HTML</b> message of the email.</p>" 6513498266Sopenharmony_ci "<br />\r\n" 6613498266Sopenharmony_ci "<p>It could be a lot of HTML data that would be displayed by " 6713498266Sopenharmony_ci "email viewers able to handle HTML.</p>" 6813498266Sopenharmony_ci "</body></html>\r\n"; 6913498266Sopenharmony_ci 7013498266Sopenharmony_ci 7113498266Sopenharmony_ciint main(void) 7213498266Sopenharmony_ci{ 7313498266Sopenharmony_ci CURL *curl; 7413498266Sopenharmony_ci CURLcode res = CURLE_OK; 7513498266Sopenharmony_ci 7613498266Sopenharmony_ci curl = curl_easy_init(); 7713498266Sopenharmony_ci if(curl) { 7813498266Sopenharmony_ci struct curl_slist *headers = NULL; 7913498266Sopenharmony_ci struct curl_slist *recipients = NULL; 8013498266Sopenharmony_ci struct curl_slist *slist = NULL; 8113498266Sopenharmony_ci curl_mime *mime; 8213498266Sopenharmony_ci curl_mime *alt; 8313498266Sopenharmony_ci curl_mimepart *part; 8413498266Sopenharmony_ci const char **cpp; 8513498266Sopenharmony_ci 8613498266Sopenharmony_ci /* This is the URL for your mailserver */ 8713498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com"); 8813498266Sopenharmony_ci 8913498266Sopenharmony_ci /* Note that this option is not strictly required, omitting it will result 9013498266Sopenharmony_ci * in libcurl sending the MAIL FROM command with empty sender data. All 9113498266Sopenharmony_ci * autoresponses should have an empty reverse-path, and should be directed 9213498266Sopenharmony_ci * to the address in the reverse-path which triggered them. Otherwise, 9313498266Sopenharmony_ci * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more 9413498266Sopenharmony_ci * details. 9513498266Sopenharmony_ci */ 9613498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM); 9713498266Sopenharmony_ci 9813498266Sopenharmony_ci /* Add two recipients, in this particular case they correspond to the 9913498266Sopenharmony_ci * To: and Cc: addressees in the header, but they could be any kind of 10013498266Sopenharmony_ci * recipient. */ 10113498266Sopenharmony_ci recipients = curl_slist_append(recipients, TO); 10213498266Sopenharmony_ci recipients = curl_slist_append(recipients, CC); 10313498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); 10413498266Sopenharmony_ci 10513498266Sopenharmony_ci /* allow one of the recipients to fail and still consider it okay */ 10613498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_MAIL_RCPT_ALLOWFAILS, 1L); 10713498266Sopenharmony_ci 10813498266Sopenharmony_ci /* Build and set the message header list. */ 10913498266Sopenharmony_ci for(cpp = headers_text; *cpp; cpp++) 11013498266Sopenharmony_ci headers = curl_slist_append(headers, *cpp); 11113498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); 11213498266Sopenharmony_ci 11313498266Sopenharmony_ci /* Build the mime message. */ 11413498266Sopenharmony_ci mime = curl_mime_init(curl); 11513498266Sopenharmony_ci 11613498266Sopenharmony_ci /* The inline part is an alternative proposing the html and the text 11713498266Sopenharmony_ci versions of the email. */ 11813498266Sopenharmony_ci alt = curl_mime_init(curl); 11913498266Sopenharmony_ci 12013498266Sopenharmony_ci /* HTML message. */ 12113498266Sopenharmony_ci part = curl_mime_addpart(alt); 12213498266Sopenharmony_ci curl_mime_data(part, inline_html, CURL_ZERO_TERMINATED); 12313498266Sopenharmony_ci curl_mime_type(part, "text/html"); 12413498266Sopenharmony_ci 12513498266Sopenharmony_ci /* Text message. */ 12613498266Sopenharmony_ci part = curl_mime_addpart(alt); 12713498266Sopenharmony_ci curl_mime_data(part, inline_text, CURL_ZERO_TERMINATED); 12813498266Sopenharmony_ci 12913498266Sopenharmony_ci /* Create the inline part. */ 13013498266Sopenharmony_ci part = curl_mime_addpart(mime); 13113498266Sopenharmony_ci curl_mime_subparts(part, alt); 13213498266Sopenharmony_ci curl_mime_type(part, "multipart/alternative"); 13313498266Sopenharmony_ci slist = curl_slist_append(NULL, "Content-Disposition: inline"); 13413498266Sopenharmony_ci curl_mime_headers(part, slist, 1); 13513498266Sopenharmony_ci 13613498266Sopenharmony_ci /* Add the current source program as an attachment. */ 13713498266Sopenharmony_ci part = curl_mime_addpart(mime); 13813498266Sopenharmony_ci curl_mime_filedata(part, "smtp-mime.c"); 13913498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); 14013498266Sopenharmony_ci 14113498266Sopenharmony_ci /* Send the message */ 14213498266Sopenharmony_ci res = curl_easy_perform(curl); 14313498266Sopenharmony_ci 14413498266Sopenharmony_ci /* Check for errors */ 14513498266Sopenharmony_ci if(res != CURLE_OK) 14613498266Sopenharmony_ci fprintf(stderr, "curl_easy_perform() failed: %s\n", 14713498266Sopenharmony_ci curl_easy_strerror(res)); 14813498266Sopenharmony_ci 14913498266Sopenharmony_ci /* Free lists. */ 15013498266Sopenharmony_ci curl_slist_free_all(recipients); 15113498266Sopenharmony_ci curl_slist_free_all(headers); 15213498266Sopenharmony_ci 15313498266Sopenharmony_ci /* curl will not send the QUIT command until you call cleanup, so you 15413498266Sopenharmony_ci * should be able to reuse this connection for additional messages 15513498266Sopenharmony_ci * (setting CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and 15613498266Sopenharmony_ci * calling curl_easy_perform() again. It may not be a good idea to keep 15713498266Sopenharmony_ci * the connection open for a long time though (more than a few minutes may 15813498266Sopenharmony_ci * result in the server timing out the connection), and you do want to 15913498266Sopenharmony_ci * clean up in the end. 16013498266Sopenharmony_ci */ 16113498266Sopenharmony_ci curl_easy_cleanup(curl); 16213498266Sopenharmony_ci 16313498266Sopenharmony_ci /* Free multipart message. */ 16413498266Sopenharmony_ci curl_mime_free(mime); 16513498266Sopenharmony_ci } 16613498266Sopenharmony_ci 16713498266Sopenharmony_ci return (int)res; 16813498266Sopenharmony_ci} 169