aboutsummaryrefslogtreecommitdiff
path: root/xs_curl.h
diff options
context:
space:
mode:
authorgrunfink <grunfink@noreply.codeberg.org>2025-04-27 03:11:57 +0000
committergrunfink <grunfink@noreply.codeberg.org>2025-04-27 03:11:57 +0000
commit169c5c8c070c527e7462e6ac86b916e94649c1ac (patch)
treef1ce03b950a278a1985526cb58dc4e879c3ab046 /xs_curl.h
parent2e39e4e3c36dc23598173f3b6fe64a103574d089 (diff)
parent7f936d2d1a88840209895c6091801e8c4cfdaaa1 (diff)
Merge pull request 'do email notifications with CURL' (#283) from shtrophic/snac2:curl-smtp into master
Reviewed-on: https://codeberg.org/grunfink/snac2/pulls/283
Diffstat (limited to 'xs_curl.h')
-rw-r--r--xs_curl.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/xs_curl.h b/xs_curl.h
index 657644a..0609a08 100644
--- a/xs_curl.h
+++ b/xs_curl.h
@@ -9,6 +9,10 @@ xs_dict *xs_http_request(const char *method, const char *url,
const xs_str *body, int b_size, int *status,
xs_str **payload, int *p_size, int timeout);
+int xs_smtp_request(const char *url, const char *user, const char *pass,
+ const char *from, const char *to, const xs_str *body,
+ int use_ssl);
+
const char *xs_curl_strerr(int errnum);
#ifdef XS_IMPLEMENTATION
@@ -196,6 +200,48 @@ xs_dict *xs_http_request(const char *method, const char *url,
return response;
}
+int xs_smtp_request(const char *url, const char *user, const char *pass,
+ const char *from, const char *to, const xs_str *body,
+ int use_ssl)
+{
+ CURL *curl;
+ CURLcode res = CURLE_OK;
+ struct curl_slist *rcpt = NULL;
+ struct _payload_data pd = {
+ .data = (char *)body,
+ .size = xs_size(body),
+ .offset = 0
+ };
+
+ curl = curl_easy_init();
+
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ if (user && pass) {
+ /* allow authless connections, to, e.g. localhost */
+ curl_easy_setopt(curl, CURLOPT_USERNAME, user);
+ curl_easy_setopt(curl, CURLOPT_PASSWORD, pass);
+ }
+
+ if (use_ssl)
+ curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
+
+ curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
+
+ rcpt = curl_slist_append(rcpt, to);
+ curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt);
+
+ curl_easy_setopt(curl, CURLOPT_READDATA, &pd);
+ curl_easy_setopt(curl, CURLOPT_READFUNCTION, _post_callback);
+ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
+
+ res = curl_easy_perform(curl);
+
+ curl_easy_cleanup(curl);
+ curl_slist_free_all(rcpt);
+
+ return (int)res;
+}
+
const char *xs_curl_strerr(int errnum)
{