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 ***************************************************************************/
23 #include <curl/curl.h>
25 /* This is an example showing how to send mail using libcurl's SMTP
26 * capabilities. It builds on the smtp-mail.c example to demonstrate how to use
27 * libcurl's multi interface.
29 * Note that this example requires libcurl 7.20.0 or above.
32 #define FROM "<sender@example.com>"
33 #define TO "<recipient@example.com>"
34 #define CC "<info@example.com>"
36 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
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 multi 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++;
79 static struct timeval tvnow(void)
83 /* time() returns the value of time in seconds since the epoch */
84 now.tv_sec = (long)time(NULL);
90 static long tvdiff(struct timeval newer, struct timeval older)
92 return (newer.tv_sec - older.tv_sec) * 1000 +
93 (newer.tv_usec - older.tv_usec) / 1000;
100 int still_running = 1;
101 struct timeval mp_start;
102 struct curl_slist *recipients = NULL;
103 struct upload_status upload_ctx;
105 upload_ctx.lines_read = 0;
107 curl_global_init(CURL_GLOBAL_DEFAULT);
109 curl = curl_easy_init();
113 mcurl = curl_multi_init();
117 /* This is the URL for your mailserver */
118 curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
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 /* Tell the multi stack about our easy handle */
143 curl_multi_add_handle(mcurl, curl);
145 /* Record the start time which we can use later */
148 /* We start some action by calling perform right away */
149 curl_multi_perform(mcurl, &still_running);
151 while(still_running) {
152 struct timeval timeout;
158 CURLMcode mc; /* curl_multi_fdset() return code */
160 long curl_timeo = -1;
162 /* Initialise the file descriptors */
167 /* Set a suitable timeout to play around with */
171 curl_multi_timeout(mcurl, &curl_timeo);
172 if(curl_timeo >= 0) {
173 timeout.tv_sec = curl_timeo / 1000;
174 if(timeout.tv_sec > 1)
177 timeout.tv_usec = (curl_timeo % 1000) * 1000;
180 /* get file descriptors from the transfers */
181 mc = curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
185 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
189 /* On success the value of maxfd is guaranteed to be >= -1. We call
190 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
191 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
192 to sleep 100ms, which is the minimum suggested value in the
193 curl_multi_fdset() doc. */
200 /* Portable sleep for platforms other than Windows. */
201 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
202 rc = select(0, NULL, NULL, NULL, &wait);
206 /* Note that on some platforms 'timeout' may be modified by select().
207 If you need access to the original value save a copy beforehand. */
208 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
211 if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
213 "ABORTING: Since it seems that we would have run forever.\n");
218 case -1: /* select error */
220 case 0: /* timeout */
221 default: /* action */
222 curl_multi_perform(mcurl, &still_running);
227 /* Free the list of recipients */
228 curl_slist_free_all(recipients);
231 curl_multi_remove_handle(mcurl, curl);
232 curl_multi_cleanup(mcurl);
233 curl_easy_cleanup(curl);
234 curl_global_cleanup();