$treeview $search $mathjax $extrastylesheet
artdaq_mfextensions
v1_03_03a
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "curl_send_message.h" 00002 #include <curl/curl.h> 00003 #include <time.h> 00004 00005 static size_t payload_source(void* ptr, size_t size, size_t nmemb, void* userp) { 00006 struct upload_status* upload_ctx = (struct upload_status*)userp; 00007 size_t rdsize = size * nmemb; 00008 00009 if ((size == 0) || (nmemb == 0) || (rdsize < 1) || (upload_ctx->pos >= upload_ctx->size)) { 00010 return 0; 00011 } 00012 00013 if (rdsize + upload_ctx->pos > upload_ctx->size) { 00014 rdsize = upload_ctx->size - upload_ctx->pos; 00015 } 00016 00017 memcpy(ptr, &upload_ctx->payload[upload_ctx->pos], rdsize); 00018 upload_ctx->pos += rdsize; 00019 00020 return rdsize; 00021 } 00022 00023 void send_message(const char* dest, const char* to[], size_t to_size, const char* from, const char* payload, 00024 size_t payload_size) { 00025 CURL* curl; 00026 CURLcode res; 00027 struct curl_slist* recipients = NULL; 00028 struct upload_status upload_ctx; 00029 00030 upload_ctx.pos = 0; 00031 upload_ctx.size = payload_size; 00032 upload_ctx.payload = payload; 00033 00034 curl = curl_easy_init(); 00035 if (curl) { 00036 /* This is the URL for your mailserver */ 00037 curl_easy_setopt(curl, CURLOPT_URL, dest); 00038 00039 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from); 00040 00041 /* Note that the CURLOPT_MAIL_RCPT takes a list, not a char array. */ 00042 00043 for (size_t ii = 0; ii < to_size; ++ii) { 00044 recipients = curl_slist_append(recipients, to[ii]); 00045 } 00046 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); 00047 00048 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); 00049 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); 00050 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 00051 00052 /* send the message (including headers) */ 00053 res = curl_easy_perform(curl); 00054 00055 /* Check for errors */ 00056 if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); 00057 00058 /* free the list of recipients */ 00059 curl_slist_free_all(recipients); 00060 00061 curl_easy_cleanup(curl); 00062 } 00063 } 00064 00065 void send_message_ssl(const char* dest, const char* to[], size_t to_size, const char* from, const char* payload, 00066 size_t payload_size, const char* username, const char* pw, int disableVerify) { 00067 CURL* curl; 00068 CURLcode res = CURLE_OK; 00069 struct curl_slist* recipients = NULL; 00070 struct upload_status upload_ctx; 00071 00072 upload_ctx.pos = 0; 00073 upload_ctx.size = payload_size; 00074 upload_ctx.payload = payload; 00075 00076 curl = curl_easy_init(); 00077 if (curl) { 00078 /* Set username and password */ 00079 curl_easy_setopt(curl, CURLOPT_USERNAME, username); 00080 curl_easy_setopt(curl, CURLOPT_PASSWORD, pw); 00081 00082 /* This is the URL for your mailserver. Note the use of port 587 here, 00083 * instead of the normal SMTP port (25). Port 587 is commonly used for 00084 * secure mail submission (see RFC4403), but you should use whatever 00085 * matches your server configuration. */ 00086 curl_easy_setopt(curl, CURLOPT_URL, dest); 00087 00088 /* In this example, we'll start with a plain text connection, and upgrade 00089 * to Transport Layer Security (TLS) using the STARTTLS command. Be careful 00090 * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer 00091 * will continue anyway - see the security discussion in the libcurl 00092 * tutorial for more details. */ 00093 curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); 00094 00095 if (disableVerify) { 00096 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); 00097 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); 00098 } 00099 00100 /* Note that this option isn't strictly required, omitting it will result 00101 * in libcurl sending the MAIL FROM command with empty sender data. All 00102 * autoresponses should have an empty reverse-path, and should be directed 00103 * to the address in the reverse-path which triggered them. Otherwise, 00104 * they could cause an endless loop. See RFC 5321 Section 4.5.5 for more 00105 * details. 00106 */ 00107 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from); 00108 00109 for (size_t ii = 0; ii < to_size; ++ii) { 00110 recipients = curl_slist_append(recipients, to[ii]); 00111 } 00112 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); 00113 00114 /* We're using a callback function to specify the payload (the headers and 00115 * body of the message). You could just use the CURLOPT_READDATA option to 00116 * specify a FILE pointer to read from. */ 00117 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); 00118 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); 00119 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 00120 00121 /* Since the traffic will be encrypted, it is very useful to turn on debug 00122 * information within libcurl to see what is happening during the transfer. 00123 */ 00124 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 00125 00126 /* Send the message */ 00127 res = curl_easy_perform(curl); 00128 00129 /* Check for errors */ 00130 if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); 00131 00132 /* Free the list of recipients */ 00133 curl_slist_free_all(recipients); 00134 00135 /* Always cleanup */ 00136 curl_easy_cleanup(curl); 00137 } 00138 }