2
0

smtp-authzid.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 email on behalf of another user with SMTP
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <curl/curl.h>
  31. /*
  32. * This is a simple example show how to send an email using libcurl's SMTP
  33. * capabilities.
  34. *
  35. * Note that this example requires libcurl 7.66.0 or above.
  36. */
  37. /* The libcurl options want plain addresses, the viewable headers in the mail
  38. * can get a full name as well.
  39. */
  40. #define FROM_ADDR "<ursel@example.org>"
  41. #define SENDER_ADDR "<kurt@example.org>"
  42. #define TO_ADDR "<addressee@example.net>"
  43. #define FROM_MAIL "Ursel " FROM_ADDR
  44. #define SENDER_MAIL "Kurt " SENDER_ADDR
  45. #define TO_MAIL "A Receiver " TO_ADDR
  46. static const char *payload_text =
  47. "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
  48. "To: " TO_MAIL "\r\n"
  49. "From: " FROM_MAIL "\r\n"
  50. "Sender: " SENDER_MAIL "\r\n"
  51. "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
  52. "rfcpedant.example.org>\r\n"
  53. "Subject: SMTP example message\r\n"
  54. "\r\n" /* empty line to divide headers from body, see RFC 5322 */
  55. "The body of the message starts here.\r\n"
  56. "\r\n"
  57. "It could be a lot of lines, could be MIME encoded, whatever.\r\n"
  58. "Check RFC 5322.\r\n";
  59. struct upload_status {
  60. size_t bytes_read;
  61. };
  62. static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
  63. {
  64. struct upload_status *upload_ctx = (struct upload_status *)userp;
  65. const char *data;
  66. size_t room = size * nmemb;
  67. if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
  68. return 0;
  69. }
  70. data = &payload_text[upload_ctx->bytes_read];
  71. if(data) {
  72. size_t len = strlen(data);
  73. if(room < len)
  74. len = room;
  75. memcpy(ptr, data, len);
  76. upload_ctx->bytes_read += len;
  77. return len;
  78. }
  79. return 0;
  80. }
  81. int main(void)
  82. {
  83. CURL *curl;
  84. CURLcode res = CURLE_OK;
  85. struct curl_slist *recipients = NULL;
  86. struct upload_status upload_ctx = { 0 };
  87. curl = curl_easy_init();
  88. if(curl) {
  89. /* This is the URL for your mailserver. In this example we connect to the
  90. smtp-submission port as we require an authenticated connection. */
  91. curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com:587");
  92. /* Set the username and password */
  93. curl_easy_setopt(curl, CURLOPT_USERNAME, "kurt");
  94. curl_easy_setopt(curl, CURLOPT_PASSWORD, "xipj3plmq");
  95. /* Set the authorization identity (identity to act as) */
  96. curl_easy_setopt(curl, CURLOPT_SASL_AUTHZID, "ursel");
  97. /* Force PLAIN authentication */
  98. curl_easy_setopt(curl, CURLOPT_LOGIN_OPTIONS, "AUTH=PLAIN");
  99. /* Note that this option is not strictly required, omitting it results in
  100. * libcurl sending the MAIL FROM command with empty sender data. All
  101. * autoresponses should have an empty reverse-path, and should be directed
  102. * to the address in the reverse-path which triggered them. Otherwise,
  103. * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more
  104. * details.
  105. */
  106. curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_ADDR);
  107. /* Add a recipient, in this particular case it corresponds to the
  108. * To: addressee in the header. */
  109. recipients = curl_slist_append(recipients, TO_ADDR);
  110. curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
  111. /* We are using a callback function to specify the payload (the headers and
  112. * body of the message). You could just use the CURLOPT_READDATA option to
  113. * specify a FILE pointer to read from. */
  114. curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
  115. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
  116. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  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 the list of recipients */
  124. curl_slist_free_all(recipients);
  125. /* curl does not send the QUIT command until you call cleanup, so you
  126. * should be able to reuse this connection for additional messages
  127. * (setting CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and
  128. * calling curl_easy_perform() again. It may not be a good idea to keep
  129. * the connection open for a long time though (more than a few minutes may
  130. * result in the server timing out the connection), and you do want to
  131. * clean up in the end.
  132. */
  133. curl_easy_cleanup(curl);
  134. }
  135. return (int)res;
  136. }