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 <stdio.h> 23#include <string.h> 24#include <curl/curl.h> 25 26/* This is a simple example showing how to send mail using libcurl's SMTP 27 * capabilities. It builds on the smtp-mail.c example to add authentication 28 * and, more importantly, transport security to protect the authentication 29 * details from being snooped. 30 * 31 * Note that this example requires libcurl 7.20.0 or above. 32 */ 33 34#define FROM "<sender@example.org>" 35#define TO "<addressee@example.net>" 36#define CC "<info@example.org>" 37 38static 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 SSL 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 53struct upload_status { 54 int lines_read; 55}; 56 57static 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 79int main(void) 80{ 81 CURL *curl; 82 CURLcode res = CURLE_OK; 83 struct curl_slist *recipients = NULL; 84 struct upload_status upload_ctx; 85 86 upload_ctx.lines_read = 0; 87 88 curl = curl_easy_init(); 89 if(curl) { 90 /* Set username and password */ 91 curl_easy_setopt(curl, CURLOPT_USERNAME, "user"); 92 curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret"); 93 94 /* This is the URL for your mailserver. Note the use of smtps:// rather 95 * than smtp:// to request a SSL based connection. */ 96 curl_easy_setopt(curl, CURLOPT_URL, "smtps://mainserver.example.net"); 97 98 /* If you want to connect to a site who isn't using a certificate that is 99 * signed by one of the certs in the CA bundle you have, you can skip the 100 * verification of the server's certificate. This makes the connection 101 * A LOT LESS SECURE. 102 * 103 * If you have a CA cert for the server stored someplace else than in the 104 * default bundle, then the CURLOPT_CAPATH option might come handy for 105 * you. */ 106#ifdef SKIP_PEER_VERIFICATION 107 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); 108#endif 109 110 /* If the site you're connecting to uses a different host name that what 111 * they have mentioned in their server certificate's commonName (or 112 * subjectAltName) fields, libcurl will refuse to connect. You can skip 113 * this check, but this will make the connection less secure. */ 114#ifdef SKIP_HOSTNAME_VERIFICATION 115 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); 116#endif 117 118 /* Note that this option isn't strictly required, omitting it will result in 119 * libcurl sending the MAIL FROM command with empty sender data. All 120 * autoresponses should have an empty reverse-path, and should be directed 121 * to the address in the reverse-path which triggered them. Otherwise, they 122 * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details. 123 */ 124 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM); 125 126 /* Add two recipients, in this particular case they correspond to the 127 * To: and Cc: addressees in the header, but they could be any kind of 128 * recipient. */ 129 recipients = curl_slist_append(recipients, TO); 130 recipients = curl_slist_append(recipients, CC); 131 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); 132 133 /* We're using a callback function to specify the payload (the headers and 134 * body of the message). You could just use the CURLOPT_READDATA option to 135 * specify a FILE pointer to read from. */ 136 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); 137 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); 138 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 139 140 /* Since the traffic will be encrypted, it is very useful to turn on debug 141 * information within libcurl to see what is happening during the 142 * transfer */ 143 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 144 145 /* Send the message */ 146 res = curl_easy_perform(curl); 147 148 /* Check for errors */ 149 if(res != CURLE_OK) 150 fprintf(stderr, "curl_easy_perform() failed: %s\n", 151 curl_easy_strerror(res)); 152 153 /* Free the list of recipients */ 154 curl_slist_free_all(recipients); 155 156 /* Always cleanup */ 157 curl_easy_cleanup(curl); 158 } 159 160 return (int)res; 161} 162