examples: Update SMTP TLS example mail content to be RFC-2821 compliant

...and made some minor coding style changes to better match the curl
coding standards as well as the other email related examples.
This commit is contained in:
Steve Holme
2014-01-01 18:31:42 +00:00
parent 83ae98c6c6
commit 0757a9b941

View File

@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@@ -33,17 +33,17 @@
#define CC "<info@example.org>" #define CC "<info@example.org>"
static const char *payload_text[] = { static const char *payload_text[] = {
"Date: Mon, 29 Nov 2010 21:54:29 +1100\n", "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
"To: " TO "\n", "To: " TO "\r\n",
"From: " FROM "(Example User)\n", "From: " FROM "(Example User)\r\n",
"Cc: " CC "(Another example User)\n", "Cc: " CC "(Another example User)\r\n",
"Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\n", "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n",
"Subject: SMTP TLS example message\n", "Subject: SMTP TLS example message\r\n",
"\n", /* empty line to divide headers from body, see RFC5322 */ "\r\n", /* empty line to divide headers from body, see RFC5322 */
"The body of the message starts here.\n", "The body of the message starts here.\r\n",
"\n", "\r\n",
"It could be a lot of lines, could be MIME encoded, whatever.\n", "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
"Check RFC5322.\n", "Check RFC5322.\r\n",
NULL NULL
}; };
@@ -66,15 +66,17 @@ static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
size_t len = strlen(data); size_t len = strlen(data);
memcpy(ptr, data, len); memcpy(ptr, data, len);
upload_ctx->lines_read++; upload_ctx->lines_read++;
return len; return len;
} }
return 0; return 0;
} }
int main(void) int main(void)
{ {
CURL *curl; CURL *curl;
CURLcode res; CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL; struct curl_slist *recipients = NULL;
struct upload_status upload_ctx; struct upload_status upload_ctx;
@@ -105,8 +107,7 @@ int main(void)
* Instead, you should get the issuer certificate (or the host certificate * 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 * 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 * that are known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See
* docs/SSLCERTS for more information. * docs/SSLCERTS for more information. */
*/
curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem"); curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
/* A common reason for requiring transport security is to protect /* A common reason for requiring transport security is to protect
@@ -115,7 +116,7 @@ int main(void)
curl_easy_setopt(curl, CURLOPT_USERNAME, "user@example.net"); curl_easy_setopt(curl, CURLOPT_USERNAME, "user@example.net");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "P@ssw0rd"); curl_easy_setopt(curl, CURLOPT_PASSWORD, "P@ssw0rd");
/* value for envelope reverse-path */ /* Value for envelope reverse-path */
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM); curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
/* Add two recipients, in this particular case they correspond to the /* Add two recipients, in this particular case they correspond to the
@@ -127,8 +128,7 @@ int main(void)
/* In this case, we're using a callback function to specify the data. You /* 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 * could just use the CURLOPT_READDATA option to specify a FILE pointer to
* read from. * read from. */
*/
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
@@ -138,17 +138,20 @@ int main(void)
*/ */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* send the message (including headers) */ /* Send the message */
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
/* Check for errors */ /* Check for errors */
if(res != CURLE_OK) if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res)); curl_easy_strerror(res));
/* free the list of recipients and clean up */ /* Free the list of recipients */
curl_slist_free_all(recipients); curl_slist_free_all(recipients);
/* Always cleanup */
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }
return 0; return (int)res;
} }