smtp-ssl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * Send SMTP email using implicit SSL
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <curl/curl.h>
  31. /* This is a simple example showing how to send mail using libcurl's SMTP
  32. * capabilities. It builds on the smtp-mail.c example to add authentication
  33. * and, more importantly, transport security to protect the authentication
  34. * details from being snooped.
  35. *
  36. * Note that this example requires libcurl 7.20.0 or above.
  37. */
  38. #define FROM_MAIL "<sender@example.com>"
  39. #define TO_MAIL "<recipient@example.com>"
  40. #define CC_MAIL "<info@example.com>"
  41. static const char *payload_text =
  42. "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
  43. "To: " TO_MAIL "\r\n"
  44. "From: " FROM_MAIL "\r\n"
  45. "Cc: " CC_MAIL "\r\n"
  46. "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
  47. "rfcpedant.example.org>\r\n"
  48. "Subject: SMTP example message\r\n"
  49. "\r\n" /* empty line to divide headers from body, see RFC 5322 */
  50. "The body of the message starts here.\r\n"
  51. "\r\n"
  52. "It could be a lot of lines, could be MIME encoded, whatever.\r\n"
  53. "Check RFC 5322.\r\n";
  54. struct upload_status {
  55. size_t bytes_read;
  56. };
  57. static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
  58. {
  59. struct upload_status *upload_ctx = (struct upload_status *)userp;
  60. const char *data;
  61. size_t room = size * nmemb;
  62. if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
  63. return 0;
  64. }
  65. data = &payload_text[upload_ctx->bytes_read];
  66. if(data) {
  67. size_t len = strlen(data);
  68. if(room < len)
  69. len = room;
  70. memcpy(ptr, data, len);
  71. upload_ctx->bytes_read += len;
  72. return len;
  73. }
  74. return 0;
  75. }
  76. int main(void)
  77. {
  78. CURL *curl;
  79. CURLcode res = CURLE_OK;
  80. struct curl_slist *recipients = NULL;
  81. struct upload_status upload_ctx = { 0 };
  82. curl = curl_easy_init();
  83. if(curl) {
  84. /* Set username and password */
  85. curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
  86. curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
  87. /* This is the URL for your mailserver. Note the use of smtps:// rather
  88. * than smtp:// to request a SSL based connection. */
  89. curl_easy_setopt(curl, CURLOPT_URL, "smtps://mainserver.example.net");
  90. /* If you want to connect to a site who is not using a certificate that is
  91. * signed by one of the certs in the CA bundle you have, you can skip the
  92. * verification of the server's certificate. This makes the connection
  93. * A LOT LESS SECURE.
  94. *
  95. * If you have a CA cert for the server stored someplace else than in the
  96. * default bundle, then the CURLOPT_CAPATH option might come handy for
  97. * you. */
  98. #ifdef SKIP_PEER_VERIFICATION
  99. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  100. #endif
  101. /* If the site you are connecting to uses a different host name that what
  102. * they have mentioned in their server certificate's commonName (or
  103. * subjectAltName) fields, libcurl will refuse to connect. You can skip
  104. * this check, but this will make the connection less secure. */
  105. #ifdef SKIP_HOSTNAME_VERIFICATION
  106. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  107. #endif
  108. /* Note that this option is not strictly required, omitting it will result
  109. * in libcurl sending the MAIL FROM command with empty sender data. All
  110. * autoresponses should have an empty reverse-path, and should be directed
  111. * to the address in the reverse-path which triggered them. Otherwise,
  112. * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more
  113. * details.
  114. */
  115. curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_MAIL);
  116. /* Add two recipients, in this particular case they correspond to the
  117. * To: and Cc: addressees in the header, but they could be any kind of
  118. * recipient. */
  119. recipients = curl_slist_append(recipients, TO_MAIL);
  120. recipients = curl_slist_append(recipients, CC_MAIL);
  121. curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
  122. /* We are using a callback function to specify the payload (the headers and
  123. * body of the message). You could just use the CURLOPT_READDATA option to
  124. * specify a FILE pointer to read from. */
  125. curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
  126. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
  127. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  128. /* Since the traffic will be encrypted, it is very useful to turn on debug
  129. * information within libcurl to see what is happening during the
  130. * transfer */
  131. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  132. /* Send the message */
  133. res = curl_easy_perform(curl);
  134. /* Check for errors */
  135. if(res != CURLE_OK)
  136. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  137. curl_easy_strerror(res));
  138. /* Free the list of recipients */
  139. curl_slist_free_all(recipients);
  140. /* Always cleanup */
  141. curl_easy_cleanup(curl);
  142. }
  143. return (int)res;
  144. }