2
0

lib1520.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2014 - 2020, Steve Holme, <steve_holme@hotmail.com>.
  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. #include "test.h"
  23. #include "memdebug.h"
  24. /*
  25. * This is the list of basic details you need to tweak to get things right.
  26. */
  27. #define TO "<recipient@example.com>"
  28. #define FROM "<sender@example.com>"
  29. static const char *payload_text[] = {
  30. "From: different\r\n",
  31. "To: another\r\n",
  32. "\r\n",
  33. "\r\n",
  34. ".\r\n",
  35. ".\r\n",
  36. "\r\n",
  37. ".\r\n",
  38. "\r\n",
  39. "body",
  40. NULL
  41. };
  42. struct upload_status {
  43. int lines_read;
  44. };
  45. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  46. {
  47. struct upload_status *upload_ctx = (struct upload_status *)userp;
  48. const char *data;
  49. if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
  50. return 0;
  51. }
  52. data = payload_text[upload_ctx->lines_read];
  53. if(data) {
  54. size_t len = strlen(data);
  55. memcpy(ptr, data, len);
  56. upload_ctx->lines_read++;
  57. return len;
  58. }
  59. return 0;
  60. }
  61. int test(char *URL)
  62. {
  63. CURLcode res;
  64. CURL *curl;
  65. struct curl_slist *rcpt_list = NULL;
  66. struct upload_status upload_ctx = {0};
  67. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  68. fprintf(stderr, "curl_global_init() failed\n");
  69. return TEST_ERR_MAJOR_BAD;
  70. }
  71. curl = curl_easy_init();
  72. if(!curl) {
  73. fprintf(stderr, "curl_easy_init() failed\n");
  74. curl_global_cleanup();
  75. return TEST_ERR_MAJOR_BAD;
  76. }
  77. rcpt_list = curl_slist_append(rcpt_list, TO);
  78. /* more addresses can be added here
  79. rcpt_list = curl_slist_append(rcpt_list, "<others@example.com>");
  80. */
  81. test_setopt(curl, CURLOPT_URL, URL);
  82. test_setopt(curl, CURLOPT_UPLOAD, 1L);
  83. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  84. test_setopt(curl, CURLOPT_READDATA, &upload_ctx);
  85. test_setopt(curl, CURLOPT_MAIL_FROM, FROM);
  86. test_setopt(curl, CURLOPT_MAIL_RCPT, rcpt_list);
  87. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  88. res = curl_easy_perform(curl);
  89. test_cleanup:
  90. curl_slist_free_all(rcpt_list);
  91. curl_easy_cleanup(curl);
  92. curl_global_cleanup();
  93. return (int)res;
  94. }