smtp-tls.c 6.1 KB

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