smtp-mime.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. * SMTP example showing how to send mime emails
  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 mime mail using libcurl's SMTP
  32. * capabilities. For an example of using the multi interface please see
  33. * smtp-multi.c.
  34. *
  35. * Note that this example requires libcurl 7.56.0 or above.
  36. */
  37. #define FROM "<sender@example.org>"
  38. #define TO "<addressee@example.net>"
  39. #define CC "<info@example.org>"
  40. static const char *headers_text[] = {
  41. "Date: Tue, 22 Aug 2017 14:08:43 +0100",
  42. "To: " TO,
  43. "From: " FROM " (Example User)",
  44. "Cc: " CC " (Another example User)",
  45. "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
  46. "rfcpedant.example.org>",
  47. "Subject: example sending a MIME-formatted message",
  48. NULL
  49. };
  50. static const char inline_text[] =
  51. "This is the inline text message of the email.\r\n"
  52. "\r\n"
  53. " It could be a lot of lines that would be displayed in an email\r\n"
  54. "viewer that is not able to handle HTML.\r\n";
  55. static const char inline_html[] =
  56. "<html><body>\r\n"
  57. "<p>This is the inline <b>HTML</b> message of the email.</p>"
  58. "<br />\r\n"
  59. "<p>It could be a lot of HTML data that would be displayed by "
  60. "email viewers able to handle HTML.</p>"
  61. "</body></html>\r\n";
  62. int main(void)
  63. {
  64. CURL *curl;
  65. CURLcode res = CURLE_OK;
  66. curl = curl_easy_init();
  67. if(curl) {
  68. struct curl_slist *headers = NULL;
  69. struct curl_slist *recipients = NULL;
  70. struct curl_slist *slist = NULL;
  71. curl_mime *mime;
  72. curl_mime *alt;
  73. curl_mimepart *part;
  74. const char **cpp;
  75. /* This is the URL for your mailserver */
  76. curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
  77. /* Note that this option is not strictly required, omitting it will result
  78. * in libcurl sending the MAIL FROM command with empty sender data. All
  79. * autoresponses should have an empty reverse-path, and should be directed
  80. * to the address in the reverse-path which triggered them. Otherwise,
  81. * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more
  82. * 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. /* Build and set the message header list. */
  92. for(cpp = headers_text; *cpp; cpp++)
  93. headers = curl_slist_append(headers, *cpp);
  94. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  95. /* Build the mime message. */
  96. mime = curl_mime_init(curl);
  97. /* The inline part is an alternative proposing the html and the text
  98. versions of the email. */
  99. alt = curl_mime_init(curl);
  100. /* HTML message. */
  101. part = curl_mime_addpart(alt);
  102. curl_mime_data(part, inline_html, CURL_ZERO_TERMINATED);
  103. curl_mime_type(part, "text/html");
  104. /* Text message. */
  105. part = curl_mime_addpart(alt);
  106. curl_mime_data(part, inline_text, CURL_ZERO_TERMINATED);
  107. /* Create the inline part. */
  108. part = curl_mime_addpart(mime);
  109. curl_mime_subparts(part, alt);
  110. curl_mime_type(part, "multipart/alternative");
  111. slist = curl_slist_append(NULL, "Content-Disposition: inline");
  112. curl_mime_headers(part, slist, 1);
  113. /* Add the current source program as an attachment. */
  114. part = curl_mime_addpart(mime);
  115. curl_mime_filedata(part, "smtp-mime.c");
  116. curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
  117. /* Send the message */
  118. res = curl_easy_perform(curl);
  119. /* Check for errors */
  120. if(res != CURLE_OK)
  121. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  122. curl_easy_strerror(res));
  123. /* Free lists. */
  124. curl_slist_free_all(recipients);
  125. curl_slist_free_all(headers);
  126. /* curl will not send the QUIT command until you call cleanup, so you
  127. * should be able to re-use this connection for additional messages
  128. * (setting CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and
  129. * calling curl_easy_perform() again. It may not be a good idea to keep
  130. * the connection open for a very long time though (more than a few
  131. * minutes may result in the server timing out the connection), and you do
  132. * want to clean up in the end.
  133. */
  134. curl_easy_cleanup(curl);
  135. /* Free multipart message. */
  136. curl_mime_free(mime);
  137. }
  138. return (int)res;
  139. }