Home | History | Annotate | Download | only in examples
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel (at) 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 (at) example.com>"
     33 #define TO       "<recipient (at) example.com>"
     34 #define CC       "<info (at) 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 (at) 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     CURLMcode mc; /* curl_multi_fdset() return code */
    159 
    160     long curl_timeo = -1;
    161 
    162     /* Initialise the file descriptors */
    163     FD_ZERO(&fdread);
    164     FD_ZERO(&fdwrite);
    165     FD_ZERO(&fdexcep);
    166 
    167     /* Set a suitable timeout to play around with */
    168     timeout.tv_sec = 1;
    169     timeout.tv_usec = 0;
    170 
    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)
    175         timeout.tv_sec = 1;
    176       else
    177         timeout.tv_usec = (curl_timeo % 1000) * 1000;
    178     }
    179 
    180     /* get file descriptors from the transfers */
    181     mc = curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
    182 
    183     if(mc != CURLM_OK)
    184     {
    185       fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
    186       break;
    187     }
    188 
    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. */
    194 
    195     if(maxfd == -1) {
    196 #ifdef _WIN32
    197       Sleep(100);
    198       rc = 0;
    199 #else
    200       /* Portable sleep for platforms other than Windows. */
    201       struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
    202       rc = select(0, NULL, NULL, NULL, &wait);
    203 #endif
    204     }
    205     else {
    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);
    209     }
    210 
    211     if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
    212       fprintf(stderr,
    213               "ABORTING: Since it seems that we would have run forever.\n");
    214       break;
    215     }
    216 
    217     switch(rc) {
    218     case -1:  /* select error */
    219       break;
    220     case 0:   /* timeout */
    221     default:  /* action */
    222       curl_multi_perform(mcurl, &still_running);
    223       break;
    224     }
    225   }
    226 
    227   /* Free the list of recipients */
    228   curl_slist_free_all(recipients);
    229 
    230   /* Always cleanup */
    231   curl_multi_remove_handle(mcurl, curl);
    232   curl_multi_cleanup(mcurl);
    233   curl_easy_cleanup(curl);
    234   curl_global_cleanup();
    235 
    236   return 0;
    237 }
    238