xref: /curl/docs/examples/smtp-ssl.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 SMTP email using implicit SSL
27  * </DESC>
28  */
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include <curl/curl.h>
33 
34 /* This is a simple example showing how to send mail using libcurl's SMTP
35  * capabilities. It builds on the smtp-mail.c example to add authentication
36  * and, more importantly, transport security to protect the authentication
37  * details from being snooped.
38  *
39  * Note that this example requires libcurl 7.20.0 or above.
40  */
41 
42 #define FROM_MAIL     "<sender@example.com>"
43 #define TO_MAIL       "<recipient@example.com>"
44 #define CC_MAIL       "<info@example.com>"
45 
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   "Cc: " CC_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 
60 struct upload_status {
61   size_t bytes_read;
62 };
63 
payload_source(char * ptr,size_t size,size_t nmemb,void * userp)64 static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
65 {
66   struct upload_status *upload_ctx = (struct upload_status *)userp;
67   const char *data;
68   size_t room = size * nmemb;
69 
70   if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
71     return 0;
72   }
73 
74   data = &payload_text[upload_ctx->bytes_read];
75 
76   if(data) {
77     size_t len = strlen(data);
78     if(room < len)
79       len = room;
80     memcpy(ptr, data, len);
81     upload_ctx->bytes_read += len;
82 
83     return len;
84   }
85 
86   return 0;
87 }
88 
main(void)89 int main(void)
90 {
91   CURL *curl;
92   CURLcode res = CURLE_OK;
93   struct curl_slist *recipients = NULL;
94   struct upload_status upload_ctx = { 0 };
95 
96   curl = curl_easy_init();
97   if(curl) {
98     /* Set username and password */
99     curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
100     curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
101 
102     /* This is the URL for your mailserver. Note the use of smtps:// rather
103      * than smtp:// to request a SSL based connection. */
104     curl_easy_setopt(curl, CURLOPT_URL, "smtps://mainserver.example.net");
105 
106     /* If you want to connect to a site who is not using a certificate that is
107      * signed by one of the certs in the CA bundle you have, you can skip the
108      * verification of the server's certificate. This makes the connection
109      * A LOT LESS SECURE.
110      *
111      * If you have a CA cert for the server stored someplace else than in the
112      * default bundle, then the CURLOPT_CAPATH option might come handy for
113      * you. */
114 #ifdef SKIP_PEER_VERIFICATION
115     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
116 #endif
117 
118     /* If the site you are connecting to uses a different host name that what
119      * they have mentioned in their server certificate's commonName (or
120      * subjectAltName) fields, libcurl refuses to connect. You can skip this
121      * check, but it makes the connection insecure. */
122 #ifdef SKIP_HOSTNAME_VERIFICATION
123     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
124 #endif
125 
126     /* Note that this option is not strictly required, omitting it results in
127      * libcurl sending the MAIL FROM command with empty sender data. All
128      * autoresponses should have an empty reverse-path, and should be directed
129      * to the address in the reverse-path which triggered them. Otherwise,
130      * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more
131      * details.
132      */
133     curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_MAIL);
134 
135     /* Add two recipients, in this particular case they correspond to the
136      * To: and Cc: addressees in the header, but they could be any kind of
137      * recipient. */
138     recipients = curl_slist_append(recipients, TO_MAIL);
139     recipients = curl_slist_append(recipients, CC_MAIL);
140     curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
141 
142     /* We are using a callback function to specify the payload (the headers and
143      * body of the message). You could just use the CURLOPT_READDATA option to
144      * specify a FILE pointer to read from. */
145     curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
146     curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
147     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
148 
149     /* Since the traffic is encrypted, it is useful to turn on debug
150      * information within libcurl to see what is happening during the
151      * transfer */
152     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
153 
154     /* Send the message */
155     res = curl_easy_perform(curl);
156 
157     /* Check for errors */
158     if(res != CURLE_OK)
159       fprintf(stderr, "curl_easy_perform() failed: %s\n",
160               curl_easy_strerror(res));
161 
162     /* Free the list of recipients */
163     curl_slist_free_all(recipients);
164 
165     /* Always cleanup */
166     curl_easy_cleanup(curl);
167   }
168 
169   return (int)res;
170 }
171