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