123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- #include <stdio.h>
- #include <string.h>
- #include <curl/curl.h>
- #define FROM_MAIL "<sender@example.com>"
- #define TO_MAIL "<recipient@example.com>"
- #define CC_MAIL "<info@example.com>"
- static const char *payload_text =
- "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
- "To: " TO_MAIL "\r\n"
- "From: " FROM_MAIL "\r\n"
- "Cc: " CC_MAIL "\r\n"
- "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
- "rfcpedant.example.org>\r\n"
- "Subject: SMTP example message\r\n"
- "\r\n"
- "The body of the message starts here.\r\n"
- "\r\n"
- "It could be a lot of lines, could be MIME encoded, whatever.\r\n"
- "Check RFC 5322.\r\n";
- struct upload_status {
- size_t bytes_read;
- };
- static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
- {
- struct upload_status *upload_ctx = (struct upload_status *)userp;
- const char *data;
- size_t room = size * nmemb;
- if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
- return 0;
- }
- data = &payload_text[upload_ctx->bytes_read];
- if(data) {
- size_t len = strlen(data);
- if(room < len)
- len = room;
- memcpy(ptr, data, len);
- upload_ctx->bytes_read += len;
- return len;
- }
- return 0;
- }
- int main(void)
- {
- CURL *curl;
- CURLcode res = CURLE_OK;
- struct curl_slist *recipients = NULL;
- struct upload_status upload_ctx = { 0 };
- curl = curl_easy_init();
- if(curl) {
-
- curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
- curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
-
- curl_easy_setopt(curl, CURLOPT_URL, "smtps://mainserver.example.net");
-
- #ifdef SKIP_PEER_VERIFICATION
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
- #endif
-
- #ifdef SKIP_HOSTNAME_VERIFICATION
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
- #endif
-
- curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_MAIL);
-
- recipients = curl_slist_append(recipients, TO_MAIL);
- recipients = curl_slist_append(recipients, CC_MAIL);
- curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
-
- curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
- curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
- curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
-
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
-
- res = curl_easy_perform(curl);
-
- if(res != CURLE_OK)
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(res));
-
- curl_slist_free_all(recipients);
-
- curl_easy_cleanup(curl);
- }
- return (int)res;
- }
|