smtp-multi.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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.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. /* <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. * Note that this example requires libcurl 7.20.0 or above.
  33. */
  34. #define FROM "<sender@example.com>"
  35. #define TO "<recipient@example.com>"
  36. #define CC "<info@example.com>"
  37. #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
  38. static const char *payload_text[] = {
  39. "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
  40. "To: " TO "\r\n",
  41. "From: " FROM " (Example User)\r\n",
  42. "Cc: " CC " (Another example User)\r\n",
  43. "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
  44. "rfcpedant.example.org>\r\n",
  45. "Subject: SMTP multi example message\r\n",
  46. "\r\n", /* empty line to divide headers from body, see RFC5322 */
  47. "The body of the message starts here.\r\n",
  48. "\r\n",
  49. "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
  50. "Check RFC5322.\r\n",
  51. NULL
  52. };
  53. struct upload_status {
  54. int lines_read;
  55. };
  56. static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
  57. {
  58. struct upload_status *upload_ctx = (struct upload_status *)userp;
  59. const char *data;
  60. if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
  61. return 0;
  62. }
  63. data = payload_text[upload_ctx->lines_read];
  64. if(data) {
  65. size_t len = strlen(data);
  66. memcpy(ptr, data, len);
  67. upload_ctx->lines_read++;
  68. return len;
  69. }
  70. return 0;
  71. }
  72. static struct timeval tvnow(void)
  73. {
  74. struct timeval now;
  75. /* time() returns the value of time in seconds since the epoch */
  76. now.tv_sec = (long)time(NULL);
  77. now.tv_usec = 0;
  78. return now;
  79. }
  80. static long tvdiff(struct timeval newer, struct timeval older)
  81. {
  82. return (newer.tv_sec - older.tv_sec) * 1000 +
  83. (newer.tv_usec - older.tv_usec) / 1000;
  84. }
  85. int main(void)
  86. {
  87. CURL *curl;
  88. CURLM *mcurl;
  89. int still_running = 1;
  90. struct timeval mp_start;
  91. struct curl_slist *recipients = NULL;
  92. struct upload_status upload_ctx;
  93. upload_ctx.lines_read = 0;
  94. curl_global_init(CURL_GLOBAL_DEFAULT);
  95. curl = curl_easy_init();
  96. if(!curl)
  97. return 1;
  98. mcurl = curl_multi_init();
  99. if(!mcurl)
  100. return 2;
  101. /* This is the URL for your mailserver */
  102. curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
  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. /* Tell the multi stack about our easy handle */
  123. curl_multi_add_handle(mcurl, curl);
  124. /* Record the start time which we can use later */
  125. mp_start = tvnow();
  126. /* We start some action by calling perform right away */
  127. curl_multi_perform(mcurl, &still_running);
  128. while(still_running) {
  129. struct timeval timeout;
  130. fd_set fdread;
  131. fd_set fdwrite;
  132. fd_set fdexcep;
  133. int maxfd = -1;
  134. int rc;
  135. CURLMcode mc; /* curl_multi_fdset() return code */
  136. long curl_timeo = -1;
  137. /* Initialise the file descriptors */
  138. FD_ZERO(&fdread);
  139. FD_ZERO(&fdwrite);
  140. FD_ZERO(&fdexcep);
  141. /* Set a suitable timeout to play around with */
  142. timeout.tv_sec = 1;
  143. timeout.tv_usec = 0;
  144. curl_multi_timeout(mcurl, &curl_timeo);
  145. if(curl_timeo >= 0) {
  146. timeout.tv_sec = curl_timeo / 1000;
  147. if(timeout.tv_sec > 1)
  148. timeout.tv_sec = 1;
  149. else
  150. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  151. }
  152. /* get file descriptors from the transfers */
  153. mc = curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
  154. if(mc != CURLM_OK) {
  155. fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
  156. break;
  157. }
  158. /* On success the value of maxfd is guaranteed to be >= -1. We call
  159. select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
  160. no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
  161. to sleep 100ms, which is the minimum suggested value in the
  162. curl_multi_fdset() doc. */
  163. if(maxfd == -1) {
  164. #ifdef _WIN32
  165. Sleep(100);
  166. rc = 0;
  167. #else
  168. /* Portable sleep for platforms other than Windows. */
  169. struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
  170. rc = select(0, NULL, NULL, NULL, &wait);
  171. #endif
  172. }
  173. else {
  174. /* Note that on some platforms 'timeout' may be modified by select().
  175. If you need access to the original value save a copy beforehand. */
  176. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  177. }
  178. if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
  179. fprintf(stderr,
  180. "ABORTING: Since it seems that we would have run forever.\n");
  181. break;
  182. }
  183. switch(rc) {
  184. case -1: /* select error */
  185. break;
  186. case 0: /* timeout */
  187. default: /* action */
  188. curl_multi_perform(mcurl, &still_running);
  189. break;
  190. }
  191. }
  192. /* Free the list of recipients */
  193. curl_slist_free_all(recipients);
  194. /* Always cleanup */
  195. curl_multi_remove_handle(mcurl, curl);
  196. curl_multi_cleanup(mcurl);
  197. curl_easy_cleanup(curl);
  198. curl_global_cleanup();
  199. return 0;
  200. }