lib1520.c 2.9 KB

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