3635c103f2608a457f7271d3b93e98806732bb7a
[platform/upstream/curl.git] / docs / examples / smtp-tls.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, 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 http://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 #include <stdio.h>
23 #include <string.h>
24 #include <curl/curl.h>
25
26 /* This is a simple example showing how to send mail using libcurl's SMTP
27  * capabilities. It builds on the simplesmtp.c example, adding some
28  * authentication and transport security.
29  */
30
31 #define FROM    "<sender@example.org>"
32 #define TO      "<addressee@example.net>"
33 #define CC      "<info@example.org>"
34
35 static const char *payload_text[]={
36   "Date: Mon, 29 Nov 2010 21:54:29 +1100\n",
37   "To: " TO "\n",
38   "From: " FROM "(Example User)\n",
39   "Cc: " CC "(Another example User)\n",
40   "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\n",
41   "Subject: SMTP TLS example message\n",
42   "\n", /* empty line to divide headers from body, see RFC5322 */
43   "The body of the message starts here.\n",
44   "\n",
45   "It could be a lot of lines, could be MIME encoded, whatever.\n",
46   "Check RFC5322.\n",
47   NULL
48 };
49
50 struct upload_status {
51   int lines_read;
52 };
53
54 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
55 {
56   struct upload_status *upload_ctx = (struct upload_status *)userp;
57   const char *data;
58
59   if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
60     return 0;
61   }
62
63   data = payload_text[upload_ctx->lines_read];
64
65   if (data) {
66     size_t len = strlen(data);
67     memcpy(ptr, data, len);
68     upload_ctx->lines_read ++;
69     return len;
70   }
71   return 0;
72 }
73
74
75 int main(void)
76 {
77   CURL *curl;
78   CURLcode res;
79   struct curl_slist *recipients = NULL;
80   struct upload_status upload_ctx;
81
82   upload_ctx.lines_read = 0;
83
84   curl = curl_easy_init();
85   if (curl) {
86     /* This is the URL for your mailserver. Note the use of port 587 here,
87      * instead of the normal SMTP port (25). Port 587 is commonly used for
88      * secure mail submission (see RFC4403), but you should use whatever
89      * matches your server configuration. */
90     curl_easy_setopt(curl, CURLOPT_URL, "smtp://mainserver.example.net:587");
91
92     /* In this example, we'll start with a plain text connection, and upgrade
93      * to Transport Layer Security (TLS) using the STARTTLS command. Be careful
94      * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer
95      * will continue anyway - see the security discussion in the libcurl
96      * tutorial for more details. */
97     curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
98
99     /* If your server doesn't have a valid certificate, then you can disable
100      * part of the Transport Layer Security protection by setting the
101      * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
102      *   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
103      *   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
104      * That is, in general, a bad idea. It is still better than sending your
105      * authentication details in plain text though.
106      * Instead, you should get the issuer certificate (or the host certificate
107      * if the certificate is self-signed) and add it to the set of certificates
108      * that are known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See
109      * docs/SSLCERTS for more information.
110      */
111     curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
112
113     /* A common reason for requiring transport security is to protect
114      * authentication details (user names and passwords) from being "snooped"
115      * on the network. Here is how the user name and password are provided: */
116     curl_easy_setopt(curl, CURLOPT_USERNAME, "user@example.net");
117     curl_easy_setopt(curl, CURLOPT_PASSWORD, "P@ssw0rd");
118
119     /* value for envelope reverse-path */
120     curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
121     /* Add two recipients, in this particular case they correspond to the
122      * To: and Cc: addressees in the header, but they could be any kind of
123      * recipient. */
124     recipients = curl_slist_append(recipients, TO);
125     recipients = curl_slist_append(recipients, CC);
126     curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
127
128     /* In this case, we're using a callback function to specify the data. You
129      * could just use the CURLOPT_READDATA option to specify a FILE pointer to
130      * read from.
131      */
132     curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
133     curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
134
135     /* Since the traffic will be encrypted, it is very useful to turn on debug
136      * information within libcurl to see what is happening during the transfer.
137      */
138     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
139
140     /* send the message (including headers) */
141     res = curl_easy_perform(curl);
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 and clean up */
148     curl_slist_free_all(recipients);
149     curl_easy_cleanup(curl);
150   }
151   return 0;
152 }