From 57523e3578b17c07b0fbf7b22063544ade3df799 Mon Sep 17 00:00:00 2001 From: Brad Hards Date: Fri, 17 Dec 2010 22:55:58 +0100 Subject: smtp-tls: new example This example shows how to send SMTP with TLS --- docs/examples/smtp-tls.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 docs/examples/smtp-tls.c (limited to 'docs/examples/smtp-tls.c') diff --git a/docs/examples/smtp-tls.c b/docs/examples/smtp-tls.c new file mode 100644 index 000000000..58719ade2 --- /dev/null +++ b/docs/examples/smtp-tls.c @@ -0,0 +1,135 @@ +/***************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + */ + +#include +#include +#include + +/* This is a simple example showing how to send mail using libcurl's SMTP + * capabilities. It builds on the simplesmtp.c example, adding some + * authentication and transport security. + */ + +#define FROM "sender@example.org" +#define TO "addressee@example.net" +#define CC "info@example.org" + +static const char *payload_text[]={ + "Date: Mon, 29 Nov 2010 21:54:29 +1100\n", + "To: " TO "\n", + "From: " FROM "(Example User)\n", + "Cc: " CC "(Another example User)\n" + "Subject: SMTP TLS example message\n", + "\n", /* empty line to divide headers from body, see RFC5322 */ + "The body of the message starts here.\n", + "\n", + "It could be a lot of lines, could be MIME encoded, whatever.\n", + "Check RFC5322.\n", + NULL +}; + +struct upload_status { + int lines_read; +}; + +static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp) +{ + struct upload_status *upload_ctx = (struct upload_status *)userp; + const char *data; + + if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) { + return 0; + } + + data = payload_text[upload_ctx->lines_read]; + + if (data) { + size_t len = strlen(data); + memcpy(ptr, data, len); + upload_ctx->lines_read ++; + return len; + } + return 0; +} + + +int main(void) +{ + CURL *curl; + CURLcode res; + struct curl_slist *recipients = NULL; + struct upload_status upload_ctx; + + upload_ctx.lines_read = 0; + + curl = curl_easy_init(); + if (curl) { + /* This is the URL for your mailserver. Note the use of port 587 here, + * instead of the normal SMTP port (25). Port 587 is commonly used for + * secure mail submission (see RFC4403), but you should use whatever + * matches your server configuration. */ + curl_easy_setopt(curl, CURLOPT_URL, "smtp://mainserver.example.net:587"); + + /* In this example, we'll start with a plain text connection, and upgrade + * to Transport Layer Security (TLS) using the STARTTLS command. Be careful + * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer + * will continue anyway - see the security discussion in the libcurl + * tutorial for more details. */ + curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL); + + /* If your server doesn't have a valid certificate, then you can disable + * part of the Transport Layer Security protection by setting the + * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false). + * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); + * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); + * That is, in general, a bad idea. It is still better than sending your + * authentication details in plain text though. + * Instead, you should get the issuer certificate (or the host certificate + * if the certificate is self-signed) and add it to the set of certificates + * that are known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See + * docs/SSLCERTS for more information. + */ + curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem"); + + /* A common reason for requiring transport security is to protect + * authentication details (user names and passwords) from being "snooped" + * on the network. Here is how the user name and password are provided: */ + curl_easy_setopt(curl, CURLOPT_USERNAME, "user@example.net"); + curl_easy_setopt(curl, CURLOPT_PASSWORD, "P@ssw0rd"); + + /* value for envelope reverse-path */ + 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); + + /* In this case, we're using a callback function to specify the data. You + * could just use the CURLOPT_READDATA option to specify a FILE pointer to + * read from. + */ + curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); + curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); + + /* Since the traffic will be encrypted, it is very useful to turn on debug + * information within libcurl to see what is happening during the transfer. + */ + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); + + /* send the message (including headers) */ + res = curl_easy_perform(curl); + + /* free the list of recipients and clean up */ + curl_slist_free_all(recipients); + curl_easy_cleanup(curl); + } + return 0; +} -- cgit v1.2.1 From d2395f962dfbeb546b8a64eec88271da48fa66de Mon Sep 17 00:00:00 2001 From: Brad Hards Date: Sat, 18 Dec 2010 17:07:57 +0100 Subject: smtp-tls: add Message-ID: header --- docs/examples/smtp-tls.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'docs/examples/smtp-tls.c') diff --git a/docs/examples/smtp-tls.c b/docs/examples/smtp-tls.c index 58719ade2..a8baade58 100644 --- a/docs/examples/smtp-tls.c +++ b/docs/examples/smtp-tls.c @@ -24,7 +24,8 @@ static const char *payload_text[]={ "Date: Mon, 29 Nov 2010 21:54:29 +1100\n", "To: " TO "\n", "From: " FROM "(Example User)\n", - "Cc: " CC "(Another example User)\n" + "Cc: " CC "(Another example User)\n", + "Message-ID: ", "Subject: SMTP TLS example message\n", "\n", /* empty line to divide headers from body, see RFC5322 */ "The body of the message starts here.\n", -- cgit v1.2.1 From 0e944fb24e5ac49e3ff863f89ae83ad10051525b Mon Sep 17 00:00:00 2001 From: Brad Hards Date: Wed, 22 Dec 2010 11:57:48 +1100 Subject: smtp-tls: add a missing newline Without this you won't get the next (Subject) line. --- docs/examples/smtp-tls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/examples/smtp-tls.c') diff --git a/docs/examples/smtp-tls.c b/docs/examples/smtp-tls.c index a8baade58..97119c568 100644 --- a/docs/examples/smtp-tls.c +++ b/docs/examples/smtp-tls.c @@ -25,7 +25,7 @@ static const char *payload_text[]={ "To: " TO "\n", "From: " FROM "(Example User)\n", "Cc: " CC "(Another example User)\n", - "Message-ID: ", + "Message-ID: \n", "Subject: SMTP TLS example message\n", "\n", /* empty line to divide headers from body, see RFC5322 */ "The body of the message starts here.\n", -- cgit v1.2.1 From 60765493047cba21e76888e36efaf0cba43ccf6d Mon Sep 17 00:00:00 2001 From: Brad Hards Date: Sat, 25 Dec 2010 11:54:41 +1100 Subject: Add angle brackets to addresses in easy SMTP examples, as for smtp-multi example. --- docs/examples/smtp-tls.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'docs/examples/smtp-tls.c') diff --git a/docs/examples/smtp-tls.c b/docs/examples/smtp-tls.c index 97119c568..276908861 100644 --- a/docs/examples/smtp-tls.c +++ b/docs/examples/smtp-tls.c @@ -16,9 +16,9 @@ * authentication and transport security. */ -#define FROM "sender@example.org" -#define TO "addressee@example.net" -#define CC "info@example.org" +#define FROM "" +#define TO "" +#define CC "" static const char *payload_text[]={ "Date: Mon, 29 Nov 2010 21:54:29 +1100\n", -- cgit v1.2.1 From 1aeb635cdd296c16acb375a4a83a78f13166ccab Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 10 Mar 2011 11:48:02 +0100 Subject: sources: update source headers All C and H files now (should) feature the proper project curl source code header, which includes basic info, a copyright statement and some basic disclaimers. --- docs/examples/smtp-tls.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'docs/examples/smtp-tls.c') diff --git a/docs/examples/smtp-tls.c b/docs/examples/smtp-tls.c index 276908861..2e71f973e 100644 --- a/docs/examples/smtp-tls.c +++ b/docs/examples/smtp-tls.c @@ -1,12 +1,24 @@ -/***************************************************************************** +/*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - */ - + * Copyright (C) 1998 - 2011, Daniel Stenberg, , 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 http://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. + * + ***************************************************************************/ #include #include #include -- cgit v1.2.1 From 95ddbdb1dbfbb051d67bf0d6643b1a917a4c7d88 Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Mon, 14 Nov 2011 14:03:31 -0800 Subject: curl_easy_setopt arguments should be of type long in the examples --- docs/examples/smtp-tls.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs/examples/smtp-tls.c') diff --git a/docs/examples/smtp-tls.c b/docs/examples/smtp-tls.c index 2e71f973e..8e2603fa4 100644 --- a/docs/examples/smtp-tls.c +++ b/docs/examples/smtp-tls.c @@ -94,13 +94,13 @@ int main(void) * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer * will continue anyway - see the security discussion in the libcurl * tutorial for more details. */ - curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL); + curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); /* If your server doesn't have a valid certificate, then you can disable * part of the Transport Layer Security protection by setting the * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false). - * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); - * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); + * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); * That is, in general, a bad idea. It is still better than sending your * authentication details in plain text though. * Instead, you should get the issuer certificate (or the host certificate @@ -135,7 +135,7 @@ int main(void) /* Since the traffic will be encrypted, it is very useful to turn on debug * information within libcurl to see what is happening during the transfer. */ - curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); /* send the message (including headers) */ res = curl_easy_perform(curl); -- cgit v1.2.1 From a3dbbcfd2ac7ab8f597e26e76935726279d3fa0c Mon Sep 17 00:00:00 2001 From: Guenter Knauf Date: Wed, 4 Jul 2012 17:03:52 +0200 Subject: Added error checking for samples. --- docs/examples/smtp-tls.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'docs/examples/smtp-tls.c') diff --git a/docs/examples/smtp-tls.c b/docs/examples/smtp-tls.c index 8e2603fa4..3635c103f 100644 --- a/docs/examples/smtp-tls.c +++ b/docs/examples/smtp-tls.c @@ -139,6 +139,10 @@ int main(void) /* send the message (including headers) */ 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 the list of recipients and clean up */ curl_slist_free_all(recipients); -- cgit v1.2.1