smtp-ssl.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 /* <DESC>
00024  * SMTP example using SSL
00025  * </DESC>
00026  */
00027 
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <curl/curl.h>
00031 
00032 /* This is a simple example showing how to send mail using libcurl's SMTP
00033  * capabilities. It builds on the smtp-mail.c example to add authentication
00034  * and, more importantly, transport security to protect the authentication
00035  * details from being snooped.
00036  *
00037  * Note that this example requires libcurl 7.20.0 or above.
00038  */
00039 
00040 #define FROM    "<sender@example.org>"
00041 #define TO      "<addressee@example.net>"
00042 #define CC      "<info@example.org>"
00043 
00044 static const char *payload_text[] = {
00045   "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
00046   "To: " TO "\r\n",
00047   "From: " FROM "(Example User)\r\n",
00048   "Cc: " CC "(Another example User)\r\n",
00049   "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
00050   "rfcpedant.example.org>\r\n",
00051   "Subject: SMTP SSL example message\r\n",
00052   "\r\n", /* empty line to divide headers from body, see RFC5322 */
00053   "The body of the message starts here.\r\n",
00054   "\r\n",
00055   "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
00056   "Check RFC5322.\r\n",
00057   NULL
00058 };
00059 
00060 struct upload_status {
00061   int lines_read;
00062 };
00063 
00064 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
00065 {
00066   struct upload_status *upload_ctx = (struct upload_status *)userp;
00067   const char *data;
00068 
00069   if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
00070     return 0;
00071   }
00072 
00073   data = payload_text[upload_ctx->lines_read];
00074 
00075   if(data) {
00076     size_t len = strlen(data);
00077     memcpy(ptr, data, len);
00078     upload_ctx->lines_read++;
00079 
00080     return len;
00081   }
00082 
00083   return 0;
00084 }
00085 
00086 int main(void)
00087 {
00088   CURL *curl;
00089   CURLcode res = CURLE_OK;
00090   struct curl_slist *recipients = NULL;
00091   struct upload_status upload_ctx;
00092 
00093   upload_ctx.lines_read = 0;
00094 
00095   curl = curl_easy_init();
00096   if(curl) {
00097     /* Set username and password */
00098     curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
00099     curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
00100 
00101     /* This is the URL for your mailserver. Note the use of smtps:// rather
00102      * than smtp:// to request a SSL based connection. */
00103     curl_easy_setopt(curl, CURLOPT_URL, "smtps://mainserver.example.net");
00104 
00105     /* If you want to connect to a site who isn't using a certificate that is
00106      * signed by one of the certs in the CA bundle you have, you can skip the
00107      * verification of the server's certificate. This makes the connection
00108      * A LOT LESS SECURE.
00109      *
00110      * If you have a CA cert for the server stored someplace else than in the
00111      * default bundle, then the CURLOPT_CAPATH option might come handy for
00112      * you. */
00113 #ifdef SKIP_PEER_VERIFICATION
00114     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
00115 #endif
00116 
00117     /* If the site you're connecting to uses a different host name that what
00118      * they have mentioned in their server certificate's commonName (or
00119      * subjectAltName) fields, libcurl will refuse to connect. You can skip
00120      * this check, but this will make the connection less secure. */
00121 #ifdef SKIP_HOSTNAME_VERIFICATION
00122     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
00123 #endif
00124 
00125     /* Note that this option isn't strictly required, omitting it will result
00126      * in libcurl sending the MAIL FROM command with empty sender data. All
00127      * autoresponses should have an empty reverse-path, and should be directed
00128      * to the address in the reverse-path which triggered them. Otherwise,
00129      * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more
00130      * details.
00131      */
00132     curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
00133 
00134     /* Add two recipients, in this particular case they correspond to the
00135      * To: and Cc: addressees in the header, but they could be any kind of
00136      * recipient. */
00137     recipients = curl_slist_append(recipients, TO);
00138     recipients = curl_slist_append(recipients, CC);
00139     curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
00140 
00141     /* We're using a callback function to specify the payload (the headers and
00142      * body of the message). You could just use the CURLOPT_READDATA option to
00143      * specify a FILE pointer to read from. */
00144     curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
00145     curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
00146     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00147 
00148     /* Since the traffic will be encrypted, it is very useful to turn on debug
00149      * information within libcurl to see what is happening during the
00150      * transfer */
00151     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00152 
00153     /* Send the message */
00154     res = curl_easy_perform(curl);
00155 
00156     /* Check for errors */
00157     if(res != CURLE_OK)
00158       fprintf(stderr, "curl_easy_perform() failed: %s\n",
00159               curl_easy_strerror(res));
00160 
00161     /* Free the list of recipients */
00162     curl_slist_free_all(recipients);
00163 
00164     /* Always cleanup */
00165     curl_easy_cleanup(curl);
00166   }
00167 
00168   return (int)res;
00169 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06