xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2022, 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 | * SMTP example showing how to send 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 | |
| 45 | static 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 | |
| 56 | static 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 | |
| 62 | static 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 | |
| 71 | int 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 | /* Build and set the message header list. */ |
| 106 | for(cpp = headers_text; *cpp; cpp++) |
| 107 | headers = curl_slist_append(headers, *cpp); |
| 108 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); |
| 109 | |
| 110 | /* Build the mime message. */ |
| 111 | mime = curl_mime_init(curl); |
| 112 | |
| 113 | /* The inline part is an alternative proposing the html and the text |
| 114 | versions of the email. */ |
| 115 | alt = curl_mime_init(curl); |
| 116 | |
| 117 | /* HTML message. */ |
| 118 | part = curl_mime_addpart(alt); |
| 119 | curl_mime_data(part, inline_html, CURL_ZERO_TERMINATED); |
| 120 | curl_mime_type(part, "text/html"); |
| 121 | |
| 122 | /* Text message. */ |
| 123 | part = curl_mime_addpart(alt); |
| 124 | curl_mime_data(part, inline_text, CURL_ZERO_TERMINATED); |
| 125 | |
| 126 | /* Create the inline part. */ |
| 127 | part = curl_mime_addpart(mime); |
| 128 | curl_mime_subparts(part, alt); |
| 129 | curl_mime_type(part, "multipart/alternative"); |
| 130 | slist = curl_slist_append(NULL, "Content-Disposition: inline"); |
| 131 | curl_mime_headers(part, slist, 1); |
| 132 | |
| 133 | /* Add the current source program as an attachment. */ |
| 134 | part = curl_mime_addpart(mime); |
| 135 | curl_mime_filedata(part, "smtp-mime.c"); |
| 136 | curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); |
| 137 | |
| 138 | /* Send the message */ |
| 139 | res = curl_easy_perform(curl); |
| 140 | |
| 141 | /* Check for errors */ |
| 142 | if(res != CURLE_OK) |
| 143 | fprintf(stderr, "curl_easy_perform() failed: %s\n", |
| 144 | curl_easy_strerror(res)); |
| 145 | |
| 146 | /* Free lists. */ |
| 147 | curl_slist_free_all(recipients); |
| 148 | curl_slist_free_all(headers); |
| 149 | |
| 150 | /* curl will not send the QUIT command until you call cleanup, so you |
| 151 | * should be able to re-use this connection for additional messages |
| 152 | * (setting CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and |
| 153 | * calling curl_easy_perform() again. It may not be a good idea to keep |
| 154 | * the connection open for a very long time though (more than a few |
| 155 | * minutes may result in the server timing out the connection), and you do |
| 156 | * want to clean up in the end. |
| 157 | */ |
| 158 | curl_easy_cleanup(curl); |
| 159 | |
| 160 | /* Free multipart message. */ |
| 161 | curl_mime_free(mime); |
| 162 | } |
| 163 | |
| 164 | return (int)res; |
| 165 | } |