2
0

imap-append.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. /* <DESC>
  25. * Send email with IMAP
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <curl/curl.h>
  31. /* This is a simple example showing how to send mail using libcurl's IMAP
  32. * capabilities.
  33. *
  34. * Note that this example requires libcurl 7.30.0 or above.
  35. */
  36. #define FROM "<sender@example.org>"
  37. #define TO "<addressee@example.net>"
  38. #define CC "<info@example.org>"
  39. static const char *payload_text =
  40. "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
  41. "To: " TO "\r\n"
  42. "From: " FROM "(Example User)\r\n"
  43. "Cc: " CC "(Another example User)\r\n"
  44. "Message-ID: "
  45. "<dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n"
  46. "Subject: IMAP example message\r\n"
  47. "\r\n" /* empty line to divide headers from body, see RFC 5322 */
  48. "The body of the message starts here.\r\n"
  49. "\r\n"
  50. "It could be a lot of lines, could be MIME encoded, whatever.\r\n"
  51. "Check RFC 5322.\r\n";
  52. struct upload_status {
  53. size_t bytes_read;
  54. };
  55. static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
  56. {
  57. struct upload_status *upload_ctx = (struct upload_status *)userp;
  58. const char *data;
  59. size_t room = size * nmemb;
  60. if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
  61. return 0;
  62. }
  63. data = &payload_text[upload_ctx->bytes_read];
  64. if(*data) {
  65. size_t len = strlen(data);
  66. if(room < len)
  67. len = room;
  68. memcpy(ptr, data, len);
  69. upload_ctx->bytes_read += len;
  70. return len;
  71. }
  72. return 0;
  73. }
  74. int main(void)
  75. {
  76. CURL *curl;
  77. CURLcode res = CURLE_OK;
  78. curl = curl_easy_init();
  79. if(curl) {
  80. size_t filesize;
  81. long infilesize = LONG_MAX;
  82. struct upload_status upload_ctx = { 0 };
  83. /* Set username and password */
  84. curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
  85. curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
  86. /* This will create a new message in folder "Sent". */
  87. curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/Sent");
  88. /* In this case, we are using a callback function to specify the data. You
  89. * could just use the CURLOPT_READDATA option to specify a FILE pointer to
  90. * read from. */
  91. curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
  92. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
  93. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  94. filesize = strlen(payload_text);
  95. if(filesize <= LONG_MAX)
  96. infilesize = (long)filesize;
  97. curl_easy_setopt(curl, CURLOPT_INFILESIZE, infilesize);
  98. /* Perform the append */
  99. res = curl_easy_perform(curl);
  100. /* Check for errors */
  101. if(res != CURLE_OK)
  102. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  103. curl_easy_strerror(res));
  104. /* Always cleanup */
  105. curl_easy_cleanup(curl);
  106. }
  107. return (int)res;
  108. }