| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) 1998 - 2019, Daniel Stenberg, <[email protected]>, et al. |
| 9 |
* |
| 10 |
* This software is licensed as described in the file COPYING, which |
| 11 |
* you should have received as part of this distribution. The terms |
| 12 |
* are also available at https://curl.haxx.se/docs/copyright.html. |
| 13 |
* |
| 14 |
* You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 |
* copies of the Software, and permit persons to whom the Software is |
| 16 |
* furnished to do so, under the terms of the COPYING file. |
| 17 |
* |
| 18 |
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 |
* KIND, either express or implied. |
| 20 |
* |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
/* <DESC> |
| 24 |
* IMAP example showing how to send e-mails |
| 25 |
* </DESC> |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <string.h> |
| 30 |
#include <curl/curl.h> |
| 31 |
|
| 32 |
/* This is a simple example showing how to send mail using libcurl's IMAP |
| 33 |
* capabilities. |
| 34 |
* |
| 35 |
* Note that this example requires libcurl 7.30.0 or above. |
| 36 |
*/ |
| 37 |
|
| 38 |
#define FROM "<[email protected]>" |
| 39 |
#define TO "<[email protected]>" |
| 40 |
#define CC "<[email protected]>" |
| 41 |
|
| 42 |
static const char *payload_text[] = { |
| 43 |
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n", |
| 44 |
"To: " TO "\r\n", |
| 45 |
"From: " FROM "(Example User)\r\n", |
| 46 |
"Cc: " CC "(Another example User)\r\n", |
| 47 |
"Message-ID: " |
| 48 |
"<[email protected]>\r\n", |
| 49 |
"Subject: IMAP example message\r\n", |
| 50 |
"\r\n", /* empty line to divide headers from body, see RFC5322 */ |
| 51 |
"The body of the message starts here.\r\n", |
| 52 |
"\r\n", |
| 53 |
"It could be a lot of lines, could be MIME encoded, whatever.\r\n", |
| 54 |
"Check RFC5322.\r\n", |
| 55 |
NULL |
| 56 |
}; |
| 57 |
|
| 58 |
struct upload_status { |
| 59 |
int lines_read; |
| 60 |
}; |
| 61 |
|
| 62 |
static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp) |
| 63 |
{ |
| 64 |
struct upload_status *upload_ctx = (struct upload_status *)userp; |
| 65 |
const char *data; |
| 66 |
|
| 67 |
if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) { |
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
data = payload_text[upload_ctx->lines_read]; |
| 72 |
|
| 73 |
if(data) { |
| 74 |
size_t len = strlen(data); |
| 75 |
memcpy(ptr, data, len); |
| 76 |
upload_ctx->lines_read++; |
| 77 |
|
| 78 |
return len; |
| 79 |
} |
| 80 |
|
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
int main(void) |
| 85 |
{ |
| 86 |
CURL *curl; |
| 87 |
CURLcode res = CURLE_OK; |
| 88 |
|
| 89 |
curl = curl_easy_init(); |
| 90 |
if(curl) { |
| 91 |
const char **p; |
| 92 |
long infilesize; |
| 93 |
struct upload_status upload_ctx; |
| 94 |
|
| 95 |
upload_ctx.lines_read = 0; |
| 96 |
|
| 97 |
/* Set username and password */ |
| 98 |
curl_easy_setopt(curl, CURLOPT_USERNAME, "user"); |
| 99 |
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret"); |
| 100 |
|
| 101 |
/* This will create a new message 100. Note that you should perform an |
| 102 |
* EXAMINE command to obtain the UID of the next message to create and a |
| 103 |
* SELECT to ensure you are creating the message in the OUTBOX. */ |
| 104 |
curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/100"); |
| 105 |
|
| 106 |
/* In this case, we're using a callback function to specify the data. You |
| 107 |
* could just use the CURLOPT_READDATA option to specify a FILE pointer to |
| 108 |
* read from. */ |
| 109 |
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); |
| 110 |
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); |
| 111 |
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); |
| 112 |
|
| 113 |
infilesize = 0; |
| 114 |
for(p = payload_text; *p; ++p) { |
| 115 |
infilesize += (long)strlen(*p); |
| 116 |
} |
| 117 |
curl_easy_setopt(curl, CURLOPT_INFILESIZE, infilesize); |
| 118 |
|
| 119 |
/* Perform the append */ |
| 120 |
res = curl_easy_perform(curl); |
| 121 |
|
| 122 |
/* Check for errors */ |
| 123 |
if(res != CURLE_OK) |
| 124 |
fprintf(stderr, "curl_easy_perform() failed: %s\n", |
| 125 |
curl_easy_strerror(res)); |
| 126 |
|
| 127 |
/* Always cleanup */ |
| 128 |
curl_easy_cleanup(curl); |
| 129 |
} |
| 130 |
|
| 131 |
return (int)res; |
| 132 |
} |