1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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 ***************************************************************************/
29 * This is the list of basic details you need to tweak to get things right.
31 #define USERNAME "user@example.com"
32 #define PASSWORD "123qwerty"
33 #define RECIPIENT "<1507-recipient@example.com>"
34 #define MAILFROM "<1507-realuser@example.com>"
36 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
38 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
44 return CURL_READFUNC_ABORT;
47 static struct timeval tvnow(void)
50 ** time() returns the value of time in seconds since the Epoch.
53 now.tv_sec = (long)time(NULL);
58 static long tvdiff(struct timeval newer, struct timeval older)
60 return (newer.tv_sec-older.tv_sec)*1000+
61 (newer.tv_usec-older.tv_usec)/1000;
68 int still_running = 1;
69 struct timeval mp_start;
70 struct curl_slist* rcpt_list = NULL;
72 curl_global_init(CURL_GLOBAL_DEFAULT);
74 curl = curl_easy_init();
78 mcurl = curl_multi_init();
82 rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);
83 /* more addresses can be added here
84 rcpt_list = curl_slist_append(rcpt_list, "<others@example.com>");
87 curl_easy_setopt(curl, CURLOPT_URL, URL);
89 curl_easy_setopt(curl, CURLOPT_USERNAME, USERNAME);
90 curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD);
92 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
93 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, MAILFROM);
94 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt_list);
95 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
96 curl_multi_add_handle(mcurl, curl);
100 /* we start some action by calling perform right away */
101 curl_multi_perform(mcurl, &still_running);
103 while(still_running) {
104 struct timeval timeout;
105 int rc; /* select() return code */
112 long curl_timeo = -1;
118 /* set a suitable timeout to play around with */
122 curl_multi_timeout(mcurl, &curl_timeo);
123 if(curl_timeo >= 0) {
124 timeout.tv_sec = curl_timeo / 1000;
125 if(timeout.tv_sec > 1)
128 timeout.tv_usec = (curl_timeo % 1000) * 1000;
131 /* get file descriptors from the transfers */
132 curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
134 /* In a real-world program you OF COURSE check the return code of the
135 function calls. On success, the value of maxfd is guaranteed to be
136 greater or equal than -1. We call select(maxfd + 1, ...), specially in
137 case of (maxfd == -1), we call select(0, ...), which is basically equal
140 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
142 if (tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
143 fprintf(stderr, "ABORTING TEST, since it seems "
144 "that it would have run forever.\n");
152 case 0: /* timeout */
153 default: /* action */
154 curl_multi_perform(mcurl, &still_running);
159 curl_slist_free_all(rcpt_list);
160 curl_multi_remove_handle(mcurl, curl);
161 curl_multi_cleanup(mcurl);
162 curl_easy_cleanup(curl);
163 curl_global_cleanup();