diff options
Diffstat (limited to 'xs_curl.h')
| -rw-r--r-- | xs_curl.h | 46 |
1 files changed, 46 insertions, 0 deletions
| @@ -9,6 +9,10 @@ xs_dict *xs_http_request(const char *method, const char *url, | |||
| 9 | const xs_str *body, int b_size, int *status, | 9 | const xs_str *body, int b_size, int *status, |
| 10 | xs_str **payload, int *p_size, int timeout); | 10 | xs_str **payload, int *p_size, int timeout); |
| 11 | 11 | ||
| 12 | int xs_smtp_request(const char *url, const char *user, const char *pass, | ||
| 13 | const char *from, const char *to, const xs_str *body, | ||
| 14 | int use_ssl); | ||
| 15 | |||
| 12 | #ifdef XS_IMPLEMENTATION | 16 | #ifdef XS_IMPLEMENTATION |
| 13 | 17 | ||
| 14 | #include <curl/curl.h> | 18 | #include <curl/curl.h> |
| @@ -194,6 +198,48 @@ xs_dict *xs_http_request(const char *method, const char *url, | |||
| 194 | return response; | 198 | return response; |
| 195 | } | 199 | } |
| 196 | 200 | ||
| 201 | int xs_smtp_request(const char *url, const char *user, const char *pass, | ||
| 202 | const char *from, const char *to, const xs_str *body, | ||
| 203 | int use_ssl) | ||
| 204 | { | ||
| 205 | CURL *curl; | ||
| 206 | CURLcode res = CURLE_OK; | ||
| 207 | struct curl_slist *rcpt = NULL; | ||
| 208 | struct _payload_data pd = { | ||
| 209 | .data = (char *)body, | ||
| 210 | .size = xs_size(body), | ||
| 211 | .offset = 0 | ||
| 212 | }; | ||
| 213 | |||
| 214 | curl = curl_easy_init(); | ||
| 215 | |||
| 216 | curl_easy_setopt(curl, CURLOPT_URL, url); | ||
| 217 | if (user && pass) { | ||
| 218 | /* allow authless connections, to, e.g. localhost */ | ||
| 219 | curl_easy_setopt(curl, CURLOPT_USERNAME, user); | ||
| 220 | curl_easy_setopt(curl, CURLOPT_PASSWORD, pass); | ||
| 221 | } | ||
| 222 | |||
| 223 | if (use_ssl) | ||
| 224 | curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); | ||
| 225 | |||
| 226 | curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from); | ||
| 227 | |||
| 228 | rcpt = curl_slist_append(rcpt, to); | ||
| 229 | curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt); | ||
| 230 | |||
| 231 | curl_easy_setopt(curl, CURLOPT_READDATA, &pd); | ||
| 232 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, _post_callback); | ||
| 233 | curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); | ||
| 234 | |||
| 235 | res = curl_easy_perform(curl); | ||
| 236 | |||
| 237 | curl_easy_cleanup(curl); | ||
| 238 | curl_slist_free_all(rcpt); | ||
| 239 | |||
| 240 | return (int)res; | ||
| 241 | } | ||
| 242 | |||
| 197 | #endif /* XS_IMPLEMENTATION */ | 243 | #endif /* XS_IMPLEMENTATION */ |
| 198 | 244 | ||
| 199 | #endif /* _XS_CURL_H */ | 245 | #endif /* _XS_CURL_H */ |