smtp-mime.c 5.5 KB

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