1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
24 #include <curl/curl.h>
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.
31 #define FROM "<sender@example.org>"
32 #define TO "<addressee@example.net>"
33 #define CC "<info@example.org>"
35 static const char *payload_text[]={
36 "Date: Mon, 29 Nov 2010 21:54:29 +1100\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",
45 "It could be a lot of lines, could be MIME encoded, whatever.\n",
50 struct upload_status {
54 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
56 struct upload_status *upload_ctx = (struct upload_status *)userp;
59 if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
63 data = payload_text[upload_ctx->lines_read];
66 size_t len = strlen(data);
67 memcpy(ptr, data, len);
68 upload_ctx->lines_read ++;
79 struct curl_slist *recipients = NULL;
80 struct upload_status upload_ctx;
82 upload_ctx.lines_read = 0;
84 curl = curl_easy_init();
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");
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);
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.
111 curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
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");
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
124 recipients = curl_slist_append(recipients, TO);
125 recipients = curl_slist_append(recipients, CC);
126 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
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
132 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
133 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
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.
138 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
140 /* send the message (including headers) */
141 res = curl_easy_perform(curl);
142 /* Check for errors */
144 fprintf(stderr, "curl_easy_perform() failed: %s\n",
145 curl_easy_strerror(res));
147 /* free the list of recipients and clean up */
148 curl_slist_free_all(recipients);
149 curl_easy_cleanup(curl);