imap-append.c 3.8 KB

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