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