1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 smtp-mail.c example to add authentication
28 * and, more importantly, transport security to protect the authentication
29 * details from being snooped.
31 * Note that this example requires libcurl 7.20.0 or above.
34 #define FROM "<sender@example.org>"
35 #define TO "<addressee@example.net>"
36 #define CC "<info@example.org>"
38 static const char *payload_text[] = {
39 "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
41 "From: " FROM "(Example User)\r\n",
42 "Cc: " CC "(Another example User)\r\n",
43 "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n",
44 "Subject: SMTP TLS example message\r\n",
45 "\r\n", /* empty line to divide headers from body, see RFC5322 */
46 "The body of the message starts here.\r\n",
48 "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
53 struct upload_status {
57 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
59 struct upload_status *upload_ctx = (struct upload_status *)userp;
62 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
66 data = payload_text[upload_ctx->lines_read];
69 size_t len = strlen(data);
70 memcpy(ptr, data, len);
71 upload_ctx->lines_read++;
82 CURLcode res = CURLE_OK;
83 struct curl_slist *recipients = NULL;
84 struct upload_status upload_ctx;
86 upload_ctx.lines_read = 0;
88 curl = curl_easy_init();
90 /* Set username and password */
91 curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
92 curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
94 /* This is the URL for your mailserver. Note the use of port 587 here,
95 * instead of the normal SMTP port (25). Port 587 is commonly used for
96 * secure mail submission (see RFC4403), but you should use whatever
97 * matches your server configuration. */
98 curl_easy_setopt(curl, CURLOPT_URL, "smtp://mainserver.example.net:587");
100 /* In this example, we'll start with a plain text connection, and upgrade
101 * to Transport Layer Security (TLS) using the STARTTLS command. Be careful
102 * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer
103 * will continue anyway - see the security discussion in the libcurl
104 * tutorial for more details. */
105 curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
107 /* If your server doesn't have a valid certificate, then you can disable
108 * part of the Transport Layer Security protection by setting the
109 * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
110 * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
111 * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
112 * That is, in general, a bad idea. It is still better than sending your
113 * authentication details in plain text though.
114 * Instead, you should get the issuer certificate (or the host certificate
115 * if the certificate is self-signed) and add it to the set of certificates
116 * that are known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See
117 * docs/SSLCERTS for more information. */
118 curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
120 /* Note that this option isn't strictly required, omitting it will result in
121 * libcurl sending the MAIL FROM command with empty sender data. All
122 * autoresponses should have an empty reverse-path, and should be directed
123 * to the address in the reverse-path which triggered them. Otherwise, they
124 * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details.
126 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
128 /* Add two recipients, in this particular case they correspond to the
129 * To: and Cc: addressees in the header, but they could be any kind of
131 recipients = curl_slist_append(recipients, TO);
132 recipients = curl_slist_append(recipients, CC);
133 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
135 /* We're using a callback function to specify the payload (the headers and
136 * body of the message). You could just use the CURLOPT_READDATA option to
137 * specify a FILE pointer to read from. */
138 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
139 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
140 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
142 /* Since the traffic will be encrypted, it is very useful to turn on debug
143 * information within libcurl to see what is happening during the transfer.
145 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
147 /* Send the message */
148 res = curl_easy_perform(curl);
150 /* Check for errors */
152 fprintf(stderr, "curl_easy_perform() failed: %s\n",
153 curl_easy_strerror(res));
155 /* Free the list of recipients */
156 curl_slist_free_all(recipients);
159 curl_easy_cleanup(curl);