00001 #include "curl_send_message.h"
00002 #include <curl/curl.h>
00003 #include <time.h>
00004
00005
00006
00007 static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
00008 {
00009 struct upload_status *upload_ctx = (struct upload_status *)userp;
00010 size_t rdsize = size * nmemb;
00011
00012 if ((size == 0) || (nmemb == 0) || (rdsize < 1) || (upload_ctx->pos >= upload_ctx->size))
00013 {
00014 return 0;
00015 }
00016
00017 if (rdsize + upload_ctx->pos > upload_ctx->size)
00018 {
00019 rdsize = upload_ctx->size - upload_ctx->pos;
00020 }
00021
00022 memcpy(ptr, &upload_ctx->payload[upload_ctx->pos], rdsize);
00023 upload_ctx->pos += rdsize;
00024
00025 return rdsize;
00026 }
00027
00028 void send_message(const char* dest, const char* to[], size_t to_size, const char* from, const char* payload, size_t payload_size)
00029 {
00030 CURL *curl;
00031 CURLcode res;
00032 struct curl_slist *recipients = NULL;
00033 struct upload_status upload_ctx;
00034
00035 upload_ctx.pos = 0;
00036 upload_ctx.size = payload_size;
00037 upload_ctx.payload = payload;
00038
00039 curl = curl_easy_init();
00040 if (curl)
00041 {
00042
00043 curl_easy_setopt(curl, CURLOPT_URL, dest);
00044
00045 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
00046
00047
00048
00049 for (size_t ii = 0; ii < to_size; ++ii)
00050 {
00051 recipients = curl_slist_append(recipients, to[ii]);
00052 }
00053 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
00054
00055 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
00056 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
00057 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00058
00059
00060 res = curl_easy_perform(curl);
00061
00062
00063 if (res != CURLE_OK)
00064 fprintf(stderr, "curl_easy_perform() failed: %s\n",
00065 curl_easy_strerror(res));
00066
00067
00068 curl_slist_free_all(recipients);
00069
00070 curl_easy_cleanup(curl);
00071 }
00072 }
00073
00074 void send_message_ssl(const char* dest, const char* to[], size_t to_size, const char* from, const char* payload, size_t payload_size, const char* username, const char* pw, int disableVerify)
00075 {
00076 CURL *curl;
00077 CURLcode res = CURLE_OK;
00078 struct curl_slist *recipients = NULL;
00079 struct upload_status upload_ctx;
00080
00081 upload_ctx.pos = 0;
00082 upload_ctx.size = payload_size;
00083 upload_ctx.payload = payload;
00084
00085 curl = curl_easy_init();
00086 if (curl)
00087 {
00088
00089 curl_easy_setopt(curl, CURLOPT_USERNAME, username);
00090 curl_easy_setopt(curl, CURLOPT_PASSWORD, pw);
00091
00092
00093
00094
00095
00096 curl_easy_setopt(curl, CURLOPT_URL, dest);
00097
00098
00099
00100
00101
00102
00103 curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
00104
00105 if (disableVerify)
00106 {
00107 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
00108 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
00119
00120 for (size_t ii = 0; ii < to_size; ++ii)
00121 {
00122 recipients = curl_slist_append(recipients, to[ii]);
00123 }
00124 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
00125
00126
00127
00128
00129 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
00130 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
00131 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
00132
00133
00134
00135
00136 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00137
00138
00139 res = curl_easy_perform(curl);
00140
00141
00142 if (res != CURLE_OK)
00143 fprintf(stderr, "curl_easy_perform() failed: %s\n",
00144 curl_easy_strerror(res));
00145
00146
00147 curl_slist_free_all(recipients);
00148
00149
00150 curl_easy_cleanup(curl);
00151 }
00152 }