smtp-mail.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 SMTP
  26. * capabilities. For an exmaple of using the multi interface please see
  27. * smtp-multi.c.
  28. *
  29. * Note that this example requires libcurl 7.20.0 or above.
  30. */
  31. #define FROM "<sender@example.org>"
  32. #define TO "<addressee@example.net>"
  33. #define CC "<info@example.org>"
  34. static const char *payload_text[] = {
  35. "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
  36. "To: " TO "\r\n",
  37. "From: " FROM "(Example User)\r\n",
  38. "Cc: " CC "(Another example User)\r\n",
  39. "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n",
  40. "Subject: SMTP example message\r\n",
  41. "\r\n", /* empty line to divide headers from body, see RFC5322 */
  42. "The body of the message starts here.\r\n",
  43. "\r\n",
  44. "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
  45. "Check RFC5322.\r\n",
  46. NULL
  47. };
  48. struct upload_status {
  49. int lines_read;
  50. };
  51. static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
  52. {
  53. struct upload_status *upload_ctx = (struct upload_status *)userp;
  54. const char *data;
  55. if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
  56. return 0;
  57. }
  58. data = payload_text[upload_ctx->lines_read];
  59. if(data) {
  60. size_t len = strlen(data);
  61. memcpy(ptr, data, len);
  62. upload_ctx->lines_read++;
  63. return len;
  64. }
  65. return 0;
  66. }
  67. int main(void)
  68. {
  69. CURL *curl;
  70. CURLcode res = CURLE_OK;
  71. struct curl_slist *recipients = NULL;
  72. struct upload_status upload_ctx;
  73. upload_ctx.lines_read = 0;
  74. curl = curl_easy_init();
  75. if(curl) {
  76. /* This is the URL for your mailserver */
  77. curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
  78. /* Note that this option isn't strictly required, omitting it will result in
  79. * libcurl sending the MAIL FROM command with empty sender data. All
  80. * autoresponses should have an empty reverse-path, and should be directed
  81. * to the address in the reverse-path which triggered them. Otherwise, they
  82. * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details.
  83. */
  84. curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
  85. /* Add two recipients, in this particular case they correspond to the
  86. * To: and Cc: addressees in the header, but they could be any kind of
  87. * recipient. */
  88. recipients = curl_slist_append(recipients, TO);
  89. recipients = curl_slist_append(recipients, CC);
  90. curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
  91. /* We're using a callback function to specify the payload (the headers and
  92. * body of the message). You could just use the CURLOPT_READDATA option to
  93. * specify a FILE pointer to read from. */
  94. curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
  95. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
  96. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  97. /* Send the message */
  98. res = curl_easy_perform(curl);
  99. /* Check for errors */
  100. if(res != CURLE_OK)
  101. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  102. curl_easy_strerror(res));
  103. /* Free the list of recipients */
  104. curl_slist_free_all(recipients);
  105. /* curl won't send the QUIT command until you call cleanup, so you should be
  106. * able to re-use this connection for additional messages (setting
  107. * CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and calling
  108. * curl_easy_perform() again. It may not be a good idea to keep the
  109. * connection open for a very long time though (more than a few minutes may
  110. * result in the server timing out the connection), and you do want to clean
  111. * up in the end.
  112. */
  113. curl_easy_cleanup(curl);
  114. }
  115. return (int)res;
  116. }