smtp-mime.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * Send SMTP 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 results in
  78. * 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. /* allow one of the recipients to fail and still consider it okay */
  92. curl_easy_setopt(curl, CURLOPT_MAIL_RCPT_ALLOWFAILS, 1L);
  93. /* Build and set the message header list. */
  94. for(cpp = headers_text; *cpp; cpp++)
  95. headers = curl_slist_append(headers, *cpp);
  96. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  97. /* Build the mime message. */
  98. mime = curl_mime_init(curl);
  99. /* The inline part is an alternative proposing the html and the text
  100. versions of the email. */
  101. alt = curl_mime_init(curl);
  102. /* HTML message. */
  103. part = curl_mime_addpart(alt);
  104. curl_mime_data(part, inline_html, CURL_ZERO_TERMINATED);
  105. curl_mime_type(part, "text/html");
  106. /* Text message. */
  107. part = curl_mime_addpart(alt);
  108. curl_mime_data(part, inline_text, CURL_ZERO_TERMINATED);
  109. /* Create the inline part. */
  110. part = curl_mime_addpart(mime);
  111. curl_mime_subparts(part, alt);
  112. curl_mime_type(part, "multipart/alternative");
  113. slist = curl_slist_append(NULL, "Content-Disposition: inline");
  114. curl_mime_headers(part, slist, 1);
  115. /* Add the current source program as an attachment. */
  116. part = curl_mime_addpart(mime);
  117. curl_mime_filedata(part, "smtp-mime.c");
  118. curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
  119. /* Send the message */
  120. res = curl_easy_perform(curl);
  121. /* Check for errors */
  122. if(res != CURLE_OK)
  123. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  124. curl_easy_strerror(res));
  125. /* Free lists. */
  126. curl_slist_free_all(recipients);
  127. curl_slist_free_all(headers);
  128. /* curl does not send the QUIT command until you call cleanup, so you
  129. * should be able to reuse this connection for additional messages
  130. * (setting CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and
  131. * calling curl_easy_perform() again. It may not be a good idea to keep
  132. * the connection open for a long time though (more than a few minutes may
  133. * result in the server timing out the connection), and you do want to
  134. * clean up in the end.
  135. */
  136. curl_easy_cleanup(curl);
  137. /* Free multipart message. */
  138. curl_mime_free(mime);
  139. }
  140. return (int)res;
  141. }