smtp-mail.c 4.8 KB

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