smtp-multi.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 <string.h>
  23. #include <curl/curl.h>
  24. /* This is an example showing how to send mail using libcurl's SMTP
  25. * capabilities. It builds on the smtp-mail.c example to demonstrate how to use
  26. * libcurl's multi interface.
  27. *
  28. * Note that this example requires libcurl 7.20.0 or above.
  29. */
  30. #define FROM "<sender@example.com>"
  31. #define TO "<recipient@example.com>"
  32. #define CC "<info@example.com>"
  33. #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
  34. static const char *payload_text[] = {
  35. "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
  36. "To: " TO "\r\n",
  37. "From: " FROM "(Example User)\r\n",
  38. "Cc: " CC "(Another example User)\r\n",
  39. "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n",
  40. "Subject: SMTP multi example message\r\n",
  41. "\r\n", /* empty line to divide headers from body, see RFC5322 */
  42. "The body of the message starts here.\r\n",
  43. "\r\n",
  44. "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
  45. "Check RFC5322.\r\n",
  46. NULL
  47. };
  48. struct upload_status {
  49. int lines_read;
  50. };
  51. static size_t payload_source(void *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. if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
  56. return 0;
  57. }
  58. data = payload_text[upload_ctx->lines_read];
  59. if(data) {
  60. size_t len = strlen(data);
  61. memcpy(ptr, data, len);
  62. upload_ctx->lines_read++;
  63. return len;
  64. }
  65. return 0;
  66. }
  67. static struct timeval tvnow(void)
  68. {
  69. struct timeval now;
  70. /* time() returns the value of time in seconds since the epoch */
  71. now.tv_sec = (long)time(NULL);
  72. now.tv_usec = 0;
  73. return now;
  74. }
  75. static long tvdiff(struct timeval newer, struct timeval older)
  76. {
  77. return (newer.tv_sec - older.tv_sec) * 1000 +
  78. (newer.tv_usec - older.tv_usec) / 1000;
  79. }
  80. int main(void)
  81. {
  82. CURL *curl;
  83. CURLM *mcurl;
  84. int still_running = 1;
  85. struct timeval mp_start;
  86. struct curl_slist *recipients = NULL;
  87. struct upload_status upload_ctx;
  88. upload_ctx.lines_read = 0;
  89. curl_global_init(CURL_GLOBAL_DEFAULT);
  90. curl = curl_easy_init();
  91. if(!curl)
  92. return 1;
  93. mcurl = curl_multi_init();
  94. if(!mcurl)
  95. return 2;
  96. /* This is the URL for your mailserver */
  97. curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
  98. /* Note that this option isn't strictly required, omitting it will result in
  99. * libcurl sending the MAIL FROM command with empty sender data. All
  100. * autoresponses should have an empty reverse-path, and should be directed
  101. * to the address in the reverse-path which triggered them. Otherwise, they
  102. * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details.
  103. */
  104. curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
  105. /* Add two recipients, in this particular case they correspond to the
  106. * To: and Cc: addressees in the header, but they could be any kind of
  107. * recipient. */
  108. recipients = curl_slist_append(recipients, TO);
  109. recipients = curl_slist_append(recipients, CC);
  110. curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
  111. /* We're using a callback function to specify the payload (the headers and
  112. * body of the message). You could just use the CURLOPT_READDATA option to
  113. * specify a FILE pointer to read from. */
  114. curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
  115. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
  116. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  117. /* Tell the multi stack about our easy handle */
  118. curl_multi_add_handle(mcurl, curl);
  119. /* Record the start time which we can use later */
  120. mp_start = tvnow();
  121. /* We start some action by calling perform right away */
  122. curl_multi_perform(mcurl, &still_running);
  123. while(still_running) {
  124. struct timeval timeout;
  125. fd_set fdread;
  126. fd_set fdwrite;
  127. fd_set fdexcep;
  128. int maxfd = -1;
  129. int rc;
  130. long curl_timeo = -1;
  131. /* Initialise the file descriptors */
  132. FD_ZERO(&fdread);
  133. FD_ZERO(&fdwrite);
  134. FD_ZERO(&fdexcep);
  135. /* Set a suitable timeout to play around with */
  136. timeout.tv_sec = 1;
  137. timeout.tv_usec = 0;
  138. curl_multi_timeout(mcurl, &curl_timeo);
  139. if(curl_timeo >= 0) {
  140. timeout.tv_sec = curl_timeo / 1000;
  141. if(timeout.tv_sec > 1)
  142. timeout.tv_sec = 1;
  143. else
  144. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  145. }
  146. /* Get file descriptors from the transfers */
  147. curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
  148. /* In a real-world program you OF COURSE check the return code of the
  149. function calls. On success, the value of maxfd is guaranteed to be
  150. greater or equal than -1. We call select(maxfd + 1, ...), specially in
  151. case of (maxfd == -1), we call select(0, ...), which is basically equal
  152. to sleep. */
  153. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  154. if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
  155. fprintf(stderr,
  156. "ABORTING: Since it seems that we would have run forever.\n");
  157. break;
  158. }
  159. switch(rc) {
  160. case -1: /* select error */
  161. break;
  162. case 0: /* timeout */
  163. default: /* action */
  164. curl_multi_perform(mcurl, &still_running);
  165. break;
  166. }
  167. }
  168. /* Free the list of recipients */
  169. curl_slist_free_all(recipients);
  170. /* Always cleanup */
  171. curl_multi_remove_handle(mcurl, curl);
  172. curl_multi_cleanup(mcurl);
  173. curl_easy_cleanup(curl);
  174. curl_global_cleanup();
  175. return 0;
  176. }