smtp-tls.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 http://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. #include <stdio.h>
  23. #include <string.h>
  24. #include <curl/curl.h>
  25. /* This is a simple example showing how to send mail using libcurl's SMTP
  26. * capabilities. It builds on the simplesmtp.c example, adding some
  27. * authentication and transport security.
  28. */
  29. #define FROM "<sender@example.org>"
  30. #define TO "<addressee@example.net>"
  31. #define CC "<info@example.org>"
  32. static const char *payload_text[]={
  33. "Date: Mon, 29 Nov 2010 21:54:29 +1100\n",
  34. "To: " TO "\n",
  35. "From: " FROM "(Example User)\n",
  36. "Cc: " CC "(Another example User)\n",
  37. "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\n",
  38. "Subject: SMTP TLS example message\n",
  39. "\n", /* empty line to divide headers from body, see RFC5322 */
  40. "The body of the message starts here.\n",
  41. "\n",
  42. "It could be a lot of lines, could be MIME encoded, whatever.\n",
  43. "Check RFC5322.\n",
  44. NULL
  45. };
  46. struct upload_status {
  47. int lines_read;
  48. };
  49. static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
  50. {
  51. struct upload_status *upload_ctx = (struct upload_status *)userp;
  52. const char *data;
  53. if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
  54. return 0;
  55. }
  56. data = payload_text[upload_ctx->lines_read];
  57. if (data) {
  58. size_t len = strlen(data);
  59. memcpy(ptr, data, len);
  60. upload_ctx->lines_read ++;
  61. return len;
  62. }
  63. return 0;
  64. }
  65. int main(void)
  66. {
  67. CURL *curl;
  68. CURLcode res;
  69. struct curl_slist *recipients = NULL;
  70. struct upload_status upload_ctx;
  71. upload_ctx.lines_read = 0;
  72. curl = curl_easy_init();
  73. if (curl) {
  74. /* This is the URL for your mailserver. Note the use of port 587 here,
  75. * instead of the normal SMTP port (25). Port 587 is commonly used for
  76. * secure mail submission (see RFC4403), but you should use whatever
  77. * matches your server configuration. */
  78. curl_easy_setopt(curl, CURLOPT_URL, "smtp://mainserver.example.net:587");
  79. /* In this example, we'll start with a plain text connection, and upgrade
  80. * to Transport Layer Security (TLS) using the STARTTLS command. Be careful
  81. * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer
  82. * will continue anyway - see the security discussion in the libcurl
  83. * tutorial for more details. */
  84. curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
  85. /* If your server doesn't have a valid certificate, then you can disable
  86. * part of the Transport Layer Security protection by setting the
  87. * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
  88. * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  89. * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  90. * That is, in general, a bad idea. It is still better than sending your
  91. * authentication details in plain text though.
  92. * Instead, you should get the issuer certificate (or the host certificate
  93. * if the certificate is self-signed) and add it to the set of certificates
  94. * that are known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See
  95. * docs/SSLCERTS for more information.
  96. */
  97. curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
  98. /* A common reason for requiring transport security is to protect
  99. * authentication details (user names and passwords) from being "snooped"
  100. * on the network. Here is how the user name and password are provided: */
  101. curl_easy_setopt(curl, CURLOPT_USERNAME, "user@example.net");
  102. curl_easy_setopt(curl, CURLOPT_PASSWORD, "P@ssw0rd");
  103. /* value for envelope reverse-path */
  104. curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
  105. /* Add two recipients, in this particular case they correspond to the
  106. * To: and Cc: addressees in the header, but they could be any kind of
  107. * recipient. */
  108. recipients = curl_slist_append(recipients, TO);
  109. recipients = curl_slist_append(recipients, CC);
  110. curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
  111. /* In this case, we're using a callback function to specify the data. You
  112. * could just use the CURLOPT_READDATA option to specify a FILE pointer to
  113. * read from.
  114. */
  115. curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
  116. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
  117. /* Since the traffic will be encrypted, it is very useful to turn on debug
  118. * information within libcurl to see what is happening during the transfer.
  119. */
  120. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  121. /* send the message (including headers) */
  122. res = curl_easy_perform(curl);
  123. /* free the list of recipients and clean up */
  124. curl_slist_free_all(recipients);
  125. curl_easy_cleanup(curl);
  126. }
  127. return 0;
  128. }