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