Home | History | Annotate | Download | only in examples
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2016, 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 https://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 
     23 /* <DESC>
     24  * SMTP example showing how to send e-mails
     25  * </DESC>
     26  */
     27 
     28 #include <stdio.h>
     29 #include <string.h>
     30 #include <curl/curl.h>
     31 
     32 /* This is a simple example showing how to send mail using libcurl's SMTP
     33  * capabilities. For an example of using the multi interface please see
     34  * smtp-multi.c.
     35  *
     36  * Note that this example requires libcurl 7.20.0 or above.
     37  */
     38 
     39 #define FROM    "<sender (at) example.org>"
     40 #define TO      "<addressee (at) example.net>"
     41 #define CC      "<info (at) example.org>"
     42 
     43 static const char *payload_text[] = {
     44   "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
     45   "To: " TO "\r\n",
     46   "From: " FROM "(Example User)\r\n",
     47   "Cc: " CC "(Another example User)\r\n",
     48   "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
     49   "rfcpedant.example.org>\r\n",
     50   "Subject: SMTP example message\r\n",
     51   "\r\n", /* empty line to divide headers from body, see RFC5322 */
     52   "The body of the message starts here.\r\n",
     53   "\r\n",
     54   "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
     55   "Check RFC5322.\r\n",
     56   NULL
     57 };
     58 
     59 struct upload_status {
     60   int lines_read;
     61 };
     62 
     63 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
     64 {
     65   struct upload_status *upload_ctx = (struct upload_status *)userp;
     66   const char *data;
     67 
     68   if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
     69     return 0;
     70   }
     71 
     72   data = payload_text[upload_ctx->lines_read];
     73 
     74   if(data) {
     75     size_t len = strlen(data);
     76     memcpy(ptr, data, len);
     77     upload_ctx->lines_read++;
     78 
     79     return len;
     80   }
     81 
     82   return 0;
     83 }
     84 
     85 int main(void)
     86 {
     87   CURL *curl;
     88   CURLcode res = CURLE_OK;
     89   struct curl_slist *recipients = NULL;
     90   struct upload_status upload_ctx;
     91 
     92   upload_ctx.lines_read = 0;
     93 
     94   curl = curl_easy_init();
     95   if(curl) {
     96     /* This is the URL for your mailserver */
     97     curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
     98 
     99     /* Note that this option isn't strictly required, omitting it will result
    100      * in libcurl sending the MAIL FROM command with empty sender data. All
    101      * autoresponses should have an empty reverse-path, and should be directed
    102      * to the address in the reverse-path which triggered them. Otherwise,
    103      * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more
    104      * details.
    105      */
    106     curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
    107 
    108     /* Add two recipients, in this particular case they correspond to the
    109      * To: and Cc: addressees in the header, but they could be any kind of
    110      * recipient. */
    111     recipients = curl_slist_append(recipients, TO);
    112     recipients = curl_slist_append(recipients, CC);
    113     curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
    114 
    115     /* We're using a callback function to specify the payload (the headers and
    116      * body of the message). You could just use the CURLOPT_READDATA option to
    117      * specify a FILE pointer to read from. */
    118     curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
    119     curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
    120     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
    121 
    122     /* Send the message */
    123     res = curl_easy_perform(curl);
    124 
    125     /* Check for errors */
    126     if(res != CURLE_OK)
    127       fprintf(stderr, "curl_easy_perform() failed: %s\n",
    128               curl_easy_strerror(res));
    129 
    130     /* Free the list of recipients */
    131     curl_slist_free_all(recipients);
    132 
    133     /* curl won't send the QUIT command until you call cleanup, so you should
    134      * be able to re-use this connection for additional messages (setting
    135      * CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and calling
    136      * curl_easy_perform() again. It may not be a good idea to keep the
    137      * connection open for a very long time though (more than a few minutes
    138      * may result in the server timing out the connection), and you do want to
    139      * clean up in the end.
    140      */
    141     curl_easy_cleanup(curl);
    142   }
    143 
    144   return (int)res;
    145 }
    146