smtp-mail.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 showing how to send e-mails
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. For an example of using the multi interface please see
00034  * smtp-multi.c.
00035  *
00036  * Note that this example requires libcurl 7.20.0 or above.
00037  */
00038 
00039 #define FROM    "<sender@example.org>"
00040 #define TO      "<addressee@example.net>"
00041 #define CC      "<info@example.org>"
00042 
00043 static const char *payload_text[] = {
00044   "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
00045   "To: " TO "\r\n",
00046   "From: " FROM "(Example User)\r\n",
00047   "Cc: " CC "(Another example User)\r\n",
00048   "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
00049   "rfcpedant.example.org>\r\n",
00050   "Subject: SMTP example message\r\n",
00051   "\r\n", /* empty line to divide headers from body, see RFC5322 */
00052   "The body of the message starts here.\r\n",
00053   "\r\n",
00054   "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
00055   "Check RFC5322.\r\n",
00056   NULL
00057 };
00058 
00059 struct upload_status {
00060   int lines_read;
00061 };
00062 
00063 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
00064 {
00065   struct upload_status *upload_ctx = (struct upload_status *)userp;
00066   const char *data;
00067 
00068   if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
00069     return 0;
00070   }
00071 
00072   data = payload_text[upload_ctx->lines_read];
00073 
00074   if(data) {
00075     size_t len = strlen(data);
00076     memcpy(ptr, data, len);
00077     upload_ctx->lines_read++;
00078 
00079     return len;
00080   }
00081 
00082   return 0;
00083 }
00084 
00085 int main(void)
00086 {
00087   CURL *curl;
00088   CURLcode res = CURLE_OK;
00089   struct curl_slist *recipients = NULL;
00090   struct upload_status upload_ctx;
00091 
00092   upload_ctx.lines_read = 0;
00093 
00094   curl = curl_easy_init();
00095   if(curl) {
00096     /* This is the URL for your mailserver */
00097     curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
00098 
00099     /* Note that this option isn't strictly required, omitting it will result
00100      * in libcurl sending the MAIL FROM command with empty sender data. All
00101      * autoresponses should have an empty reverse-path, and should be directed
00102      * to the address in the reverse-path which triggered them. Otherwise,
00103      * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more
00104      * details.
00105      */
00106     curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
00107 
00108     /* Add two recipients, in this particular case they correspond to the
00109      * To: and Cc: addressees in the header, but they could be any kind of
00110      * recipient. */
00111     recipients = curl_slist_append(recipients, TO);
00112     recipients = curl_slist_append(recipients, CC);
00113     curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
00114 
00115     /* We're using a callback function to specify the payload (the headers and
00116      * body of the message). You could just use the CURLOPT_READDATA option to
00117      * specify a FILE pointer to read from. */
00118     curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
00119     curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
00120     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00121 
00122     /* Send the message */
00123     res = curl_easy_perform(curl);
00124 
00125     /* Check for errors */
00126     if(res != CURLE_OK)
00127       fprintf(stderr, "curl_easy_perform() failed: %s\n",
00128               curl_easy_strerror(res));
00129 
00130     /* Free the list of recipients */
00131     curl_slist_free_all(recipients);
00132 
00133     /* curl won't send the QUIT command until you call cleanup, so you should
00134      * be able to re-use this connection for additional messages (setting
00135      * CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and calling
00136      * curl_easy_perform() again. It may not be a good idea to keep the
00137      * connection open for a very long time though (more than a few minutes
00138      * may result in the server timing out the connection), and you do want to
00139      * clean up in the end.
00140      */
00141     curl_easy_cleanup(curl);
00142   }
00143 
00144   return (int)res;
00145 }


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