diff options
Diffstat (limited to 'docs')
-rw-r--r-- | docs/examples/Makefile.inc | 4 | ||||
-rw-r--r-- | docs/examples/multi-post.c | 60 | ||||
-rw-r--r-- | docs/examples/postit2.c | 56 | ||||
-rw-r--r-- | docs/examples/smtp-mime.c | 159 |
4 files changed, 215 insertions, 64 deletions
diff --git a/docs/examples/Makefile.inc b/docs/examples/Makefile.inc index 31c87a794..f21633d6d 100644 --- a/docs/examples/Makefile.inc +++ b/docs/examples/Makefile.inc @@ -26,8 +26,8 @@ check_PROGRAMS = 10-at-a-time anyauthput cookie_interface debug fileupload \ https multi-app multi-debugcallback multi-double multi-post multi-single \ persistant post-callback postit2 sepheaders simple simplepost simplessl \ sendrecv httpcustomheader certinfo chkspeed ftpgetinfo ftp-wildcard \ - smtp-mail smtp-multi smtp-ssl smtp-tls smtp-vrfy smtp-expn rtsp \ - externalsocket resolve progressfunc pop3-retr pop3-list pop3-uidl \ + smtp-mail smtp-mime smtp-multi smtp-ssl smtp-tls smtp-vrfy smtp-expn \ + rtsp externalsocket resolve progressfunc pop3-retr pop3-list pop3-uidl \ pop3-dele pop3-top pop3-stat pop3-noop pop3-ssl pop3-tls pop3-multi \ imap-list imap-lsub imap-fetch imap-store imap-append imap-examine \ imap-search imap-create imap-delete imap-copy imap-noop imap-ssl \ diff --git a/docs/examples/multi-post.c b/docs/examples/multi-post.c index cc2ca18e9..3777f521a 100644 --- a/docs/examples/multi-post.c +++ b/docs/examples/multi-post.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -37,47 +37,43 @@ int main(void) CURLM *multi_handle; int still_running; - struct curl_httppost *formpost=NULL; - struct curl_httppost *lastptr=NULL; - struct curl_slist *headerlist=NULL; + curl_mime *form = NULL; + curl_mimepart *field = NULL; + struct curl_slist *headerlist = NULL; static const char buf[] = "Expect:"; - /* Fill in the file upload field. This makes libcurl load data from - the given file name when curl_easy_perform() is called. */ - curl_formadd(&formpost, - &lastptr, - CURLFORM_COPYNAME, "sendfile", - CURLFORM_FILE, "postit2.c", - CURLFORM_END); - - /* Fill in the filename field */ - curl_formadd(&formpost, - &lastptr, - CURLFORM_COPYNAME, "filename", - CURLFORM_COPYCONTENTS, "postit2.c", - CURLFORM_END); - - /* Fill in the submit field too, even if this is rarely needed */ - curl_formadd(&formpost, - &lastptr, - CURLFORM_COPYNAME, "submit", - CURLFORM_COPYCONTENTS, "send", - CURLFORM_END); - curl = curl_easy_init(); multi_handle = curl_multi_init(); - /* initialize custom header list (stating that Expect: 100-continue is not - wanted */ - headerlist = curl_slist_append(headerlist, buf); if(curl && multi_handle) { + /* Create the form */ + form = curl_mime_init(curl); + + /* Fill in the file upload field */ + field = curl_mime_addpart(form); + curl_mime_name(field, "sendfile", -1); + curl_mime_filedata(field, "multi-post.c"); + + /* Fill in the filename field */ + field = curl_mime_addpart(form); + curl_mime_name(field, "filename", -1); + curl_mime_data(field, "multi-post.c", -1); + + /* Fill in the submit field too, even if this is rarely needed */ + field = curl_mime_addpart(form); + curl_mime_name(field, "submit", -1); + curl_mime_data(field, "send", -1); + + /* initialize custom header list (stating that Expect: 100-continue is not + wanted */ + headerlist = curl_slist_append(headerlist, buf); /* what URL that receives this POST */ curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi"); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); - curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); + curl_easy_setopt(curl, CURLOPT_MIMEPOST, form); curl_multi_add_handle(multi_handle, curl); @@ -161,8 +157,8 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); - /* then cleanup the formpost chain */ - curl_formfree(formpost); + /* then cleanup the form */ + curl_mime_free(form); /* free slist */ curl_slist_free_all(headerlist); diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c index ef50a66d9..f4c0a8f32 100644 --- a/docs/examples/postit2.c +++ b/docs/examples/postit2.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -47,46 +47,42 @@ int main(int argc, char *argv[]) CURL *curl; CURLcode res; - struct curl_httppost *formpost=NULL; - struct curl_httppost *lastptr=NULL; - struct curl_slist *headerlist=NULL; + curl_mime *form = NULL; + curl_mimepart *field = NULL; + struct curl_slist *headerlist = NULL; static const char buf[] = "Expect:"; curl_global_init(CURL_GLOBAL_ALL); - /* Fill in the file upload field */ - curl_formadd(&formpost, - &lastptr, - CURLFORM_COPYNAME, "sendfile", - CURLFORM_FILE, "postit2.c", - CURLFORM_END); + curl = curl_easy_init(); + if(curl) { + /* Create the form */ + form = curl_mime_init(curl); - /* Fill in the filename field */ - curl_formadd(&formpost, - &lastptr, - CURLFORM_COPYNAME, "filename", - CURLFORM_COPYCONTENTS, "postit2.c", - CURLFORM_END); + /* Fill in the file upload field */ + field = curl_mime_addpart(form); + curl_mime_name(field, "sendfile", -1); + curl_mime_filedata(field, "postit2.c"); + /* Fill in the filename field */ + field = curl_mime_addpart(form); + curl_mime_name(field, "filename", -1); + curl_mime_data(field, "postit2.c", -1); - /* Fill in the submit field too, even if this is rarely needed */ - curl_formadd(&formpost, - &lastptr, - CURLFORM_COPYNAME, "submit", - CURLFORM_COPYCONTENTS, "send", - CURLFORM_END); + /* Fill in the submit field too, even if this is rarely needed */ + field = curl_mime_addpart(form); + curl_mime_name(field, "submit", -1); + curl_mime_data(field, "send", -1); - curl = curl_easy_init(); - /* initialize custom header list (stating that Expect: 100-continue is not - wanted */ - headerlist = curl_slist_append(headerlist, buf); - if(curl) { + /* initialize custom header list (stating that Expect: 100-continue is not + wanted */ + headerlist = curl_slist_append(headerlist, buf); /* what URL that receives this POST */ curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/examplepost.cgi"); if((argc == 2) && (!strcmp(argv[1], "noexpectheader"))) /* only disable 100-continue header if explicitly requested */ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); - curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); + curl_easy_setopt(curl, CURLOPT_MIMEPOST, form); /* Perform the request, res will get the return code */ res = curl_easy_perform(curl); @@ -98,8 +94,8 @@ int main(int argc, char *argv[]) /* always cleanup */ curl_easy_cleanup(curl); - /* then cleanup the formpost chain */ - curl_formfree(formpost); + /* then cleanup the form */ + curl_mime_free(form); /* free slist */ curl_slist_free_all(headerlist); } diff --git a/docs/examples/smtp-mime.c b/docs/examples/smtp-mime.c new file mode 100644 index 000000000..a467535a9 --- /dev/null +++ b/docs/examples/smtp-mime.c @@ -0,0 +1,159 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at https://curl.haxx.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ***************************************************************************/ + +/* <DESC> + * SMTP example showing how to send mime e-mails + * </DESC> + */ + +#include <stdio.h> +#include <string.h> +#include <curl/curl.h> + +/* This is a simple example showing how to send mime mail using libcurl's SMTP + * capabilities. For an example of using the multi interface please see + * smtp-multi.c. + * + * Note that this example requires libcurl 7.56.0 or above. + */ + +#define FROM "<sender@example.org>" +#define TO "<addressee@example.net>" +#define CC "<info@example.org>" + +static const char *headers_text[] = { + "Date: Tue, 22 Aug 2017 14:08:43 +0100", + "To: " TO, + "From: " FROM "(Example User)", + "Cc: " CC "(Another example User)", + "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@" + "rfcpedant.example.org>", + "Subject: example sending a MIME-formatted message", + NULL +}; + +static const char inline_text[] = + "This is the inline text message of the e-mail.\r\n" + "\r\n" + " It could be a lot of lines that would be displayed in an e-mail\r\n" + "viewer that is not able to handle HTML.\r\n"; + +static const char inline_html[] = + "<html><body>\r\n" + "<p>This is the inline <b>HTML</b> message of the e-mail.</p>" + "<br />\r\n" + "<p>It could be a lot of HTML data that would be displayed by " + "e-mail viewers able to handle HTML.</p>" + "</body></html>\r\n"; + + +int main(void) +{ + CURL *curl; + CURLcode res = CURLE_OK; + struct curl_slist *headers = NULL; + struct curl_slist *recipients = NULL; + struct curl_slist *slist = NULL; + curl_mime *mime; + curl_mime *alt; + curl_mimepart *part; + const char **cpp; + + curl = curl_easy_init(); + if(curl) { + /* This is the URL for your mailserver */ + curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com"); + + /* Note that this option isn't strictly required, omitting it will result + * in libcurl sending the MAIL FROM command with empty sender data. All + * autoresponses should have an empty reverse-path, and should be directed + * to the address in the reverse-path which triggered them. Otherwise, + * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more + * details. + */ + curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM); + + /* Add two recipients, in this particular case they correspond to the + * To: and Cc: addressees in the header, but they could be any kind of + * recipient. */ + recipients = curl_slist_append(recipients, TO); + recipients = curl_slist_append(recipients, CC); + curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); + + /* Build and set the message header list. */ + for(cpp = headers_text; *cpp; cpp++) + headers = curl_slist_append(headers, *cpp); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + /* Build the mime message. */ + mime = curl_mime_init(curl); + + /* The inline part is an alterative proposing the html and the text + versions of the e-mail. */ + alt = curl_mime_init(curl); + + /* HTML message. */ + part = curl_mime_addpart(alt); + curl_mime_data(part, inline_html, -1); + curl_mime_type(part, "text/html"); + + /* Text message. */ + part = curl_mime_addpart(alt); + curl_mime_data(part, inline_text, -1); + + /* Create the inline part. */ + part = curl_mime_addpart(mime); + curl_mime_subparts(part, alt); + curl_mime_type(part, "multipart/alternative"); + slist = curl_slist_append(NULL, "Content-Disposition: inline"); + curl_mime_headers(part, slist, 1); + + /* Add the current source program as an attachment. */ + part = curl_mime_addpart(mime); + curl_mime_filedata(part, "smtp-mime.c"); + curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime); + + /* Send the message */ + res = curl_easy_perform(curl); + + /* Check for errors */ + if(res != CURLE_OK) + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); + + /* Free lists. */ + curl_slist_free_all(recipients); + curl_slist_free_all(headers); + + /* curl won't send the QUIT command until you call cleanup, so you should + * be able to re-use this connection for additional messages (setting + * CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and calling + * curl_easy_perform() again. It may not be a good idea to keep the + * connection open for a very long time though (more than a few minutes + * may result in the server timing out the connection), and you do want to + * clean up in the end. + */ + curl_easy_cleanup(curl); + } + + return (int)res; +} |