1/*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 9 * 10 * This software is licensed as described in the file COPYING, which 11 * you should have received as part of this distribution. The terms 12 * are also available at https://curl.se/docs/copyright.html. 13 * 14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 15 * copies of the Software, and permit persons to whom the Software is 16 * furnished to do so, under the terms of the COPYING file. 17 * 18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 * KIND, either express or implied. 20 * 21 * SPDX-License-Identifier: curl 22 * 23 ***************************************************************************/ 24 25/* <DESC> 26 * Send SMTP mime emails 27 * </DESC> 28 */ 29 30#include <stdio.h> 31#include <string.h> 32#include <curl/curl.h> 33 34/* This is a simple example showing how to send mime mail using libcurl's SMTP 35 * capabilities. For an example of using the multi interface please see 36 * smtp-multi.c. 37 * 38 * Note that this example requires libcurl 7.56.0 or above. 39 */ 40 41#define FROM "<sender@example.org>" 42#define TO "<addressee@example.net>" 43#define CC "<info@example.org>" 44 45static const char *headers_text[] = { 46 "Date: Tue, 22 Aug 2017 14:08:43 +0100", 47 "To: " TO, 48 "From: " FROM " (Example User)", 49 "Cc: " CC " (Another example User)", 50 "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@" 51 "rfcpedant.example.org>", 52 "Subject: example sending a MIME-formatted message", 53 NULL 54}; 55 56static const char inline_text[] = 57 "This is the inline text message of the email.\r\n" 58 "\r\n" 59 " It could be a lot of lines that would be displayed in an email\r\n" 60 "viewer that is not able to handle HTML.\r\n"; 61 62static const char inline_html[] = 63 "<html><body>\r\n" 64 "<p>This is the inline <b>HTML</b> message of the email.</p>" 65 "<br />\r\n" 66 "<p>It could be a lot of HTML data that would be displayed by " 67 "email viewers able to handle HTML.</p>" 68 "</body></html>\r\n"; 69 70 71int main(void) 72{ 73 CURL *curl; 74 CURLcode res = CURLE_OK; 75 76 curl = curl_easy_init(); 77 if(curl) { 78 struct curl_slist *headers = NULL; 79 struct curl_slist *recipients = NULL; 80 struct curl_slist *slist = NULL; 81 curl_mime *mime; 82 curl_mime *alt; 83 curl_mimepart *part; 84 const char **cpp; 85 86 /* This is the URL for your mailserver */ 87 curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com"); 88 89 /* Note that this option is not strictly required, omitting it will result 90 * in libcurl sending the MAIL FROM command with empty sender data. All 91 * autoresponses should have an empty reverse-path, and should be directed 92 * to the address in the reverse-path which triggered them. Otherwise, 93 * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more 94 * details. 95 */ 96 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM); 97 98 /* Add two recipients, in this particular case they correspond to the 99 * To: and Cc: addressees in the header, but they could be any kind of 100 * recipient. */ 101 recipients = curl_slist_append(recipients, TO); 102 recipients = curl_slist_append(recipients, CC); 103 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); 104 105 /* allow one of the recipients to fail and still consider it okay */ 106 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT_ALLOWFAILS, 1L); 107 108 /* Build and set the message header list. */ 109 for(cpp = headers_text; *cpp; cpp++) 110 headers = curl_slist_append(headers, *cpp); 111 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); 112 113 /* Build the mime message. */ 114 mime = curl_mime_init(curl); 115 116 /* The inline part is an alternative proposing the html and the text 117 versions of the email. */ 118 alt = curl_mime_init(curl); 119 120 /* HTML message. */ 121 part = curl_mime_addpart(alt); 122 curl_mime_data(part, inline_html, CURL_ZERO_TERMINATED); 123 curl_mime_type(part, "text/html"); 124 125 /* Text message. */ 126 part = curl_mime_addpart(alt); 127 curl_mime_data(part, inline_text, CURL_ZERO_TERMINATED); 128 129 /* Create the inline part. */ 130 part = curl_mime_addpart(mime); 131 curl_mime_subparts(part, alt); 132 curl_mime_type(part, "multipart/alternative"); 133 slist = curl_slist_append(NULL, "Content-Disposition: inline"); 134 curl_mime_headers(part, slist, 1); 135 136 /* Add the current source program as an attachment. */ 137 part = curl_mime_addpart(mime); 138 curl_mime_filedata(part, "smtp-mime.c"); 139 curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); 140 141 /* Send the message */ 142 res = curl_easy_perform(curl); 143 144 /* Check for errors */ 145 if(res != CURLE_OK) 146 fprintf(stderr, "curl_easy_perform() failed: %s\n", 147 curl_easy_strerror(res)); 148 149 /* Free lists. */ 150 curl_slist_free_all(recipients); 151 curl_slist_free_all(headers); 152 153 /* curl will not send the QUIT command until you call cleanup, so you 154 * should be able to reuse this connection for additional messages 155 * (setting CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and 156 * calling curl_easy_perform() again. It may not be a good idea to keep 157 * the connection open for a long time though (more than a few minutes may 158 * result in the server timing out the connection), and you do want to 159 * clean up in the end. 160 */ 161 curl_easy_cleanup(curl); 162 163 /* Free multipart message. */ 164 curl_mime_free(mime); 165 } 166 167 return (int)res; 168} 169