imap-append.c 3.5 KB

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