5 static size_t payload_source(
void* ptr,
size_t size,
size_t nmemb,
void* userp) {
7 size_t rdsize = size * nmemb;
9 if ((size == 0) || (nmemb == 0) || (rdsize < 1) || (upload_ctx->
pos >= upload_ctx->
size)) {
13 if (rdsize + upload_ctx->
pos > upload_ctx->
size) {
14 rdsize = upload_ctx->
size - upload_ctx->
pos;
17 memcpy(ptr, &upload_ctx->
payload[upload_ctx->
pos], rdsize);
18 upload_ctx->
pos += rdsize;
23 void send_message(
const char* dest,
const char* to[],
size_t to_size,
const char* from,
const char*
payload,
24 size_t payload_size) {
27 struct curl_slist* recipients = NULL;
31 upload_ctx.
size = payload_size;
34 curl = curl_easy_init();
37 curl_easy_setopt(curl, CURLOPT_URL, dest);
39 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
43 for (
size_t ii = 0; ii < to_size; ++ii) {
44 recipients = curl_slist_append(recipients, to[ii]);
46 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
48 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
49 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
50 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
53 res = curl_easy_perform(curl);
56 if (res != CURLE_OK) fprintf(stderr,
"curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
59 curl_slist_free_all(recipients);
61 curl_easy_cleanup(curl);
66 size_t payload_size,
const char* username,
const char* pw,
int disableVerify) {
68 CURLcode res = CURLE_OK;
69 struct curl_slist* recipients = NULL;
73 upload_ctx.
size = payload_size;
76 curl = curl_easy_init();
79 curl_easy_setopt(curl, CURLOPT_USERNAME, username);
80 curl_easy_setopt(curl, CURLOPT_PASSWORD, pw);
86 curl_easy_setopt(curl, CURLOPT_URL, dest);
93 curl_easy_setopt(curl, CURLOPT_USE_SSL, (
long)CURLUSESSL_ALL);
96 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
97 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
107 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
109 for (
size_t ii = 0; ii < to_size; ++ii) {
110 recipients = curl_slist_append(recipients, to[ii]);
112 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
117 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
118 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
119 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
124 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
127 res = curl_easy_perform(curl);
130 if (res != CURLE_OK) fprintf(stderr,
"curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
133 curl_slist_free_all(recipients);
136 curl_easy_cleanup(curl);
size_t size
Size of payload.
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)
Sends a message to the given SMTP server, using SSL encryption.
void send_message(const char *dest, const char *to[], size_t to_size, const char *from, const char *payload, size_t payload_size)
Sends a message to the given SMTP server.
const char * payload
payload string
size_t pos
Current position within payload.
Structure to track progress of upload in cURL send function.