Annotation of embedaddon/curl/docs/examples/smtp-mime.c, revision 1.1.1.1

1.1       misho       1: /***************************************************************************
                      2:  *                                  _   _ ____  _
                      3:  *  Project                     ___| | | |  _ \| |
                      4:  *                             / __| | | | |_) | |
                      5:  *                            | (__| |_| |  _ <| |___
                      6:  *                             \___|\___/|_| \_\_____|
                      7:  *
                      8:  * Copyright (C) 1998 - 2019, 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: 
                     74:   curl = curl_easy_init();
                     75:   if(curl) {
                     76:     struct curl_slist *headers = NULL;
                     77:     struct curl_slist *recipients = NULL;
                     78:     struct curl_slist *slist = NULL;
                     79:     curl_mime *mime;
                     80:     curl_mime *alt;
                     81:     curl_mimepart *part;
                     82:     const char **cpp;
                     83: 
                     84:     /* This is the URL for your mailserver */
                     85:     curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
                     86: 
                     87:     /* Note that this option isn't strictly required, omitting it will result
                     88:      * in libcurl sending the MAIL FROM command with empty sender data. All
                     89:      * autoresponses should have an empty reverse-path, and should be directed
                     90:      * to the address in the reverse-path which triggered them. Otherwise,
                     91:      * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more
                     92:      * details.
                     93:      */
                     94:     curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
                     95: 
                     96:     /* Add two recipients, in this particular case they correspond to the
                     97:      * To: and Cc: addressees in the header, but they could be any kind of
                     98:      * recipient. */
                     99:     recipients = curl_slist_append(recipients, TO);
                    100:     recipients = curl_slist_append(recipients, CC);
                    101:     curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
                    102: 
                    103:     /* Build and set the message header list. */
                    104:     for(cpp = headers_text; *cpp; cpp++)
                    105:       headers = curl_slist_append(headers, *cpp);
                    106:     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
                    107: 
                    108:     /* Build the mime message. */
                    109:     mime = curl_mime_init(curl);
                    110: 
                    111:     /* The inline part is an alternative proposing the html and the text
                    112:        versions of the e-mail. */
                    113:     alt = curl_mime_init(curl);
                    114: 
                    115:     /* HTML message. */
                    116:     part = curl_mime_addpart(alt);
                    117:     curl_mime_data(part, inline_html, CURL_ZERO_TERMINATED);
                    118:     curl_mime_type(part, "text/html");
                    119: 
                    120:     /* Text message. */
                    121:     part = curl_mime_addpart(alt);
                    122:     curl_mime_data(part, inline_text, CURL_ZERO_TERMINATED);
                    123: 
                    124:     /* Create the inline part. */
                    125:     part = curl_mime_addpart(mime);
                    126:     curl_mime_subparts(part, alt);
                    127:     curl_mime_type(part, "multipart/alternative");
                    128:     slist = curl_slist_append(NULL, "Content-Disposition: inline");
                    129:     curl_mime_headers(part, slist, 1);
                    130: 
                    131:     /* Add the current source program as an attachment. */
                    132:     part = curl_mime_addpart(mime);
                    133:     curl_mime_filedata(part, "smtp-mime.c");
                    134:     curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
                    135: 
                    136:     /* Send the message */
                    137:     res = curl_easy_perform(curl);
                    138: 
                    139:     /* Check for errors */
                    140:     if(res != CURLE_OK)
                    141:       fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    142:               curl_easy_strerror(res));
                    143: 
                    144:     /* Free lists. */
                    145:     curl_slist_free_all(recipients);
                    146:     curl_slist_free_all(headers);
                    147: 
                    148:     /* curl won't send the QUIT command until you call cleanup, so you should
                    149:      * be able to re-use this connection for additional messages (setting
                    150:      * CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and calling
                    151:      * curl_easy_perform() again. It may not be a good idea to keep the
                    152:      * connection open for a very long time though (more than a few minutes
                    153:      * may result in the server timing out the connection), and you do want to
                    154:      * clean up in the end.
                    155:      */
                    156:     curl_easy_cleanup(curl);
                    157: 
                    158:     /* Free multipart message. */
                    159:     curl_mime_free(mime);
                    160:   }
                    161: 
                    162:   return (int)res;
                    163: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>