1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, 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 ***************************************************************************/
22
23 /* <DESC>
24 * SMTP example using TLS
25 * </DESC>
26 */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <curl/curl.h>
31
32 /* This is a simple example showing how to send mail using libcurl's SMTP
33 * capabilities. It builds on the smtp-mail.c example to add authentication
34 * and, more importantly, transport security to protect the authentication
35 * details from being snooped.
36 *
37 * Note that this example requires libcurl 7.20.0 or above.
38 */
39
40 #define FROM_MAIL "<sender@example.com>"
41 #define TO_MAIL "<recipient@example.com>"
42 #define CC_MAIL "<info@example.com>"
43
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 "Cc: " CC_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
58 struct upload_status {
59 size_t bytes_read;
60 };
61
payload_source(char * ptr,size_t size,size_t nmemb,void * userp)62 static size_t payload_source(char *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 size_t room = size * nmemb;
67
68 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
69 return 0;
70 }
71
72 data = &payload_text[upload_ctx->bytes_read];
73
74 if(data) {
75 size_t len = strlen(data);
76 if(room < len)
77 len = room;
78 memcpy(ptr, data, len);
79 upload_ctx->bytes_read += len;
80
81 return len;
82 }
83
84 return 0;
85 }
86
main(void)87 int main(void)
88 {
89 CURL *curl;
90 CURLcode res = CURLE_OK;
91 struct curl_slist *recipients = NULL;
92 struct upload_status upload_ctx = { 0 };
93
94 curl = curl_easy_init();
95 if(curl) {
96 /* Set username and password */
97 curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
98 curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
99
100 /* This is the URL for your mailserver. Note the use of port 587 here,
101 * instead of the normal SMTP port (25). Port 587 is commonly used for
102 * secure mail submission (see RFC4403), but you should use whatever
103 * matches your server configuration. */
104 curl_easy_setopt(curl, CURLOPT_URL, "smtp://mainserver.example.net:587");
105
106 /* In this example, we'll start with a plain text connection, and upgrade
107 * to Transport Layer Security (TLS) using the STARTTLS command. Be careful
108 * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer
109 * will continue anyway - see the security discussion in the libcurl
110 * tutorial for more details. */
111 curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
112
113 /* If your server doesn't have a valid certificate, then you can disable
114 * part of the Transport Layer Security protection by setting the
115 * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
116 * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
117 * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
118 * That is, in general, a bad idea. It is still better than sending your
119 * authentication details in plain text though. Instead, you should get
120 * the issuer certificate (or the host certificate if the certificate is
121 * self-signed) and add it to the set of certificates that are known to
122 * libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See docs/SSLCERTS
123 * for more information. */
124 curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
125
126 /* Note that this option isn't strictly required, omitting it will result
127 * in 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're 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 will be encrypted, it is very useful to turn on debug
150 * information within libcurl to see what is happening during the transfer.
151 */
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