smtp-multi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 the multi interface
  24. * </DESC>
  25. */
  26. #include <string.h>
  27. #include <curl/curl.h>
  28. /* This is an example showing how to send mail using libcurl's SMTP
  29. * capabilities. It builds on the smtp-mail.c example to demonstrate how to use
  30. * libcurl's multi interface.
  31. */
  32. #define FROM_MAIL "<[email protected]>"
  33. #define TO_MAIL "<[email protected]>"
  34. #define CC_MAIL "<[email protected]>"
  35. static const char *payload_text =
  36. "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
  37. "To: " TO_MAIL "\r\n"
  38. "From: " FROM_MAIL "\r\n"
  39. "Cc: " CC_MAIL "\r\n"
  40. "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
  41. "rfcpedant.example.org>\r\n"
  42. "Subject: SMTP example message\r\n"
  43. "\r\n" /* empty line to divide headers from body, see RFC5322 */
  44. "The body of the message starts here.\r\n"
  45. "\r\n"
  46. "It could be a lot of lines, could be MIME encoded, whatever.\r\n"
  47. "Check RFC5322.\r\n";
  48. struct upload_status {
  49. size_t bytes_read;
  50. };
  51. static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
  52. {
  53. struct upload_status *upload_ctx = (struct upload_status *)userp;
  54. const char *data;
  55. size_t room = size * nmemb;
  56. if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
  57. return 0;
  58. }
  59. data = &payload_text[upload_ctx->bytes_read];
  60. if(data) {
  61. size_t len = strlen(data);
  62. if(room < len)
  63. len = room;
  64. memcpy(ptr, data, len);
  65. upload_ctx->bytes_read += len;
  66. return len;
  67. }
  68. return 0;
  69. }
  70. int main(void)
  71. {
  72. CURL *curl;
  73. CURLM *mcurl;
  74. int still_running = 1;
  75. struct curl_slist *recipients = NULL;
  76. struct upload_status upload_ctx = { 0 };
  77. curl_global_init(CURL_GLOBAL_DEFAULT);
  78. curl = curl_easy_init();
  79. if(!curl)
  80. return 1;
  81. mcurl = curl_multi_init();
  82. if(!mcurl)
  83. return 2;
  84. /* This is the URL for your mailserver */
  85. curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
  86. /* Note that this option is not strictly required, omitting it will result in
  87. * libcurl sending the MAIL FROM command with empty sender data. All
  88. * autoresponses should have an empty reverse-path, and should be directed
  89. * to the address in the reverse-path which triggered them. Otherwise, they
  90. * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details.
  91. */
  92. curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_MAIL);
  93. /* Add two recipients, in this particular case they correspond to the
  94. * To: and Cc: addressees in the header, but they could be any kind of
  95. * recipient. */
  96. recipients = curl_slist_append(recipients, TO_MAIL);
  97. recipients = curl_slist_append(recipients, CC_MAIL);
  98. curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
  99. /* We are using a callback function to specify the payload (the headers and
  100. * body of the message). You could just use the CURLOPT_READDATA option to
  101. * specify a FILE pointer to read from. */
  102. curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
  103. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
  104. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  105. /* Tell the multi stack about our easy handle */
  106. curl_multi_add_handle(mcurl, curl);
  107. do {
  108. CURLMcode mc = curl_multi_perform(mcurl, &still_running);
  109. if(still_running)
  110. /* wait for activity, timeout or "nothing" */
  111. mc = curl_multi_poll(mcurl, NULL, 0, 1000, NULL);
  112. if(mc)
  113. break;
  114. } while(still_running);
  115. /* Free the list of recipients */
  116. curl_slist_free_all(recipients);
  117. /* Always cleanup */
  118. curl_multi_remove_handle(mcurl, curl);
  119. curl_multi_cleanup(mcurl);
  120. curl_easy_cleanup(curl);
  121. curl_global_cleanup();
  122. return 0;
  123. }