Revert "Update to 7.40.1"
[platform/upstream/curl.git] / docs / examples / smtp-multi.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2014, 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 <string.h>
23 #include <curl/curl.h>
24
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.
28  *
29  * Note that this example requires libcurl 7.20.0 or above.
30  */
31
32 #define FROM     "<sender@example.com>"
33 #define TO       "<recipient@example.com>"
34 #define CC       "<info@example.com>"
35
36 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
37
38 static const char *payload_text[] = {
39   "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
40   "To: " TO "\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",
47   "\r\n",
48   "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
49   "Check RFC5322.\r\n",
50   NULL
51 };
52
53 struct upload_status {
54   int lines_read;
55 };
56
57 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
58 {
59   struct upload_status *upload_ctx = (struct upload_status *)userp;
60   const char *data;
61
62   if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
63     return 0;
64   }
65
66   data = payload_text[upload_ctx->lines_read];
67
68   if(data) {
69     size_t len = strlen(data);
70     memcpy(ptr, data, len);
71     upload_ctx->lines_read++;
72
73     return len;
74   }
75
76   return 0;
77 }
78
79 static struct timeval tvnow(void)
80 {
81   struct timeval now;
82
83   /* time() returns the value of time in seconds since the epoch */
84   now.tv_sec = (long)time(NULL);
85   now.tv_usec = 0;
86
87   return now;
88 }
89
90 static long tvdiff(struct timeval newer, struct timeval older)
91 {
92   return (newer.tv_sec - older.tv_sec) * 1000 +
93     (newer.tv_usec - older.tv_usec) / 1000;
94 }
95
96 int main(void)
97 {
98   CURL *curl;
99   CURLM *mcurl;
100   int still_running = 1;
101   struct timeval mp_start;
102   struct curl_slist *recipients = NULL;
103   struct upload_status upload_ctx;
104
105   upload_ctx.lines_read = 0;
106
107   curl_global_init(CURL_GLOBAL_DEFAULT);
108
109   curl = curl_easy_init();
110   if(!curl)
111     return 1;
112
113   mcurl = curl_multi_init();
114   if(!mcurl)
115     return 2;
116
117   /* This is the URL for your mailserver */
118   curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
119
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.
125    */
126   curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
127
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
130    * recipient. */
131   recipients = curl_slist_append(recipients, TO);
132   recipients = curl_slist_append(recipients, CC);
133   curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
134
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);
141
142   /* Tell the multi stack about our easy handle */
143   curl_multi_add_handle(mcurl, curl);
144
145   /* Record the start time which we can use later */
146   mp_start = tvnow();
147
148   /* We start some action by calling perform right away */
149   curl_multi_perform(mcurl, &still_running);
150
151   while(still_running) {
152     struct timeval timeout;
153     fd_set fdread;
154     fd_set fdwrite;
155     fd_set fdexcep;
156     int maxfd = -1;
157     int rc;
158
159     long curl_timeo = -1;
160
161     /* Initialise the file descriptors */
162     FD_ZERO(&fdread);
163     FD_ZERO(&fdwrite);
164     FD_ZERO(&fdexcep);
165
166     /* Set a suitable timeout to play around with */
167     timeout.tv_sec = 1;
168     timeout.tv_usec = 0;
169
170     curl_multi_timeout(mcurl, &curl_timeo);
171     if(curl_timeo >= 0) {
172       timeout.tv_sec = curl_timeo / 1000;
173       if(timeout.tv_sec > 1)
174         timeout.tv_sec = 1;
175       else
176         timeout.tv_usec = (curl_timeo % 1000) * 1000;
177     }
178
179     /* Get file descriptors from the transfers */
180     curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
181
182     /* In a real-world program you OF COURSE check the return code of the
183        function calls.  On success, the value of maxfd is guaranteed to be
184        greater or equal than -1.  We call select(maxfd + 1, ...), specially in
185        case of (maxfd == -1), we call select(0, ...), which is basically equal
186        to sleep. */
187     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
188
189     if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
190       fprintf(stderr,
191               "ABORTING: Since it seems that we would have run forever.\n");
192       break;
193     }
194
195     switch(rc) {
196     case -1:  /* select error */
197       break;
198     case 0:   /* timeout */
199     default:  /* action */
200       curl_multi_perform(mcurl, &still_running);
201       break;
202     }
203   }
204
205   /* Free the list of recipients */
206   curl_slist_free_all(recipients);
207
208   /* Always cleanup */
209   curl_multi_remove_handle(mcurl, curl);
210   curl_multi_cleanup(mcurl);
211   curl_easy_cleanup(curl);
212   curl_global_cleanup();
213
214   return 0;
215 }