xref: /aosp_15_r20/external/curl/docs/examples/smtp-authzid.c (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
2*6236dae4SAndroid Build Coastguard Worker  *                                  _   _ ____  _
3*6236dae4SAndroid Build Coastguard Worker  *  Project                     ___| | | |  _ \| |
4*6236dae4SAndroid Build Coastguard Worker  *                             / __| | | | |_) | |
5*6236dae4SAndroid Build Coastguard Worker  *                            | (__| |_| |  _ <| |___
6*6236dae4SAndroid Build Coastguard Worker  *                             \___|\___/|_| \_\_____|
7*6236dae4SAndroid Build Coastguard Worker  *
8*6236dae4SAndroid Build Coastguard Worker  * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9*6236dae4SAndroid Build Coastguard Worker  *
10*6236dae4SAndroid Build Coastguard Worker  * This software is licensed as described in the file COPYING, which
11*6236dae4SAndroid Build Coastguard Worker  * you should have received as part of this distribution. The terms
12*6236dae4SAndroid Build Coastguard Worker  * are also available at https://curl.se/docs/copyright.html.
13*6236dae4SAndroid Build Coastguard Worker  *
14*6236dae4SAndroid Build Coastguard Worker  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*6236dae4SAndroid Build Coastguard Worker  * copies of the Software, and permit persons to whom the Software is
16*6236dae4SAndroid Build Coastguard Worker  * furnished to do so, under the terms of the COPYING file.
17*6236dae4SAndroid Build Coastguard Worker  *
18*6236dae4SAndroid Build Coastguard Worker  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*6236dae4SAndroid Build Coastguard Worker  * KIND, either express or implied.
20*6236dae4SAndroid Build Coastguard Worker  *
21*6236dae4SAndroid Build Coastguard Worker  * SPDX-License-Identifier: curl
22*6236dae4SAndroid Build Coastguard Worker  *
23*6236dae4SAndroid Build Coastguard Worker  ***************************************************************************/
24*6236dae4SAndroid Build Coastguard Worker 
25*6236dae4SAndroid Build Coastguard Worker /* <DESC>
26*6236dae4SAndroid Build Coastguard Worker  * Send email on behalf of another user with SMTP
27*6236dae4SAndroid Build Coastguard Worker  * </DESC>
28*6236dae4SAndroid Build Coastguard Worker  */
29*6236dae4SAndroid Build Coastguard Worker 
30*6236dae4SAndroid Build Coastguard Worker #include <stdio.h>
31*6236dae4SAndroid Build Coastguard Worker #include <string.h>
32*6236dae4SAndroid Build Coastguard Worker #include <curl/curl.h>
33*6236dae4SAndroid Build Coastguard Worker 
34*6236dae4SAndroid Build Coastguard Worker /*
35*6236dae4SAndroid Build Coastguard Worker  * This is a simple example show how to send an email using libcurl's SMTP
36*6236dae4SAndroid Build Coastguard Worker  * capabilities.
37*6236dae4SAndroid Build Coastguard Worker  *
38*6236dae4SAndroid Build Coastguard Worker  * Note that this example requires libcurl 7.66.0 or above.
39*6236dae4SAndroid Build Coastguard Worker  */
40*6236dae4SAndroid Build Coastguard Worker 
41*6236dae4SAndroid Build Coastguard Worker /* The libcurl options want plain addresses, the viewable headers in the mail
42*6236dae4SAndroid Build Coastguard Worker  * can get a full name as well.
43*6236dae4SAndroid Build Coastguard Worker  */
44*6236dae4SAndroid Build Coastguard Worker #define FROM_ADDR    "<[email protected]>"
45*6236dae4SAndroid Build Coastguard Worker #define SENDER_ADDR  "<[email protected]>"
46*6236dae4SAndroid Build Coastguard Worker #define TO_ADDR      "<[email protected]>"
47*6236dae4SAndroid Build Coastguard Worker 
48*6236dae4SAndroid Build Coastguard Worker #define FROM_MAIL    "Ursel " FROM_ADDR
49*6236dae4SAndroid Build Coastguard Worker #define SENDER_MAIL  "Kurt " SENDER_ADDR
50*6236dae4SAndroid Build Coastguard Worker #define TO_MAIL      "A Receiver " TO_ADDR
51*6236dae4SAndroid Build Coastguard Worker 
52*6236dae4SAndroid Build Coastguard Worker static const char *payload_text =
53*6236dae4SAndroid Build Coastguard Worker   "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
54*6236dae4SAndroid Build Coastguard Worker   "To: " TO_MAIL "\r\n"
55*6236dae4SAndroid Build Coastguard Worker   "From: " FROM_MAIL "\r\n"
56*6236dae4SAndroid Build Coastguard Worker   "Sender: " SENDER_MAIL "\r\n"
57*6236dae4SAndroid Build Coastguard Worker   "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
58*6236dae4SAndroid Build Coastguard Worker   "rfcpedant.example.org>\r\n"
59*6236dae4SAndroid Build Coastguard Worker   "Subject: SMTP example message\r\n"
60*6236dae4SAndroid Build Coastguard Worker   "\r\n" /* empty line to divide headers from body, see RFC 5322 */
61*6236dae4SAndroid Build Coastguard Worker   "The body of the message starts here.\r\n"
62*6236dae4SAndroid Build Coastguard Worker   "\r\n"
63*6236dae4SAndroid Build Coastguard Worker   "It could be a lot of lines, could be MIME encoded, whatever.\r\n"
64*6236dae4SAndroid Build Coastguard Worker   "Check RFC 5322.\r\n";
65*6236dae4SAndroid Build Coastguard Worker 
66*6236dae4SAndroid Build Coastguard Worker struct upload_status {
67*6236dae4SAndroid Build Coastguard Worker   size_t bytes_read;
68*6236dae4SAndroid Build Coastguard Worker };
69*6236dae4SAndroid Build Coastguard Worker 
payload_source(char * ptr,size_t size,size_t nmemb,void * userp)70*6236dae4SAndroid Build Coastguard Worker static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
71*6236dae4SAndroid Build Coastguard Worker {
72*6236dae4SAndroid Build Coastguard Worker   struct upload_status *upload_ctx = (struct upload_status *)userp;
73*6236dae4SAndroid Build Coastguard Worker   const char *data;
74*6236dae4SAndroid Build Coastguard Worker   size_t room = size * nmemb;
75*6236dae4SAndroid Build Coastguard Worker 
76*6236dae4SAndroid Build Coastguard Worker   if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
77*6236dae4SAndroid Build Coastguard Worker     return 0;
78*6236dae4SAndroid Build Coastguard Worker   }
79*6236dae4SAndroid Build Coastguard Worker 
80*6236dae4SAndroid Build Coastguard Worker   data = &payload_text[upload_ctx->bytes_read];
81*6236dae4SAndroid Build Coastguard Worker 
82*6236dae4SAndroid Build Coastguard Worker   if(data) {
83*6236dae4SAndroid Build Coastguard Worker     size_t len = strlen(data);
84*6236dae4SAndroid Build Coastguard Worker     if(room < len)
85*6236dae4SAndroid Build Coastguard Worker       len = room;
86*6236dae4SAndroid Build Coastguard Worker     memcpy(ptr, data, len);
87*6236dae4SAndroid Build Coastguard Worker     upload_ctx->bytes_read += len;
88*6236dae4SAndroid Build Coastguard Worker 
89*6236dae4SAndroid Build Coastguard Worker     return len;
90*6236dae4SAndroid Build Coastguard Worker   }
91*6236dae4SAndroid Build Coastguard Worker 
92*6236dae4SAndroid Build Coastguard Worker   return 0;
93*6236dae4SAndroid Build Coastguard Worker }
94*6236dae4SAndroid Build Coastguard Worker 
main(void)95*6236dae4SAndroid Build Coastguard Worker int main(void)
96*6236dae4SAndroid Build Coastguard Worker {
97*6236dae4SAndroid Build Coastguard Worker   CURL *curl;
98*6236dae4SAndroid Build Coastguard Worker   CURLcode res = CURLE_OK;
99*6236dae4SAndroid Build Coastguard Worker   struct curl_slist *recipients = NULL;
100*6236dae4SAndroid Build Coastguard Worker   struct upload_status upload_ctx = { 0 };
101*6236dae4SAndroid Build Coastguard Worker 
102*6236dae4SAndroid Build Coastguard Worker   curl = curl_easy_init();
103*6236dae4SAndroid Build Coastguard Worker   if(curl) {
104*6236dae4SAndroid Build Coastguard Worker     /* This is the URL for your mailserver. In this example we connect to the
105*6236dae4SAndroid Build Coastguard Worker        smtp-submission port as we require an authenticated connection. */
106*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com:587");
107*6236dae4SAndroid Build Coastguard Worker 
108*6236dae4SAndroid Build Coastguard Worker     /* Set the username and password */
109*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_USERNAME, "kurt");
110*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_PASSWORD, "xipj3plmq");
111*6236dae4SAndroid Build Coastguard Worker 
112*6236dae4SAndroid Build Coastguard Worker     /* Set the authorization identity (identity to act as) */
113*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_SASL_AUTHZID, "ursel");
114*6236dae4SAndroid Build Coastguard Worker 
115*6236dae4SAndroid Build Coastguard Worker     /* Force PLAIN authentication */
116*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_LOGIN_OPTIONS, "AUTH=PLAIN");
117*6236dae4SAndroid Build Coastguard Worker 
118*6236dae4SAndroid Build Coastguard Worker     /* Note that this option is not strictly required, omitting it results in
119*6236dae4SAndroid Build Coastguard Worker      * libcurl sending the MAIL FROM command with empty sender data. All
120*6236dae4SAndroid Build Coastguard Worker      * autoresponses should have an empty reverse-path, and should be directed
121*6236dae4SAndroid Build Coastguard Worker      * to the address in the reverse-path which triggered them. Otherwise,
122*6236dae4SAndroid Build Coastguard Worker      * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more
123*6236dae4SAndroid Build Coastguard Worker      * details.
124*6236dae4SAndroid Build Coastguard Worker      */
125*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_ADDR);
126*6236dae4SAndroid Build Coastguard Worker 
127*6236dae4SAndroid Build Coastguard Worker     /* Add a recipient, in this particular case it corresponds to the
128*6236dae4SAndroid Build Coastguard Worker      * To: addressee in the header. */
129*6236dae4SAndroid Build Coastguard Worker     recipients = curl_slist_append(recipients, TO_ADDR);
130*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
131*6236dae4SAndroid Build Coastguard Worker 
132*6236dae4SAndroid Build Coastguard Worker     /* We are using a callback function to specify the payload (the headers and
133*6236dae4SAndroid Build Coastguard Worker      * body of the message). You could just use the CURLOPT_READDATA option to
134*6236dae4SAndroid Build Coastguard Worker      * specify a FILE pointer to read from. */
135*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
136*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
137*6236dae4SAndroid Build Coastguard Worker     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
138*6236dae4SAndroid Build Coastguard Worker 
139*6236dae4SAndroid Build Coastguard Worker     /* Send the message */
140*6236dae4SAndroid Build Coastguard Worker     res = curl_easy_perform(curl);
141*6236dae4SAndroid Build Coastguard Worker 
142*6236dae4SAndroid Build Coastguard Worker     /* Check for errors */
143*6236dae4SAndroid Build Coastguard Worker     if(res != CURLE_OK)
144*6236dae4SAndroid Build Coastguard Worker       fprintf(stderr, "curl_easy_perform() failed: %s\n",
145*6236dae4SAndroid Build Coastguard Worker               curl_easy_strerror(res));
146*6236dae4SAndroid Build Coastguard Worker 
147*6236dae4SAndroid Build Coastguard Worker     /* Free the list of recipients */
148*6236dae4SAndroid Build Coastguard Worker     curl_slist_free_all(recipients);
149*6236dae4SAndroid Build Coastguard Worker 
150*6236dae4SAndroid Build Coastguard Worker     /* curl does not send the QUIT command until you call cleanup, so you
151*6236dae4SAndroid Build Coastguard Worker      * should be able to reuse this connection for additional messages
152*6236dae4SAndroid Build Coastguard Worker      * (setting CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and
153*6236dae4SAndroid Build Coastguard Worker      * calling curl_easy_perform() again. It may not be a good idea to keep
154*6236dae4SAndroid Build Coastguard Worker      * the connection open for a long time though (more than a few minutes may
155*6236dae4SAndroid Build Coastguard Worker      * result in the server timing out the connection), and you do want to
156*6236dae4SAndroid Build Coastguard Worker      * clean up in the end.
157*6236dae4SAndroid Build Coastguard Worker      */
158*6236dae4SAndroid Build Coastguard Worker     curl_easy_cleanup(curl);
159*6236dae4SAndroid Build Coastguard Worker   }
160*6236dae4SAndroid Build Coastguard Worker 
161*6236dae4SAndroid Build Coastguard Worker   return (int)res;
162*6236dae4SAndroid Build Coastguard Worker }
163