smtp-ssl.c 5.8 KB

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