smtp-authzid.c 5.3 KB

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